Skip to content

Commit f6fa6eb

Browse files
committed
feat: support to download differnt kinds of ext
1 parent 2f409ca commit f6fa6eb

File tree

4 files changed

+83
-67
lines changed

4 files changed

+83
-67
lines changed

cmd/extension.go

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,72 +17,75 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"fmt"
21-
"io"
22-
"path/filepath"
23-
"runtime"
24-
"time"
20+
"fmt"
21+
"io"
22+
"path/filepath"
23+
"runtime"
24+
"time"
2525

26-
"github.com/linuxsuren/api-testing/pkg/downloader"
27-
"github.com/spf13/cobra"
26+
"github.com/linuxsuren/api-testing/pkg/downloader"
27+
"github.com/spf13/cobra"
2828
)
2929

3030
type extensionOption struct {
31-
ociDownloader downloader.PlatformAwareOCIDownloader
32-
output string
33-
registry string
34-
tag string
35-
os string
36-
arch string
37-
timeout time.Duration
38-
imagePrefix string
31+
ociDownloader downloader.PlatformAwareOCIDownloader
32+
output string
33+
registry string
34+
kind string
35+
tag string
36+
os string
37+
arch string
38+
timeout time.Duration
39+
imagePrefix string
3940
}
4041

4142
func createExtensionCommand(ociDownloader downloader.PlatformAwareOCIDownloader) (c *cobra.Command) {
42-
opt := &extensionOption{
43-
ociDownloader: ociDownloader,
44-
}
45-
c = &cobra.Command{
46-
Use: "extension",
47-
Short: "Download extension binary files",
48-
Long: "Download the store extension files",
49-
Args: cobra.MinimumNArgs(1),
50-
RunE: opt.runE,
51-
}
52-
flags := c.Flags()
53-
flags.StringVarP(&opt.output, "output", "", ".", "The target directory")
54-
flags.StringVarP(&opt.tag, "tag", "", "", "The extension image tag, try to find the latest one if this is empty")
55-
flags.StringVarP(&opt.registry, "registry", "", "", "The target extension image registry, supported: docker.io, ghcr.io")
56-
flags.StringVarP(&opt.os, "os", "", runtime.GOOS, "The OS")
57-
flags.StringVarP(&opt.arch, "arch", "", runtime.GOARCH, "The architecture")
58-
flags.DurationVarP(&opt.timeout, "timeout", "", time.Minute, "The timeout of downloading")
59-
flags.StringVarP(&opt.imagePrefix, "image-prefix", "", "linuxsuren", "The prefix for the image address")
60-
return
43+
opt := &extensionOption{
44+
ociDownloader: ociDownloader,
45+
}
46+
c = &cobra.Command{
47+
Use: "extension",
48+
Short: "Download extension binary files",
49+
Long: "Download the store extension files",
50+
Args: cobra.MinimumNArgs(1),
51+
RunE: opt.runE,
52+
}
53+
flags := c.Flags()
54+
flags.StringVarP(&opt.output, "output", "", ".", "The target directory")
55+
flags.StringVarP(&opt.tag, "tag", "", "", "The extension image tag, try to find the latest one if this is empty")
56+
flags.StringVarP(&opt.registry, "registry", "", "", "The target extension image registry, supported: docker.io, ghcr.io")
57+
flags.StringVarP(&opt.kind, "kind", "", "store", "The extension kind")
58+
flags.StringVarP(&opt.os, "os", "", runtime.GOOS, "The OS")
59+
flags.StringVarP(&opt.arch, "arch", "", runtime.GOARCH, "The architecture")
60+
flags.DurationVarP(&opt.timeout, "timeout", "", time.Minute, "The timeout of downloading")
61+
flags.StringVarP(&opt.imagePrefix, "image-prefix", "", "linuxsuren", "The prefix for the image address")
62+
return
6163
}
6264

6365
func (o *extensionOption) runE(cmd *cobra.Command, args []string) (err error) {
64-
o.ociDownloader.WithOS(o.os)
65-
o.ociDownloader.WithArch(o.arch)
66-
o.ociDownloader.WithRegistry(o.registry)
67-
o.ociDownloader.WithImagePrefix(o.imagePrefix)
68-
o.ociDownloader.WithTimeout(o.timeout)
69-
o.ociDownloader.WithContext(cmd.Context())
66+
o.ociDownloader.WithOS(o.os)
67+
o.ociDownloader.WithArch(o.arch)
68+
o.ociDownloader.WithRegistry(o.registry)
69+
o.ociDownloader.WithImagePrefix(o.imagePrefix)
70+
o.ociDownloader.WithTimeout(o.timeout)
71+
o.ociDownloader.WithKind(o.kind)
72+
o.ociDownloader.WithContext(cmd.Context())
7073

71-
for _, arg := range args {
72-
var reader io.Reader
73-
if reader, err = o.ociDownloader.Download(arg, o.tag, ""); err != nil {
74-
return
75-
} else if reader == nil {
76-
err = fmt.Errorf("cannot find %s", arg)
77-
return
78-
}
79-
extFile := o.ociDownloader.GetTargetFile()
80-
cmd.Println("found target file", extFile)
74+
for _, arg := range args {
75+
var reader io.Reader
76+
if reader, err = o.ociDownloader.Download(arg, o.tag, ""); err != nil {
77+
return
78+
} else if reader == nil {
79+
err = fmt.Errorf("cannot find %s", arg)
80+
return
81+
}
82+
extFile := o.ociDownloader.GetTargetFile()
83+
cmd.Println("found target file", extFile)
8184

82-
targetFile := filepath.Base(extFile)
83-
if err = downloader.WriteTo(reader, o.output, targetFile); err == nil {
84-
cmd.Println("downloaded", targetFile)
85-
}
86-
}
87-
return
85+
targetFile := filepath.Base(extFile)
86+
if err = downloader.WriteTo(reader, o.output, targetFile); err == nil {
87+
cmd.Println("downloaded", targetFile)
88+
}
89+
}
90+
return
8891
}

