github.com/renier/terraform@v0.7.8-0.20161024133817-eb8a9ef5471a/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 is used to output the help for Terraform. 14 func helpFunc(commands map[string]cli.CommandFactory) string { 15 // Determine the maximum key length, and classify based on type 16 porcelain := make(map[string]cli.CommandFactory) 17 plumbing := make(map[string]cli.CommandFactory) 18 maxKeyLen := 0 19 for key, f := range commands { 20 if len(key) > maxKeyLen { 21 maxKeyLen = len(key) 22 } 23 24 if _, ok := PlumbingCommands[key]; ok { 25 plumbing[key] = f 26 } else { 27 porcelain[key] = f 28 } 29 } 30 31 helpText := fmt.Sprintf(` 32 Usage: terraform [--version] [--help] <command> [args] 33 34 The available commands for execution are listed below. 35 The most common, useful commands are shown first, followed by 36 less common or more advanced commands. If you're just getting 37 started with Terraform, stick with the common commands. For the 38 other commands, please read the help and docs before usage. 39 40 Common commands: 41 %s 42 All other commands: 43 %s 44 `, listCommands(porcelain, maxKeyLen), listCommands(plumbing, maxKeyLen)) 45 46 return strings.TrimSpace(helpText) 47 } 48 49 // listCommands just lists the commands in the map with the 50 // given maximum key length. 51 func listCommands(commands map[string]cli.CommandFactory, maxKeyLen int) string { 52 var buf bytes.Buffer 53 54 // Get the list of keys so we can sort them, and also get the maximum 55 // key length so they can be aligned properly. 56 keys := make([]string, 0, len(commands)) 57 for key, _ := range commands { 58 // This is an internal command that users should never call directly so 59 // we will hide it from the command listing. 60 if key == "internal-plugin" { 61 continue 62 } 63 keys = append(keys, key) 64 } 65 sort.Strings(keys) 66 67 for _, key := range keys { 68 commandFunc, ok := commands[key] 69 if !ok { 70 // This should never happen since we JUST built the list of 71 // keys. 72 panic("command not found: " + key) 73 } 74 75 command, err := commandFunc() 76 if err != nil { 77 log.Printf("[ERR] cli: Command '%s' failed to load: %s", 78 key, err) 79 continue 80 } 81 82 key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key))) 83 buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis())) 84 } 85 86 return buf.String() 87 }