/
updated_commands.go
72 lines (65 loc) · 2.15 KB
/
updated_commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package marketplace
import (
"context"
"fmt"
"reflect"
"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-sdk-go/api/marketplace/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/scaleway-sdk-go/strcase"
)
func updateCommands(commands *core.Commands) {
updateMarketplaceListImages(commands.MustFind("marketplace", "image", "list"))
updateMarketplaceGetImage(commands.MustFind("marketplace", "image", "get"))
}
// updateMarketplaceListImages is the custom Run for scw marketplace list images
// TODO: remove when [APIGW-1959] is done
func updateMarketplaceListImages(c *core.Command) {
c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
client := core.ExtractClient(ctx)
req := argsI.(*marketplace.ListImagesRequest)
api := marketplace.NewAPI(client)
req.PerPage = scw.Uint32Ptr(100)
imagesResponse, err := api.ListImages(req, scw.WithAllPages())
if err != nil {
return nil, err
}
return imagesResponse.Images, nil
}
}
// TODO : use generated command with label 'param'
// marketplaceGetImagesCustom is the custom command for scw marketplace get image
func updateMarketplaceGetImage(c *core.Command) {
type getImagesArgs struct {
Label string
}
c.ArgsType = reflect.TypeOf(getImagesArgs{})
c.ArgSpecs = core.ArgSpecs{
{
Name: "label",
Short: ``,
Required: true,
},
}
c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
args := argsI.(*getImagesArgs)
req := &marketplace.ListImagesRequest{}
req.PerPage = scw.Uint32Ptr(100)
client := core.ExtractClient(ctx)
api := marketplace.NewAPI(client)
resp, err := api.ListImages(req, scw.WithAllPages())
if err != nil {
return nil, err
}
return getImageByLabel(resp.Images, args.Label)
}
}
// getImageByLabel returns a single Image from a slice based on the label of the Image
func getImageByLabel(images []*marketplace.Image, label string) (*marketplace.Image, error) {
for _, image := range images {
if strcase.ToBashArg(image.Label) == strcase.ToBashArg(label) {
return image, nil
}
}
return nil, fmt.Errorf("image not found: no image with label '%v'", label)
}