github.com/kevinklinger/open_terraform@v1.3.6/help.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "log" 7 "sort" 8 "strings" 9 10 "github.com/mitchellh/cli" 11 ) 12 13 // helpFunc is a cli.HelpFunc that can be used to output the help CLI instructions for Terraform. 14 func helpFunc(commands map[string]cli.CommandFactory) string { 15 // Determine the maximum key length, and classify based on type 16 var otherCommands []string 17 maxKeyLen := 0 18 19 for key := range commands { 20 if _, ok := HiddenCommands[key]; ok { 21 // We don't consider hidden commands when deciding the 22 // maximum command length. 23 continue 24 } 25 26 if len(key) > maxKeyLen { 27 maxKeyLen = len(key) 28 } 29 30 isOther := true 31 for _, candidate := range PrimaryCommands { 32 if candidate == key { 33 isOther = false 34 break 35 } 36 } 37 if isOther { 38 otherCommands = append(otherCommands, key) 39 } 40 } 41 sort.Strings(otherCommands) 42 43 // The output produced by this is included in the docs at 44 // website/source/docs/cli/commands/index.html.markdown; if you 45 // change this then consider updating that to match. 46 helpText := fmt.Sprintf(` 47 Usage: terraform [global options] <subcommand> [args] 48 49 The available commands for execution are listed below. 50 The primary workflow commands are given first, followed by 51 less common or more advanced commands. 52 53 Main commands: 54 %s 55 All other commands: 56 %s 57 Global options (use these before the subcommand, if any): 58 -chdir=DIR Switch to a different working directory before executing the 59 given subcommand. 60 -help Show this help output, or the help for a specified subcommand. 61 -version An alias for the "version" subcommand. 62 `, listCommands(commands, PrimaryCommands, maxKeyLen), listCommands(commands, otherCommands, maxKeyLen)) 63 64 return strings.TrimSpace(helpText) 65 } 66 67 // listCommands just lists the commands in the map with the 68 // given maximum key length. 69 func listCommands(allCommands map[string]cli.CommandFactory, order []string, maxKeyLen int) string { 70 var buf bytes.Buffer 71 72 for _, key := range order { 73 commandFunc, ok := allCommands[key] 74 if !ok { 75 // This suggests an inconsistency in the command table definitions 76 // in commands.go . 77 panic("command not found: " + key) 78 } 79 80 command, err := commandFunc() 81 if err != nil { 82 // This would be really weird since there's no good reason for 83 // any of our command factories to fail. 84 log.Printf("[ERR] cli: Command '%s' failed to load: %s", 85 key, err) 86 continue 87 } 88 89 key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key))) 90 buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis())) 91 } 92 93 return buf.String() 94 }