Skip to content

Commit eab1d54

Browse files
committed
Merge pull request #137 from moul/fix-134-filters
Support of 'scw {images,ps} --filter' (#134)
2 parents e81c6db + 672c193 commit eab1d54

File tree

6 files changed

+317
-82
lines changed

6 files changed

+317
-82
lines changed

README.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,29 @@ List images.
410410
Options:
411411

412412
-a, --all=false Show all iamges
413+
-f, --filter="" Filter output based on conditions provided
413414
-h, --help=false Print usage
414415
--no-trunc=false Don't truncate output
415416
-q, --quiet=false Only show numeric IDs
417+
418+
Examples:
419+
420+
$ scw images
421+
$ scw images -a
422+
$ scw images -q
423+
$ scw images --no-trunc
424+
$ scw images -f organization=me
425+
$ scw images -f organization=official-distribs
426+
$ scw images -f organization=official-apps
427+
$ scw images -f organization=UUIDOFORGANIZATION
428+
$ scw images -f name=ubuntu
429+
$ scw images -f type=image
430+
$ scw images -f type=bootscript
431+
$ scw images -f type=snapshot
432+
$ scw images -f type=volume
433+
$ scw images -f public=true
434+
$ scw images -f public=false
435+
$ scw images -f "organization=me type=volume" -q
416436
```
417437

418438

@@ -544,11 +564,31 @@ List servers. By default, only running servers are displayed.
544564
Options:
545565

546566
-a, --all=false Show all servers. Only running servers are shown by default
567+
-f, --filter="" Filter output based on conditions provided
547568
-h, --help=false Print usage
548569
-l, --latest=false Show only the latest created server, include non-running ones
549570
-n=0 Show n last created servers, include non-running ones
550571
--no-trunc=false Don't truncate output
551572
-q, --quiet=false Only display numeric IDs
573+
574+
Examples:
575+
576+
$ scw ps
577+
$ scw ps -a
578+
$ scw ps -l
579+
$ scw ps -n=10
580+
$ scw ps -q
581+
$ scw ps --no-trunc
582+
$ scw ps -f state=booted
583+
$ scw ps -f state=running
584+
$ scw ps -f state=stopped
585+
$ scw ps -f ip=212.47.229.26
586+
$ scw ps -f tags=prod
587+
$ scw ps -f tags=boot=live
588+
$ scw ps -f image=docker
589+
$ scw ps -f image=alpine
590+
$ scw ps -f image=UUIDOFIMAGE
591+
$ scw ps -f "state=booted image=docker tags=prod"
552592
```
553593

554594

@@ -1091,10 +1131,12 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
10911131
* `scw -D login` displays a fake password
10921132
* Support --skip-ssh-key `scw login` ([#129](https://github.com/scaleway/scaleway-cli/issues/129))
10931133
* Now `scw login` ask your login/password, you can also pass token and organization with -o and -t ([#59](https://github.com/scaleway/scaleway-cli/issues/59))
1134+
* Support of `scw images --filter` option *(type, organization, name, public)* ([#134](https://github.com/scaleway/scaleway-cli/issues/134))
1135+
* Support of `scw {ps,images} --filter` option *(images: type,organization,name,public; ps:state,ip,tags,image)* ([#134](https://github.com/scaleway/scaleway-cli/issues/134))
10941136
* Syncing cache to disk after server creation when running `scw run` in a non-detached mode
10951137
* Bump to Golang 1.5
10961138
* Support --tmp-ssh-key `scw {run,create}` option ([#99](https://github.com/scaleway/scaleway-cli/issues/99))
1097-
* Support -f `scw run --rm` option ([#117](https://github.com/scaleway/scaleway-cli/issues/117))
1139+
* Support of `scw run --rm` option ([#117](https://github.com/scaleway/scaleway-cli/issues/117))
10981140
* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))
10991141
* Upload local ssh key to scaleway account on `scw login` ([#100](https://github.com/scaleway/scaleway-cli/issues/100))
11001142
* Add a 'running indicator' for `scw run`, can be disabled with the new flag `--quiet`

pkg/api/helpers.go

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type ScalewayImageInterface struct {
3737
VirtualSize float64
3838
Public bool
3939
Type string
40+
Organization string
4041
}
4142

4243
// ResolveGateway tries to resolve a server public ip address, else returns the input string, i.e. IPv4, hostname

pkg/cli/cmd_images.go

+45-5
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,52 @@
44

55
package cli
66

7-
import "github.com/scaleway/scaleway-cli/pkg/commands"
7+
import (
8+
"strings"
9+
10+
"github.com/scaleway/scaleway-cli/pkg/commands"
11+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
12+
)
813

914
var cmdImages = &Command{
1015
Exec: runImages,
1116
UsageLine: "images [OPTIONS]",
1217
Description: "List images",
1318
Help: "List images.",
19+
Examples: `
20+
$ scw images
21+
$ scw images -a
22+
$ scw images -q
23+
$ scw images --no-trunc
24+
$ scw images -f organization=me
25+
$ scw images -f organization=official-distribs
26+
$ scw images -f organization=official-apps
27+
$ scw images -f organization=UUIDOFORGANIZATION
28+
$ scw images -f name=ubuntu
29+
$ scw images -f type=image
30+
$ scw images -f type=bootscript
31+
$ scw images -f type=snapshot
32+
$ scw images -f type=volume
33+
$ scw images -f public=true
34+
$ scw images -f public=false
35+
$ scw images -f "organization=me type=volume" -q
36+
`,
1437
}
1538

1639
func init() {
1740
cmdImages.Flag.BoolVar(&imagesA, []string{"a", "-all"}, false, "Show all iamges")
1841
cmdImages.Flag.BoolVar(&imagesNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
1942
cmdImages.Flag.BoolVar(&imagesQ, []string{"q", "-quiet"}, false, "Only show numeric IDs")
2043
cmdImages.Flag.BoolVar(&imagesHelp, []string{"h", "-help"}, false, "Print usage")
44+
cmdImages.Flag.StringVar(&imagesFilters, []string{"f", "-filter"}, "", "Filter output based on conditions provided")
2145
}
2246

2347
// Flags
24-
var imagesA bool // -a flag
25-
var imagesQ bool // -q flag
26-
var imagesNoTrunc bool // -no-trunc flag
27-
var imagesHelp bool // -h, --help flag
48+
var imagesA bool // -a flag
49+
var imagesQ bool // -q flag
50+
var imagesNoTrunc bool // -no-trunc flag
51+
var imagesHelp bool // -h, --help flag
52+
var imagesFilters string // -f, --filters
2853

2954
func runImages(cmd *Command, rawArgs []string) error {
3055
if imagesHelp {
@@ -38,6 +63,21 @@ func runImages(cmd *Command, rawArgs []string) error {
3863
All: imagesA,
3964
Quiet: imagesQ,
4065
NoTrunc: imagesNoTrunc,
66+
Filters: make(map[string]string, 0),
67+
}
68+
if imagesFilters != "" {
69+
for _, filter := range strings.Split(imagesFilters, " ") {
70+
parts := strings.SplitN(filter, "=", 2)
71+
if len(parts) != 2 {
72+
logrus.Warnf("Invalid filter '%s', should be in the form 'key=value'", filter)
73+
continue
74+
}
75+
if _, ok := args.Filters[parts[0]]; ok {
76+
logrus.Warnf("Duplicated filter: %q", parts[0])
77+
} else {
78+
args.Filters[parts[0]] = parts[1]
79+
}
80+
}
4181
}
4282
ctx := cmd.GetContext(rawArgs)
4383
return commands.RunImages(ctx, args)

pkg/cli/cmd_ps.go

+47-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,36 @@
44

55
package cli
66

7-
import "github.com/scaleway/scaleway-cli/pkg/commands"
7+
import (
8+
"strings"
9+
10+
"github.com/scaleway/scaleway-cli/pkg/commands"
11+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
12+
)
813

914
var cmdPs = &Command{
1015
Exec: runPs,
1116
UsageLine: "ps [OPTIONS]",
1217
Description: "List servers",
1318
Help: "List servers. By default, only running servers are displayed.",
19+
Examples: `
20+
$ scw ps
21+
$ scw ps -a
22+
$ scw ps -l
23+
$ scw ps -n=10
24+
$ scw ps -q
25+
$ scw ps --no-trunc
26+
$ scw ps -f state=booted
27+
$ scw ps -f state=running
28+
$ scw ps -f state=stopped
29+
$ scw ps -f ip=212.47.229.26
30+
$ scw ps -f tags=prod
31+
$ scw ps -f tags=boot=live
32+
$ scw ps -f image=docker
33+
$ scw ps -f image=alpine
34+
$ scw ps -f image=UUIDOFIMAGE
35+
$ scw ps -f "state=booted image=docker tags=prod"
36+
`,
1437
}
1538

1639
func init() {
@@ -20,15 +43,17 @@ func init() {
2043
cmdPs.Flag.BoolVar(&psNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
2144
cmdPs.Flag.BoolVar(&psQ, []string{"q", "-quiet"}, false, "Only display numeric IDs")
2245
cmdPs.Flag.BoolVar(&psHelp, []string{"h", "-help"}, false, "Print usage")
46+
cmdPs.Flag.StringVar(&psFilters, []string{"f", "-filter"}, "", "Filter output based on conditions provided")
2347
}
2448

2549
// Flags
26-
var psA bool // -a flag
27-
var psL bool // -l flag
28-
var psQ bool // -q flag
29-
var psNoTrunc bool // -no-trunc flag
30-
var psN int // -n flag
31-
var psHelp bool // -h, --help flag
50+
var psA bool // -a flag
51+
var psL bool // -l flag
52+
var psQ bool // -q flag
53+
var psNoTrunc bool // -no-trunc flag
54+
var psN int // -n flag
55+
var psHelp bool // -h, --help flag
56+
var psFilters string // -f, --filter flag
3257

3358
func runPs(cmd *Command, rawArgs []string) error {
3459
if psHelp {
@@ -44,6 +69,21 @@ func runPs(cmd *Command, rawArgs []string) error {
4469
Quiet: psQ,
4570
NoTrunc: psNoTrunc,
4671
NLast: psN,
72+
Filters: make(map[string]string, 0),
73+
}
74+
if psFilters != "" {
75+
for _, filter := range strings.Split(psFilters, " ") {
76+
parts := strings.SplitN(filter, "=", 2)
77+
if len(parts) != 2 {
78+
logrus.Warnf("Invalid filter '%s', should be in the form 'key=value'", filter)
79+
continue
80+
}
81+
if _, ok := args.Filters[parts[0]]; ok {
82+
logrus.Warnf("Duplicated filter: %q", parts[0])
83+
} else {
84+
args.Filters[parts[0]] = parts[1]
85+
}
86+
}
4787
}
4888
ctx := cmd.GetContext(rawArgs)
4989
return commands.RunPs(ctx, args)

0 commit comments

Comments
 (0)