/
cmd_images.go
84 lines (76 loc) · 2.42 KB
/
cmd_images.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
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (C) 2015 Scaleway. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.
package cli
import (
"strings"
"github.com/Sirupsen/logrus"
"github.com/scaleway/scaleway-cli/pkg/commands"
)
var cmdImages = &Command{
Exec: runImages,
UsageLine: "images [OPTIONS]",
Description: "List images",
Help: "List images.",
Examples: `
$ scw images
$ scw images -a
$ scw images -q
$ scw images --no-trunc
$ scw images -f organization=me
$ scw images -f organization=official-distribs
$ scw images -f organization=official-apps
$ scw images -f organization=UUIDOFORGANIZATION
$ scw images -f name=ubuntu
$ scw images -f type=image
$ scw images -f type=bootscript
$ scw images -f type=snapshot
$ scw images -f type=volume
$ scw images -f public=true
$ scw images -f public=false
$ scw images -f "organization=me type=volume" -q
`,
}
func init() {
cmdImages.Flag.BoolVar(&imagesA, []string{"a", "-all"}, false, "Show all images")
cmdImages.Flag.BoolVar(&imagesNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
cmdImages.Flag.BoolVar(&imagesQ, []string{"q", "-quiet"}, false, "Only show numeric IDs")
cmdImages.Flag.BoolVar(&imagesHelp, []string{"h", "-help"}, false, "Print usage")
cmdImages.Flag.StringVar(&imagesFilters, []string{"f", "-filter"}, "", "Filter output based on conditions provided")
}
// Flags
var imagesA bool // -a flag
var imagesQ bool // -q flag
var imagesNoTrunc bool // -no-trunc flag
var imagesHelp bool // -h, --help flag
var imagesFilters string // -f, --filters
func runImages(cmd *Command, rawArgs []string) error {
if imagesHelp {
return cmd.PrintUsage()
}
if len(rawArgs) != 0 {
return cmd.PrintShortUsage()
}
args := commands.ImagesArgs{
All: imagesA,
Quiet: imagesQ,
NoTrunc: imagesNoTrunc,
Filters: make(map[string]string, 0),
}
if imagesFilters != "" {
for _, filter := range strings.Split(imagesFilters, " ") {
parts := strings.SplitN(filter, "=", 2)
if len(parts) != 2 {
logrus.Warnf("Invalid filter '%s', should be in the form 'key=value'", filter)
continue
}
if _, ok := args.Filters[parts[0]]; ok {
logrus.Warnf("Duplicated filter: %q", parts[0])
} else {
args.Filters[parts[0]] = parts[1]
}
}
}
ctx := cmd.GetContext(rawArgs)
return commands.RunImages(ctx, args)
}