-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support of '_completion' command (#45)
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |