diff options
author | Alex Pilon <apilon@hashicorp.com> | 2019-02-22 18:24:37 -0500 |
---|---|---|
committer | Alex Pilon <apilon@hashicorp.com> | 2019-02-22 18:24:37 -0500 |
commit | 15c0b25d011f37e7c20aeca9eaf461f78285b8d9 (patch) | |
tree | 255c250a5c9d4801c74092d33b7337d8c14438ff /vendor/github.com/mitchellh/cli/help.go | |
parent | 07971ca38143c5faf951d152fba370ddcbe26ad5 (diff) | |
download | terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.tar.gz terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.tar.zst terraform-provider-statuscake-15c0b25d011f37e7c20aeca9eaf461f78285b8d9.zip |
deps: github.com/hashicorp/terraform@sdk-v0.11-with-go-modules
Updated via: go get github.com/hashicorp/terraform@sdk-v0.11-with-go-modules and go mod tidy
Diffstat (limited to 'vendor/github.com/mitchellh/cli/help.go')
-rw-r--r-- | vendor/github.com/mitchellh/cli/help.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/vendor/github.com/mitchellh/cli/help.go b/vendor/github.com/mitchellh/cli/help.go new file mode 100644 index 0000000..f5ca58f --- /dev/null +++ b/vendor/github.com/mitchellh/cli/help.go | |||
@@ -0,0 +1,79 @@ | |||
1 | package cli | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "fmt" | ||
6 | "log" | ||
7 | "sort" | ||
8 | "strings" | ||
9 | ) | ||
10 | |||
11 | // HelpFunc is the type of the function that is responsible for generating | ||
12 | // the help output when the CLI must show the general help text. | ||
13 | type HelpFunc func(map[string]CommandFactory) string | ||
14 | |||
15 | // BasicHelpFunc generates some basic help output that is usually good enough | ||
16 | // for most CLI applications. | ||
17 | func BasicHelpFunc(app string) HelpFunc { | ||
18 | return func(commands map[string]CommandFactory) string { | ||
19 | var buf bytes.Buffer | ||
20 | buf.WriteString(fmt.Sprintf( | ||
21 | "Usage: %s [--version] [--help] <command> [<args>]\n\n", | ||
22 | app)) | ||
23 | buf.WriteString("Available commands are:\n") | ||
24 | |||
25 | // Get the list of keys so we can sort them, and also get the maximum | ||
26 | // key length so they can be aligned properly. | ||
27 | keys := make([]string, 0, len(commands)) | ||
28 | maxKeyLen := 0 | ||
29 | for key := range commands { | ||
30 | if len(key) > maxKeyLen { | ||
31 | maxKeyLen = len(key) | ||
32 | } | ||
33 | |||
34 | keys = append(keys, key) | ||
35 | } | ||
36 | sort.Strings(keys) | ||
37 | |||
38 | for _, key := range keys { | ||
39 | commandFunc, ok := commands[key] | ||
40 | if !ok { | ||
41 | // This should never happen since we JUST built the list of | ||
42 | // keys. | ||
43 | panic("command not found: " + key) | ||
44 | } | ||
45 | |||
46 | command, err := commandFunc() | ||
47 | if err != nil { | ||
48 | log.Printf("[ERR] cli: Command '%s' failed to load: %s", | ||
49 | key, err) | ||
50 | continue | ||
51 | } | ||
52 | |||
53 | key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key))) | ||
54 | buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis())) | ||
55 | } | ||
56 | |||
57 | return buf.String() | ||
58 | } | ||
59 | } | ||
60 | |||
61 | // FilteredHelpFunc will filter the commands to only include the keys | ||
62 | // in the include parameter. | ||
63 | func FilteredHelpFunc(include []string, f HelpFunc) HelpFunc { | ||
64 | return func(commands map[string]CommandFactory) string { | ||
65 | set := make(map[string]struct{}) | ||
66 | for _, k := range include { | ||
67 | set[k] = struct{}{} | ||
68 | } | ||
69 | |||
70 | filtered := make(map[string]CommandFactory) | ||
71 | for k, f := range commands { | ||
72 | if _, ok := set[k]; ok { | ||
73 | filtered[k] = f | ||
74 | } | ||
75 | } | ||
76 | |||
77 | return f(filtered) | ||
78 | } | ||
79 | } | ||