pkg/downloader/store.go renamed to pkg/downloader/extension.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2024 API Testing Authors.
2+
Copyright 2024-2025 API Testing Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -25,30 +25,32 @@ import (
2525
"strings"
2626
)
2727

28-
type storeDownloader struct {
28+
type extensionDownloader struct {
2929
OCIDownloader
3030
os, arch string
31+
kind string
3132
extFile string
3233
imagePrefix string
3334
}
3435

3536
func NewStoreDownloader() PlatformAwareOCIDownloader {
36-
ociDownloader := &storeDownloader{
37+
ociDownloader := &extensionDownloader{
3738
OCIDownloader: NewDefaultOCIDownloader(),
3839
}
3940
ociDownloader.WithOS(runtime.GOOS)
4041
ociDownloader.WithArch(runtime.GOARCH)
4142
ociDownloader.WithImagePrefix("linuxsuren")
43+
ociDownloader.WithKind("store")
4244
return ociDownloader
4345
}
4446

45-
func (d *storeDownloader) Download(name, tag, _ string) (reader io.Reader, err error) {
46-
name = strings.TrimPrefix(name, "atest-store-")
47-
d.extFile = fmt.Sprintf("atest-store-%s_%s_%s/atest-store-%s", name, d.os, d.arch, name)
47+
func (d *extensionDownloader) Download(name, tag, _ string) (reader io.Reader, err error) {
48+
name = strings.TrimPrefix(name, fmt.Sprintf("atest-%s-", d.kind))
49+
d.extFile = fmt.Sprintf("atest-%s-%s_%s_%s/atest-%s-%s", d.kind, name, d.os, d.arch, d.kind, name)
4850
if d.os == "windows" {
4951
d.extFile = fmt.Sprintf("%s.exe", d.extFile)
5052
}
51-
image := fmt.Sprintf("%s/atest-ext-store-%s", d.imagePrefix, name)
53+
image := fmt.Sprintf("%s/atest-ext-%s-%s", d.imagePrefix, d.kind, name)
5254
reader, err = d.OCIDownloader.Download(image, tag, d.extFile)
5355
return
5456
}
@@ -64,21 +66,25 @@ func WriteTo(reader io.Reader, dir, file string) (err error) {
6466
return
6567
}
6668

67-
func (d *storeDownloader) GetTargetFile() string {
69+
func (d *extensionDownloader) GetTargetFile() string {
6870
return d.extFile
6971
}
7072

71-
func (d *storeDownloader) WithOS(os string) {
73+
func (d *extensionDownloader) WithOS(os string) {
7274
d.os = os
7375
}
7476

75-
func (d *storeDownloader) WithImagePrefix(imagePrefix string) {
77+
func (d *extensionDownloader) WithImagePrefix(imagePrefix string) {
7678
d.imagePrefix = imagePrefix
7779
}
7880

79-
func (d *storeDownloader) WithArch(arch string) {
81+
func (d *extensionDownloader) WithArch(arch string) {
8082
d.arch = arch
8183
if d.arch == "amd64" {
8284
d.arch = "amd64_v1"
8385
}
8486
}
87+
88+
func (d *extensionDownloader) WithKind(kind string) {
89+
d.kind = kind
90+
}

pkg/downloader/oci.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type PlatformAwareOCIDownloader interface {
4545
WithOS(string)
4646
WithArch(string)
4747
GetTargetFile() string
48+
WithKind(string)
4849
WithImagePrefix(string)
4950
}
5051

pkg/server/store_ext_manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ var ErrDownloadNotSupport = errors.New("no support")
176176

177177
type nonDownloader struct{}
178178

179+
var _ downloader.PlatformAwareOCIDownloader = &nonDownloader{}
180+
179181
func (n *nonDownloader) WithBasicAuth(username string, password string) {
180182
// Do nothing because this is an empty implementation
181183
}
@@ -197,6 +199,10 @@ func (n *nonDownloader) WithRegistry(string) {
197199
// Do nothing because this is an empty implementation
198200
}
199201

202+
func (n *nonDownloader) WithKind(string) {
203+
// Do nothing because this is an empty implementation
204+
}
205+
200206
func (n *nonDownloader) WithImagePrefix(imagePrefix string) {
201207
// Do nothing because this is an empty implementation
202208
}

0 commit comments

Comments
 (0)