Skip to content

Commit cda1083

Browse files
committed
feat: improve the extension downloader
1 parent b565347 commit cda1083

File tree

4 files changed

+135
-77
lines changed

4 files changed

+135
-77
lines changed

console/atest-desktop/package.json

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
11
{
2-
"name": "atest-desktop",
3-
"version": "0.0.1",
4-
"description": "API Testing Desktop Application",
5-
"main": "main.js",
6-
"scripts": {
7-
"test": "jest --coverage",
8-
"start": "electron-forge start",
9-
"package": "electron-forge package",
10-
"make": "electron-forge make",
11-
"publish": "electron-forge publish"
12-
},
13-
"author": "linuxsuren",
14-
"license": "ISC",
15-
"devDependencies": {
16-
"@electron-forge/cli": "^7.8.2",
17-
"@electron-forge/maker-deb": "^7.8.2",
18-
"@electron-forge/maker-dmg": "^7.8.2",
19-
"@electron-forge/maker-rpm": "^7.8.2",
20-
"@electron-forge/maker-squirrel": "^7.8.2",
21-
"@electron-forge/maker-wix": "^7.8.2",
22-
"@electron-forge/maker-zip": "^7.8.2",
23-
"@electron-forge/plugin-auto-unpack-natives": "^7.8.2",
24-
"@electron-forge/plugin-fuses": "^7.8.2",
25-
"@electron-forge/publisher-github": "^7.8.2",
26-
"@electron/fuses": "^1.8.0",
27-
"electron": "^37.2.5",
28-
"electron-wix-msi": "^5.1.3",
29-
"jest": "^30.0.4",
30-
"node-abi": "^4.12.0"
31-
},
32-
"dependencies": {
33-
"child_process": "^1.0.2",
34-
"electron-json-storage": "^4.6.0",
35-
"electron-log": "^5.4.1",
36-
"electron-squirrel-startup": "^1.0.1",
37-
"electron-store": "^10.1.0"
38-
},
39-
"build": {
40-
"extraResources": [
41-
"./assets/*"
42-
]
43-
},
44-
"optionalDependencies": {
45-
"appdmg": "^0.6.6"
46-
}
2+
"name": "atest-desktop",
3+
"version": "0.0.1",
4+
"description": "API Testing Desktop Application",
5+
"main": "main.js",
6+
"scripts": {
7+
"test": "jest --coverage",
8+
"start": "electron-forge start",
9+
"package": "electron-forge package",
10+
"make": "electron-forge make",
11+
"publish": "electron-forge publish"
12+
},
13+
"engines": {
14+
"node": "22.12.0"
15+
},
16+
"author": "linuxsuren",
17+
"license": "ISC",
18+
"devDependencies": {
19+
"@electron-forge/cli": "^7.8.2",
20+
"@electron-forge/maker-deb": "^7.8.2",
21+
"@electron-forge/maker-dmg": "^7.8.2",
22+
"@electron-forge/maker-rpm": "^7.8.2",
23+
"@electron-forge/maker-squirrel": "^7.8.2",
24+
"@electron-forge/maker-wix": "^7.8.2",
25+
"@electron-forge/maker-zip": "^7.8.2",
26+
"@electron-forge/plugin-auto-unpack-natives": "^7.8.2",
27+
"@electron-forge/plugin-fuses": "^7.8.2",
28+
"@electron-forge/publisher-github": "^7.8.2",
29+
"@electron/fuses": "^1.8.0",
30+
"electron": "^37.2.5",
31+
"electron-wix-msi": "^5.1.3",
32+
"jest": "^30.0.4",
33+
"node-abi": "^4.12.0"
34+
},
35+
"dependencies": {
36+
"child_process": "^1.0.2",
37+
"electron-json-storage": "^4.6.0",
38+
"electron-log": "^5.4.1",
39+
"electron-squirrel-startup": "^1.0.1",
40+
"electron-store": "^10.1.0"
41+
},
42+
"build": {
43+
"extraResources": [
44+
"./assets/*"
45+
]
46+
},
47+
"optionalDependencies": {
48+
"appdmg": "^0.6.6"
49+
}
4750
}

