|
| 1 | +/* |
| 2 | +Copyright 2024 API Testing Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "path/filepath" |
| 23 | + "runtime" |
| 24 | + |
| 25 | + "github.com/linuxsuren/api-testing/pkg/downloader" |
| 26 | + "github.com/spf13/cobra" |
| 27 | +) |
| 28 | + |
| 29 | +type extensionOption struct { |
| 30 | + ociDownloader downloader.PlatformAwareOCIDownloader |
| 31 | + output string |
| 32 | + registry string |
| 33 | + tag string |
| 34 | + os string |
| 35 | + arch string |
| 36 | +} |
| 37 | + |
| 38 | +func createExtensionCommand(ociDownloader downloader.PlatformAwareOCIDownloader) (c *cobra.Command) { |
| 39 | + opt := &extensionOption{ |
| 40 | + ociDownloader: ociDownloader, |
| 41 | + } |
| 42 | + c = &cobra.Command{ |
| 43 | + Use: "extension", |
| 44 | + Short: "Download extension binary files", |
| 45 | + Long: "Download the store extension files", |
| 46 | + Args: cobra.MinimumNArgs(1), |
| 47 | + RunE: opt.runE, |
| 48 | + } |
| 49 | + flags := c.Flags() |
| 50 | + flags.StringVarP(&opt.output, "output", "", ".", "The target directory") |
| 51 | + flags.StringVarP(&opt.tag, "tag", "", "", "The extension image tag, try to find the latest one if this is empty") |
| 52 | + flags.StringVarP(&opt.registry, "registry", "", "", "The target extension image registry, supported: docker.io, ghcr.io") |
| 53 | + flags.StringVarP(&opt.os, "os", "", runtime.GOOS, "The OS") |
| 54 | + flags.StringVarP(&opt.arch, "arch", "", runtime.GOARCH, "The architecture") |
| 55 | + return |
| 56 | +} |
| 57 | + |
| 58 | +func (o *extensionOption) runE(cmd *cobra.Command, args []string) (err error) { |
| 59 | + o.ociDownloader.WithOS(o.os) |
| 60 | + o.ociDownloader.WithArch(o.arch) |
| 61 | + |
| 62 | + for _, arg := range args { |
| 63 | + var reader io.Reader |
| 64 | + if reader, err = o.ociDownloader.Download(arg, o.tag, ""); err != nil { |
| 65 | + return |
| 66 | + } else if reader == nil { |
| 67 | + err = fmt.Errorf("cannot find %s", arg) |
| 68 | + return |
| 69 | + } |
| 70 | + extFile := o.ociDownloader.GetTargetFile() |
| 71 | + cmd.Println("found target file", extFile) |
| 72 | + |
| 73 | + targetFile := filepath.Base(extFile) |
| 74 | + if err = downloader.WriteTo(reader, o.output, targetFile); err == nil { |
| 75 | + cmd.Println("downloaded", targetFile) |
| 76 | + } |
| 77 | + } |
| 78 | + return |
| 79 | +} |
0 commit comments