github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/main.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "sort" 8 "strings" 9 10 "github.com/hashicorp/nomad/version" 11 "github.com/mitchellh/cli" 12 "github.com/sean-/seed" 13 ) 14 15 func init() { 16 seed.Init() 17 } 18 19 func main() { 20 os.Exit(Run(os.Args[1:])) 21 } 22 23 func Run(args []string) int { 24 return RunCustom(args, Commands(nil)) 25 } 26 27 func RunCustom(args []string, commands map[string]cli.CommandFactory) int { 28 // Build the commands to include in the help now. 29 commandsInclude := make([]string, 0, len(commands)) 30 for k := range commands { 31 switch k { 32 case "deployment list", "deployment status", "deployment pause", 33 "deployment resume", "deployment fail", "deployment promote": 34 case "fs ls", "fs cat", "fs stat": 35 case "job deployments", "job dispatch", "job history", "job promote", "job revert": 36 case "namespace list", "namespace delete", "namespace apply", "namespace inspect", "namespace status": 37 case "quota list", "quota delete", "quota apply", "quota status", "quota inspect", "quota init": 38 case "operator raft", "operator raft list-peers", "operator raft remove-peer": 39 case "acl policy", "acl policy apply", "acl token", "acl token create": 40 default: 41 commandsInclude = append(commandsInclude, k) 42 } 43 } 44 45 // Hidden hides the commands from both help and autocomplete. Commands that 46 // users should not be running should be placed here, versus hiding 47 // subcommands from the main help, which should be filtered out of the 48 // commands above. 49 hidden := []string{"check", "executor", "syslog"} 50 51 cli := &cli.CLI{ 52 Name: "nomad", 53 Version: version.GetVersion().FullVersionNumber(true), 54 Args: args, 55 Commands: commands, 56 Autocomplete: true, 57 HiddenCommands: hidden, 58 HelpFunc: cli.FilteredHelpFunc(commandsInclude, helpFunc), 59 } 60 61 exitCode, err := cli.Run() 62 if err != nil { 63 fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error()) 64 return 1 65 } 66 67 return exitCode 68 } 69 70 // helpFunc is a custom help function. At the moment it is essentially a copy of 71 // the cli.BasicHelpFunc that includes flags demonstrating how to use the 72 // autocomplete flags. 73 func helpFunc(commands map[string]cli.CommandFactory) string { 74 var buf bytes.Buffer 75 buf.WriteString("Usage: nomad [-version] [-help] [-autocomplete-(un)install] <command> [<args>]\n\n") 76 buf.WriteString("Available commands are:\n") 77 78 // Get the list of keys so we can sort them, and also get the maximum 79 // key length so they can be aligned properly. 80 keys := make([]string, 0, len(commands)) 81 maxKeyLen := 0 82 for key := range commands { 83 if len(key) > maxKeyLen { 84 maxKeyLen = len(key) 85 } 86 87 keys = append(keys, key) 88 } 89 sort.Strings(keys) 90 91 for _, key := range keys { 92 commandFunc, ok := commands[key] 93 if !ok { 94 // This should never happen since we JUST built the list of 95 // keys. 96 panic("command not found: " + key) 97 } 98 99 command, err := commandFunc() 100 if err != nil { 101 fmt.Fprintf(os.Stderr, "[ERR] cli: Command '%s' failed to load: %s", key, err) 102 continue 103 } 104 105 key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key))) 106 buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis())) 107 } 108 109 return buf.String() 110 }