@@ -19,7 +19,6 @@ import (
1919 "io"
2020 "net/http"
2121 "os"
22- "strings"
2322
2423 "github.com/cockroachdb/errors"
2524 "github.com/sirupsen/logrus"
@@ -38,29 +37,75 @@ type Client interface {
3837}
3938
4039type generalClient struct {
40+ vendor MarketplaceVendor
41+ logger * logrus.Entry
4142}
4243
43- func NewClient () Client {
44- return & generalClient {}
44+ func NewClient (vendor MarketplaceVendor ) (Client , error ) {
45+ switch vendor {
46+ case MarketplaceVendorOpenVSX :
47+ return & generalClient {
48+ vendor : vendor ,
49+ logger : logrus .WithField ("vendor" , MarketplaceVendorOpenVSX ),
50+ }, nil
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+ }
4559}
4660
4761func (c generalClient ) PluginPath (p Plugin ) string {
48- return fmt .Sprintf ("%s.%s-%s/extension/" , p .Publisher , p .Extension , p .Version )
62+ if p .Version != nil {
63+ return fmt .Sprintf ("%s.%s-%s/extension/" , p .Publisher , p .Extension , * p .Version )
64+
65+ }
66+ return fmt .Sprintf ("%s.%s/extension/" , p .Publisher , p .Extension )
4967}
5068
5169func unzipPath (p Plugin ) string {
52- return fmt .Sprintf ("%s/%s.%s-%s" , home .GetManager ().CacheDir (),
53- p .Publisher , p .Extension , p .Version )
70+ if p .Version != nil {
71+ return fmt .Sprintf ("%s/%s.%s-%s" , home .GetManager ().CacheDir (),
72+ p .Publisher , p .Extension , * p .Version )
73+ }
74+ return fmt .Sprintf ("%s/%s.%s" , home .GetManager ().CacheDir (),
75+ p .Publisher , p .Extension )
5476}
5577
5678// DownloadOrCache downloads or cache the plugin.
5779// If the plugin is already downloaded, it returns true.
5880func (c generalClient ) DownloadOrCache (p Plugin ) (bool , error ) {
59- url := fmt .Sprintf (vscodePackageURLTemplate ,
60- p .Publisher , p .Publisher , p .Extension , p .Version )
81+ cacheKey := fmt .Sprintf ("%s-%s" , cachekeyPrefix , p )
82+ if home .GetManager ().Cached (cacheKey ) {
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
90+ if c .vendor == MarketplaceVendorVSCode {
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 )
97+ filename = fmt .Sprintf ("%s/%s.%s-%s.vsix" , home .GetManager ().CacheDir (),
98+ p .Publisher , p .Extension , * p .Version )
99+ } else {
100+ var err error
101+ url , err = GetLatestVersionURL (p )
102+ if err != nil {
103+ return false , errors .Wrap (err , "failed to get latest version url" )
104+ }
105+ filename = fmt .Sprintf ("%s/%s.%s.vsix" , home .GetManager ().CacheDir (),
106+ p .Publisher , p .Extension )
107+ }
61108
62- filename := fmt .Sprintf ("%s/%s.%s-%s.vsix" , home .GetManager ().CacheDir (),
63- p .Publisher , p .Extension , p .Version )
64109 logger := logrus .WithFields (logrus.Fields {
65110 "publisher" : p .Publisher ,
66111 "extension" : p .Extension ,
@@ -69,14 +114,6 @@ func (c generalClient) DownloadOrCache(p Plugin) (bool, error) {
69114 "file" : filename ,
70115 })
71116
72- cacheKey := fmt .Sprintf ("%s-%s" , cachekeyPrefix , p )
73- if home .GetManager ().Cached (cacheKey ) {
74- logger .WithFields (logrus.Fields {
75- "cache" : cacheKey ,
76- }).Debugf ("vscode plugin %s already exists in cache" , p )
77- return true , nil
78- }
79-
80117 logger .Debugf ("downloading vscode plugin %s" , p )
81118 out , err := os .Create (filename )
82119
@@ -107,30 +144,3 @@ func (c generalClient) DownloadOrCache(p Plugin) (bool, error) {
107144 }
108145 return false , nil
109146}
110-
111- func ParsePlugin (p string ) (* Plugin , error ) {
112- indexPublisher := strings .Index (p , "." )
113- if indexPublisher == - 1 {
114- return nil , errors .New ("invalid publisher" )
115- }
116- publisher := p [:indexPublisher ]
117-
118- indexExtension := strings .LastIndex (p [indexPublisher :], "-" )
119- if indexExtension == - 1 {
120- return nil , errors .New ("invalid extension" )
121- }
122-
123- indexExtension = indexPublisher + indexExtension
124- extension := p [indexPublisher + 1 : indexExtension ]
125- version := p [indexExtension + 1 :]
126- logrus .WithFields (logrus.Fields {
127- "publisher" : publisher ,
128- "extension" : extension ,
129- "version" : version ,
130- }).Debug ("vscode plugin is parsed" )
131- return & Plugin {
132- Publisher : publisher ,
133- Extension : extension ,
134- Version : version ,
135- }, nil
136- }
0 commit comments