pkg/downloader/extension.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,31 @@ import (
2525
"strings"
2626
)
2727

28-
type extensionDownloader struct {
28+
type ExtensionDownloader struct {
2929
OCIDownloader
3030
os, arch string
3131
kind string
3232
extFile string
3333
imagePrefix string
3434
}
3535

36-
func NewStoreDownloader() PlatformAwareOCIDownloader {
37-
ociDownloader := &extensionDownloader{
36+
type ExtensionDownloaderOption func(*ExtensionDownloader)
37+
38+
func NewStoreDownloader(opts ...ExtensionDownloaderOption) PlatformAwareOCIDownloader {
39+
ociDownloader := &ExtensionDownloader{
3840
OCIDownloader: NewDefaultOCIDownloader(),
3941
}
4042
ociDownloader.WithOS(runtime.GOOS)
4143
ociDownloader.WithArch(runtime.GOARCH)
4244
ociDownloader.WithImagePrefix("linuxsuren")
4345
ociDownloader.WithKind("store")
46+
for _, opt := range opts {
47+
opt(ociDownloader)
48+
}
4449
return ociDownloader
4550
}
4651

47-
func (d *extensionDownloader) Download(name, tag, _ string) (reader io.Reader, err error) {
52+
func (d *ExtensionDownloader) Download(name, tag, _ string) (reader io.Reader, err error) {
4853
name = strings.TrimPrefix(name, fmt.Sprintf("atest-%s-", d.kind))
4954
if d.os == "" {
5055
d.extFile = fmt.Sprintf("atest-%s-%s.tar.gz", d.kind, name)
@@ -71,25 +76,25 @@ func WriteTo(reader io.Reader, dir, file string) (err error) {
7176
return
7277
}
7378

74-
func (d *extensionDownloader) GetTargetFile() string {
79+
func (d *ExtensionDownloader) GetTargetFile() string {
7580
return d.extFile
7681
}
7782

78-
func (d *extensionDownloader) WithOS(os string) {
83+
func (d *ExtensionDownloader) WithOS(os string) {
7984
d.os = os
8085
}
8186

82-
func (d *extensionDownloader) WithImagePrefix(imagePrefix string) {
87+
func (d *ExtensionDownloader) WithImagePrefix(imagePrefix string) {
8388
d.imagePrefix = imagePrefix
8489
}
8590

86-
func (d *extensionDownloader) WithArch(arch string) {
91+
func (d *ExtensionDownloader) WithArch(arch string) {
8792
d.arch = arch
8893
if d.arch == "amd64" {
8994
d.arch = "amd64_v1"
9095
}
9196
}
9297

93-
func (d *extensionDownloader) WithKind(kind string) {
98+
func (d *ExtensionDownloader) WithKind(kind string) {
9499
d.kind = kind
95100
}

pkg/downloader/oci.go

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type OCIDownloader interface {
4242
Download(image, tag, file string) (reader io.Reader, err error)
4343
}
4444

45+
type OICDownloaderOption func(*DefaultOCIDownloader)
46+
4547
type PlatformAwareOCIDownloader interface {
4648
OCIDownloader
4749
WithOS(string)
@@ -51,29 +53,41 @@ type PlatformAwareOCIDownloader interface {
5153
WithImagePrefix(string)
5254
}
5355

54-
type defaultOCIDownloader struct {
56+
type DefaultOCIDownloader struct {
5557
ctx context.Context
5658
timeout time.Duration
5759
serviceURL string
5860
registry string
5961
rawImage string
6062
protocol string
6163
roundTripper http.RoundTripper
64+
skipLayer func(layer *Layer) bool
6265
}
6366

64-
func NewDefaultOCIDownloader() OCIDownloader {
65-
return &defaultOCIDownloader{
67+
func NewDefaultOCIDownloader(opts ...OICDownloaderOption) OCIDownloader {
68+
downloader := &DefaultOCIDownloader{
6669
protocol: "https",
6770
timeout: time.Minute,
6871
ctx: context.Background(),
6972
}
73+
74+
for _, opt := range opts {
75+
opt(downloader)
76+
}
77+
78+
if downloader.skipLayer == nil {
79+
downloader.skipLayer = func(layer *Layer) bool {
80+
return false
81+
}
82+
}
83+
return downloader
7084
}
7185

72-
func (d *defaultOCIDownloader) WithBasicAuth(username string, password string) {
86+
func (d *DefaultOCIDownloader) WithBasicAuth(username string, password string) {
7387
fmt.Println("not support yet")
7488
}
7589

76-
func (d *defaultOCIDownloader) Download(image, tag, file string) (reader io.Reader, err error) {
90+
func (d *DefaultOCIDownloader) Download(image, tag, file string) (reader io.Reader, err error) {
7791
fmt.Println("start to download", image)
7892

7993
if d.registry == "" {
@@ -128,7 +142,7 @@ func (d *defaultOCIDownloader) Download(image, tag, file string) (reader io.Read
128142
}
129143

130144
for _, layer := range manifest.Layers {
131-
if v, ok := layer.Annotations["org.opencontainers.image.title"]; ok && v == file {
145+
if v, ok := layer.Annotations["org.opencontainers.image.title"]; ok && v == file && !d.skipLayer(&layer) {
132146
reader, err = d.downloadLayer(d.rawImage, layer.Digest, authStr)
133147
return
134148
}
@@ -139,33 +153,67 @@ func (d *defaultOCIDownloader) Download(image, tag, file string) (reader io.Read
139153
return
140154
}
141155

142-
func (d *defaultOCIDownloader) WithRegistry(registry string) {
156+
func (d *DefaultOCIDownloader) WithRegistry(registry string) {
143157
d.registry = registry
144158
}
145159

146-
func (d *defaultOCIDownloader) WithInsecure(insecure bool) {
160+
func WithRegistry(registry string) OICDownloaderOption {
161+
return func(d *DefaultOCIDownloader) {
162+
d.registry = registry
163+
}
164+
}
165+
166+
func WithInsecure(insecure bool) OICDownloaderOption {
167+
return func(d *DefaultOCIDownloader) {
168+
if insecure {
169+
d.protocol = "http"
170+
} else {
171+
d.protocol = "https"
172+
}
173+
}
174+
}
175+
176+
func WithTimeout(timeout time.Duration) OICDownloaderOption {
177+
return func(d *DefaultOCIDownloader) {
178+
d.timeout = timeout
179+
}
180+
}
181+
182+
func WithContext(ctx context.Context) OICDownloaderOption {
183+
return func(d *DefaultOCIDownloader) {
184+
d.ctx = ctx
185+
}
186+
}
187+
188+
func WithRoundTripper(rt http.RoundTripper) OICDownloaderOption {
189+
return func(d *DefaultOCIDownloader) {
190+
d.roundTripper = rt
191+
}
192+
}
193+
194+
func (d *DefaultOCIDownloader) WithInsecure(insecure bool) {
147195
if insecure {
148196
d.protocol = "http"
149197
} else {
150198
d.protocol = "https"
151199
}
152200
}
153201

154-
func (d *defaultOCIDownloader) WithTimeout(timeout time.Duration) {
202+
func (d *DefaultOCIDownloader) WithTimeout(timeout time.Duration) {
155203
d.timeout = timeout
156204
}
157205

158-
func (d *defaultOCIDownloader) WithContext(ctx context.Context) {
206+
func (d *DefaultOCIDownloader) WithContext(ctx context.Context) {
159207
d.ctx = ctx
160208
}
161209

162-
func (d *defaultOCIDownloader) WithRoundTripper(rt http.RoundTripper) {
210+
func (d *DefaultOCIDownloader) WithRoundTripper(rt http.RoundTripper) {
163211
d.roundTripper = rt
164212
}
165213

166214
// getLatestTag returns the latest artifact tag
167215
// we assume the artifact tags do not have the prefix `v`
168-
func (d *defaultOCIDownloader) getLatestTag(image, authToken string) (tag string, err error) {
216+
func (d *DefaultOCIDownloader) getLatestTag(image, authToken string) (tag string, err error) {
169217
var req *http.Request
170218
if req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("%s://%s/v2/%s/tags/list", d.protocol, d.registry, image), nil); err != nil {
171219
return
@@ -209,7 +257,7 @@ func (d *defaultOCIDownloader) getLatestTag(image, authToken string) (tag string
209257
return
210258
}
211259

212-
func (d *defaultOCIDownloader) getHTTPClient() (client *http.Client) {
260+
func (d *DefaultOCIDownloader) getHTTPClient() (client *http.Client) {
213261
client = &http.Client{
214262
Timeout: d.timeout,
215263
Transport: d.roundTripper,
@@ -222,7 +270,7 @@ type ImageTagList struct {
222270
Tags []string `json:"tags"`
223271
}
224272

225-
func (d *defaultOCIDownloader) downloadLayer(image, digest, authToken string) (reader io.Reader, err error) {
273+
func (d *DefaultOCIDownloader) downloadLayer(image, digest, authToken string) (reader io.Reader, err error) {
226274
layerURL := fmt.Sprintf("%s://%s/v2/%s/blobs/%s", d.protocol, d.registry, image, digest)
227275
buffer := bytes.NewBuffer(nil)
228276

@@ -281,7 +329,7 @@ const (
281329
HeaderWWWAuthenticate = "www-authenticate"
282330
)
283331

284-
func (d *defaultOCIDownloader) auth(image string) (authToken string, err error) {
332+
func (d *DefaultOCIDownloader) auth(image string) (authToken string, err error) {
285333
var authURL string
286334
if authURL, d.serviceURL, err = detectAuthURL(d.protocol, fmt.Sprintf("%s/%s", d.registry, d.rawImage)); err != nil {
287335
return
@@ -314,7 +362,9 @@ type RegistryAuth struct {
314362
}
315363

316364
type Manifest struct {
317-
Layers []Layer `json:"layers"`
365+
Config map[string]string `json:"config"`
366+
Layers []Layer `json:"layers"`
367+
Annotations map[string]string `json:"annotations"`
318368
}
319369

320370
type Layer struct {

pkg/server/remote_server.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,14 +1247,14 @@ func (s *server) GetStores(ctx context.Context, in *SimpleQuery) (reply *Stores,
12471247
defer wg.Done()
12481248

12491249
grpcStore := ToGRPCStore(item)
1250-
if item.Disabled {
1251-
return
1252-
}
1253-
1254-
storeStatus, sErr := s.VerifyStore(ctx, &SimpleQuery{Name: item.Name})
1255-
grpcStore.Ready = sErr == nil && storeStatus.Ready
1256-
grpcStore.ReadOnly = storeStatus.ReadOnly
12571250
grpcStore.Password = util.PasswordPlaceholder
1251+
grpcStore.Ready = false
1252+
1253+
if !item.Disabled {
1254+
storeStatus, sErr := s.VerifyStore(ctx, &SimpleQuery{Name: item.Name})
1255+
grpcStore.Ready = sErr == nil && storeStatus.Ready
1256+
grpcStore.ReadOnly = storeStatus.ReadOnly
1257+
}
12581258

12591259
mu.Lock()
12601260
reply.Data = append(reply.Data, grpcStore)

0 commit comments

Comments
 (0)