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