]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - vendor/github.com/mitchellh/cli/help.go
Merge branch 'fix_read_test' of github.com:alexandreFre/terraform-provider-statuscake
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / mitchellh / cli / help.go
CommitLineData
15c0b25d
AP
1package cli
2
3import (
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.
13type HelpFunc func(map[string]CommandFactory) string
14
15// BasicHelpFunc generates some basic help output that is usually good enough
16// for most CLI applications.
17func 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.
63func 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}