• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tensorchord / envd / 12730559639

12 Jan 2025 04:47AM UTC coverage: 44.021% (-0.06%) from 44.084%
12730559639

push

github

web-flow
feat: upgrade to python 3.11 (#1940)

6299 of 14309 relevant lines covered (44.02%)

564.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.95
/pkg/editor/vscode/vscode.go
1
// Copyright 2023 The envd Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package vscode
16

17
import (
18
        "fmt"
19
        "io"
20
        "net/http"
21
        "os"
22

23
        "github.com/cockroachdb/errors"
24
        "github.com/sirupsen/logrus"
25

26
        "github.com/tensorchord/envd/pkg/home"
27
        "github.com/tensorchord/envd/pkg/util/ziputil"
28
)
29

30
const (
31
        cacheKeyPrefix = "vscode-plugins"
32
)
33

34
type Client interface {
35
        DownloadOrCache(plugin Plugin) (bool, error)
36
        PluginPath(p Plugin) string
37
}
38

39
type generalClient struct {
40
        vendor MarketplaceVendor
41
        logger *logrus.Entry
42
}
43

44
func NewClient(vendor MarketplaceVendor) (Client, error) {
2✔
45
        switch vendor {
2✔
46
        case MarketplaceVendorOpenVSX:
2✔
47
                return &generalClient{
2✔
48
                        vendor: vendor,
2✔
49
                        logger: logrus.WithField("vendor", MarketplaceVendorOpenVSX),
2✔
50
                }, nil
2✔
51
        case MarketplaceVendorVSCode:
×
52
                return &generalClient{
×
53
                        vendor: vendor,
×
54
                        logger: logrus.WithField("vendor", MarketplaceVendorVSCode),
×
55
                }, nil
×
56
        default:
×
57
                return nil, errors.Errorf("unknown marketplace vendor %s", vendor)
×
58
        }
59
}
60

61
func (c generalClient) PluginPath(p Plugin) string {
2✔
62
        if p.Version != nil {
2✔
63
                return fmt.Sprintf("%s.%s-%s@%s/extension/", p.Publisher, p.Extension, *p.Version, p.Platform)
×
64

×
65
        }
×
66
        return fmt.Sprintf("%s.%s@%s/extension/", p.Publisher, p.Extension, p.Platform)
2✔
67
}
68

69
func unzipPath(p Plugin) string {
2✔
70
        if p.Version != nil {
2✔
71
                return fmt.Sprintf("%s/%s.%s-%s@%s", home.GetManager().CacheDir(),
×
72
                        p.Publisher, p.Extension, *p.Version, p.Platform)
×
73
        }
×
74
        return fmt.Sprintf("%s/%s.%s@%s", home.GetManager().CacheDir(),
2✔
75
                p.Publisher, p.Extension, p.Platform)
2✔
76
}
77

78
// DownloadOrCache downloads or cache the plugin.
79
// If the plugin is already downloaded, it returns true.
80
func (c generalClient) DownloadOrCache(p Plugin) (bool, error) {
2✔
81
        cacheKey := fmt.Sprintf("%s-%s", cacheKeyPrefix, p)
2✔
82
        if home.GetManager().Cached(cacheKey) {
2✔
83
                logrus.WithFields(logrus.Fields{
×
84
                        "cache": cacheKey,
×
85
                }).Debugf("vscode plugin %s already exists in cache", p)
×
86
                return true, nil
×
87
        }
×
88

89
        var url, filename string
2✔
90
        if c.vendor == MarketplaceVendorVSCode {
2✔
91
                if p.Version == nil {
×
92
                        return false, errors.New("version is required for vscode marketplace")
×
93
                }
×
94
                // TODO(gaocegege): Support version auto-detection.
95
                url = fmt.Sprintf(vendorVSCodeTemplate,
×
96
                        p.Publisher, p.Publisher, p.Extension, *p.Version, p.Platform)
×
97
                filename = fmt.Sprintf("%s/%s.%s-%s@%s.vsix", home.GetManager().CacheDir(),
×
98
                        p.Publisher, p.Extension, *p.Version, p.Platform)
×
99
        } else {
2✔
100
                var err error
2✔
101
                url, err = GetLatestVersionURL(p)
2✔
102
                if err != nil {
2✔
103
                        return false, errors.Wrap(err, "failed to get latest version url")
×
104
                }
×
105
                filename = fmt.Sprintf("%s/%s.%s@%s.vsix", home.GetManager().CacheDir(),
2✔
106
                        p.Publisher, p.Extension, p.Platform)
2✔
107
        }
108

109
        logger := logrus.WithFields(logrus.Fields{
2✔
110
                "publisher": p.Publisher,
2✔
111
                "extension": p.Extension,
2✔
112
                "version":   p.Version,
2✔
113
                "platform":  p.Platform,
2✔
114
                "url":       url,
2✔
115
                "file":      filename,
2✔
116
        })
2✔
117

2✔
118
        logger.Debugf("downloading vscode plugin %s", p)
2✔
119
        out, err := os.Create(filename)
2✔
120

2✔
121
        if err != nil {
2✔
122
                return false, err
×
123
        }
×
124
        defer out.Close()
2✔
125

2✔
126
        resp, err := http.Get(url)
2✔
127
        if err != nil {
2✔
128
                return false, err
×
129
        }
×
130
        logger.Debugf("downloading vscode plugin")
2✔
131

2✔
132
        defer resp.Body.Close()
2✔
133
        _, err = io.Copy(out, resp.Body)
2✔
134
        if err != nil {
2✔
135
                return false, err
×
136
        }
×
137

138
        _, err = ziputil.Unzip(filename, unzipPath(p))
2✔
139
        if err != nil {
2✔
140
                return false, errors.Wrap(err, "failed to unzip")
×
141
        }
×
142

143
        if err := home.GetManager().MarkCache(cacheKey, true); err != nil {
2✔
144
                return false, errors.Wrap(err, "failed to update cache status")
×
145
        }
×
146
        return false, nil
2✔
147
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc