-
Notifications
You must be signed in to change notification settings - Fork 251
/
help.go
95 lines (77 loc) · 2.28 KB
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package configCmd
import (
"encoding/json"
"fmt"
"sort"
"github.com/spf13/cobra"
"github.com/taskcluster/taskcluster/v49/clients/client-shell/config"
)
func init() {
Command.AddCommand(&cobra.Command{
Use: "help [<option>]",
Short: "Get help for Taskcluster's configuration options.",
RunE: cmdHelp,
})
}
func cmdHelp(cmd *cobra.Command, args []string) error {
// default, print help for all commands
if len(args) == 0 {
printHelp(cmd)
return nil
}
// otherwise, print for one specific option
err := printOptionHelp(cmd, args[0])
return err
}
// printOptionHelp shows help for specific option
func printOptionHelp(cmd *cobra.Command, key string) error {
command, option, definition, value, err := getOptionFromKey(key)
if err != nil {
return err
}
defaultValue := definition.Default
if definition.Parse {
if s, err := json.MarshalIndent(defaultValue, " ", " "); err == nil {
defaultValue = "\n" + string(s)
} else {
defaultValue = fmt.Sprintf("%#v", defaultValue)
}
if s, err := json.MarshalIndent(value, " ", " "); err == nil {
value = "\n" + string(s)
} else {
value = fmt.Sprintf("%#v", value)
}
}
fmt.Fprintf(cmd.OutOrStdout(), "Key: %s.%s\n", command, option)
fmt.Fprintln(cmd.OutOrStdout(), definition.Description)
fmt.Fprintf(cmd.OutOrStdout(), "Value: %s\n", value)
fmt.Fprintf(cmd.OutOrStdout(), "Default: %s\n", defaultValue)
return nil
}
// printHelp as a list of all possible configuration options
func printHelp(cmd *cobra.Command) {
commands := []string{}
maxLength := 0
for command, options := range config.OptionsDefinitions {
// this is for the list of commands
commands = append(commands, command)
// this is for the maximum name length for a key
for option := range options {
if len(command)+len(option) > maxLength {
maxLength = len(command) + len(option)
}
}
}
sort.Strings(commands)
fmt.Fprintln(cmd.OutOrStdout(), "Configuration options:")
for _, command := range commands {
options := config.OptionsDefinitions[command]
for option, definition := range options {
fmt.Fprintf(cmd.OutOrStdout(), " %s %s\n", pad(command+"."+option+":", maxLength+2), definition.Description)
}
// Add empty line between sections
if len(options) > 0 {
fmt.Fprint(cmd.OutOrStdout(), "\n")
}
}
}