Skip to content

Commit

Permalink
Merge pull request #137 from moul/fix-134-filters
Browse files Browse the repository at this point in the history
Support of 'scw {images,ps} --filter' (#134)
  • Loading branch information
moul committed Aug 26, 2015
2 parents e81c6db + 672c193 commit eab1d54
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 82 deletions.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,29 @@ List images.
Options:

-a, --all=false Show all iamges
-f, --filter="" Filter output based on conditions provided
-h, --help=false Print usage
--no-trunc=false Don't truncate output
-q, --quiet=false Only show numeric IDs

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
```


Expand Down Expand Up @@ -544,11 +564,31 @@ List servers. By default, only running servers are displayed.
Options:

-a, --all=false Show all servers. Only running servers are shown by default
-f, --filter="" Filter output based on conditions provided
-h, --help=false Print usage
-l, --latest=false Show only the latest created server, include non-running ones
-n=0 Show n last created servers, include non-running ones
--no-trunc=false Don't truncate output
-q, --quiet=false Only display numeric IDs

Examples:

$ scw ps
$ scw ps -a
$ scw ps -l
$ scw ps -n=10
$ scw ps -q
$ scw ps --no-trunc
$ scw ps -f state=booted
$ scw ps -f state=running
$ scw ps -f state=stopped
$ scw ps -f ip=212.47.229.26
$ scw ps -f tags=prod
$ scw ps -f tags=boot=live
$ scw ps -f image=docker
$ scw ps -f image=alpine
$ scw ps -f image=UUIDOFIMAGE
$ scw ps -f "state=booted image=docker tags=prod"
```


Expand Down Expand Up @@ -1091,10 +1131,12 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
* `scw -D login` displays a fake password
* Support --skip-ssh-key `scw login` ([#129](https://github.com/scaleway/scaleway-cli/issues/129))
* 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))
* Support of `scw images --filter` option *(type, organization, name, public)* ([#134](https://github.com/scaleway/scaleway-cli/issues/134))
* 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))
* Syncing cache to disk after server creation when running `scw run` in a non-detached mode
* Bump to Golang 1.5
* Support --tmp-ssh-key `scw {run,create}` option ([#99](https://github.com/scaleway/scaleway-cli/issues/99))
* Support -f `scw run --rm` option ([#117](https://github.com/scaleway/scaleway-cli/issues/117))
* Support of `scw run --rm` option ([#117](https://github.com/scaleway/scaleway-cli/issues/117))
* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))
* Upload local ssh key to scaleway account on `scw login` ([#100](https://github.com/scaleway/scaleway-cli/issues/100))
* Add a 'running indicator' for `scw run`, can be disabled with the new flag `--quiet`
Expand Down
1 change: 1 addition & 0 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ScalewayImageInterface struct {
VirtualSize float64
Public bool
Type string
Organization string
}

// ResolveGateway tries to resolve a server public ip address, else returns the input string, i.e. IPv4, hostname
Expand Down
50 changes: 45 additions & 5 deletions pkg/cli/cmd_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,52 @@

package cli

import "github.com/scaleway/scaleway-cli/pkg/commands"
import (
"strings"

"github.com/scaleway/scaleway-cli/pkg/commands"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
)

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 iamges")
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 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 {
Expand All @@ -38,6 +63,21 @@ func runImages(cmd *Command, rawArgs []string) error {
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)
Expand Down
54 changes: 47 additions & 7 deletions pkg/cli/cmd_ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,36 @@

package cli

import "github.com/scaleway/scaleway-cli/pkg/commands"
import (
"strings"

"github.com/scaleway/scaleway-cli/pkg/commands"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
)

var cmdPs = &Command{
Exec: runPs,
UsageLine: "ps [OPTIONS]",
Description: "List servers",
Help: "List servers. By default, only running servers are displayed.",
Examples: `
$ scw ps
$ scw ps -a
$ scw ps -l
$ scw ps -n=10
$ scw ps -q
$ scw ps --no-trunc
$ scw ps -f state=booted
$ scw ps -f state=running
$ scw ps -f state=stopped
$ scw ps -f ip=212.47.229.26
$ scw ps -f tags=prod
$ scw ps -f tags=boot=live
$ scw ps -f image=docker
$ scw ps -f image=alpine
$ scw ps -f image=UUIDOFIMAGE
$ scw ps -f "state=booted image=docker tags=prod"
`,
}

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

// Flags
var psA bool // -a flag
var psL bool // -l flag
var psQ bool // -q flag
var psNoTrunc bool // -no-trunc flag
var psN int // -n flag
var psHelp bool // -h, --help flag
var psA bool // -a flag
var psL bool // -l flag
var psQ bool // -q flag
var psNoTrunc bool // -no-trunc flag
var psN int // -n flag
var psHelp bool // -h, --help flag
var psFilters string // -f, --filter flag

func runPs(cmd *Command, rawArgs []string) error {
if psHelp {
Expand All @@ -44,6 +69,21 @@ func runPs(cmd *Command, rawArgs []string) error {
Quiet: psQ,
NoTrunc: psNoTrunc,
NLast: psN,
Filters: make(map[string]string, 0),
}
if psFilters != "" {
for _, filter := range strings.Split(psFilters, " ") {
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.RunPs(ctx, args)
Expand Down
Loading

0 comments on commit eab1d54

Please sign in to comment.