Skip to content

Commit

Permalink
Support of '_completion' command (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jun 9, 2015
1 parent a522e03 commit 501b8a4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ Development in progress

#### Features

* Support of `_completion` command (shell completion helper) ([#45](https://github.com/scaleway/scaleway-cli/issues/45))
* Returing more resource fields on `scw inspect` ([#50](https://github.com/scaleway/scaleway-cli/issues/50))
* Show public ip address in PORTS field in `scw ps` ([#54](https://github.com/scaleway/scaleway-cli/issues/54))
* Support of `inspect --format` option
Expand Down
1 change: 1 addition & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func (c *Command) GetBootscript(needle string) string {
var commands = []*Command{
cmdAttach,
cmdCommit,
cmdCompletion,
cmdCp,
cmdCreate,
cmdEvents,
Expand Down
71 changes: 71 additions & 0 deletions completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"log"
"sort"
"strings"
)

var cmdCompletion = &Command{
Exec: runCompletion,
UsageLine: "_completion [OPTIONS] CATEGORY",
Description: "Completion helper",
Help: "Completion helper.",
Hidden: true,
Examples: `
$ scw _completion servers-all
$ scw _completion images-all
$ scw _completion snapshots-all
$ scw _completion volumes-all
$ scw _completion bootscripts-all
`,
}

func init() {
cmdCompletion.Flag.BoolVar(&completionHelp, []string{"h", "-help"}, false, "Print usage")
}

// Flags
var completionHelp bool // -h, --help flag

func runCompletion(cmd *Command, args []string) {
if completionHelp {
cmd.PrintUsage()
}
if len(args) != 1 {
cmd.PrintShortUsage()
}

category := args[0]

elements := []string{}

switch category {
case "servers-all":
for identifier, name := range cmd.API.Cache.Servers {
elements = append(elements, identifier, wordify(name))
}
case "images-all":
for identifier, name := range cmd.API.Cache.Images {
elements = append(elements, identifier, wordify(name))
}
case "volumes-all":
for identifier, name := range cmd.API.Cache.Volumes {
elements = append(elements, identifier, wordify(name))
}
case "snapshots-all":
for identifier, name := range cmd.API.Cache.Snapshots {
elements = append(elements, identifier, wordify(name))
}
case "bootscripts-all":
for identifier, name := range cmd.API.Cache.Bootscripts {
elements = append(elements, identifier, wordify(name))
}
default:
log.Fatalf("Unhandled category of completion: %s", category)
}

sort.Strings(elements)
fmt.Println(strings.Join(RemoveDuplicates(elements), "\n"))
}

0 comments on commit 501b8a4

Please sign in to comment.