From 683e27106906dec962c53d705c28b1f7a55fcc6b Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 20 Mar 2018 17:46:24 -0700 Subject: [PATCH] common commands --- command/run.go | 1 + command/stop.go | 1 + main.go | 147 ++++++++++++++++++++++++++++++++---------------- 3 files changed, 101 insertions(+), 48 deletions(-) diff --git a/command/run.go b/command/run.go index 33d9f266f..6fb19b2bc 100644 --- a/command/run.go +++ b/command/run.go @@ -27,6 +27,7 @@ type JobRunCommand struct { func (c *JobRunCommand) Help() string { helpText := ` Usage: nomad job run [options] +Alias: nomad run Starts running a new job or updates an existing job using the specification located at . This is the main command diff --git a/command/stop.go b/command/stop.go index cd86abb4f..b0da67d8b 100644 --- a/command/stop.go +++ b/command/stop.go @@ -15,6 +15,7 @@ type JobStopCommand struct { func (c *JobStopCommand) Help() string { helpText := ` Usage: nomad job stop [options] +Alias: nomad stop Stop an existing job. This command is used to signal allocations to shut down for the given job ID. Upon successful deregistration, diff --git a/main.go b/main.go index b73c14e1a..af74edc7d 100644 --- a/main.go +++ b/main.go @@ -3,15 +3,56 @@ package main import ( "bytes" "fmt" + "io" "os" "sort" "strings" + "text/tabwriter" "github.com/hashicorp/nomad/version" "github.com/mitchellh/cli" "github.com/sean-/seed" ) +var ( + // Hidden hides the commands from both help and autocomplete. Commands that + // users should not be running should be placed here, versus hiding + // subcommands from the main help, which should be filtered out of the + // commands above. + hidden = []string{ + "alloc-status", + "check", + "client-config", + "eval-status", + "executor", + "fs", + "init", + "inspect", + "keygen", + "keyring", + "logs", + "node-drain", + "node-status", + "plan", + "server-force-leave", + "server-join", + "server-members", + "syslog", + "validate", + } + + // Common commands are grouped separately to call them out to operators. + commonCommands = []string{ + "run", + "stop", + "status", + "alloc", + "job", + "node", + "agent", + } +) + func init() { seed.Init() } @@ -28,57 +69,20 @@ func RunCustom(args []string, commands map[string]cli.CommandFactory) int { // Build the commands to include in the help now. commandsInclude := make([]string, 0, len(commands)) for k := range commands { - switch k { - case "deployment list", "deployment status", "deployment pause", - "deployment resume", "deployment fail", "deployment promote": - case "fs ls", "fs cat", "fs stat": - case "job deployments", "job dispatch", "job history", "job promote", "job revert": - case "namespace list", "namespace delete", "namespace apply", "namespace inspect", "namespace status": - case "quota list", "quota delete", "quota apply", "quota status", "quota inspect", "quota init": - case "operator raft", "operator raft list-peers", "operator raft remove-peer": - case "acl policy", "acl policy apply", "acl token", "acl token create": - case "node-drain", "node-status": - default: - commandsInclude = append(commandsInclude, k) - } - } - - // Hidden hides the commands from both help and autocomplete. Commands that - // users should not be running should be placed here, versus hiding - // subcommands from the main help, which should be filtered out of the - // commands above. - hidden := []string{ - "check", - "executor", - "syslog", - "alloc-status", - "eval-status", - "node-drain", - "node-status", - "validate", - "plan", - "init", - "inspect", - "run", - "stop", - "keygen", - "keyring", - "server-force-leave", - "server-join", - "server-members", - "fs", - "logs", - "client-config", + commandsInclude = append(commandsInclude, k) } cli := &cli.CLI{ - Name: "nomad", - Version: version.GetVersion().FullVersionNumber(true), - Args: args, - Commands: commands, - Autocomplete: true, - HiddenCommands: hidden, - HelpFunc: cli.FilteredHelpFunc(commandsInclude, helpFunc), + Name: "nomad", + Version: version.GetVersion().FullVersionNumber(true), + Args: args, + Commands: commands, + HiddenCommands: hidden, + Autocomplete: true, + AutocompleteNoDefaultFlags: true, + HelpFunc: groupedHelpFunc( + cli.BasicHelpFunc("nomad"), + ), } exitCode, err := cli.Run() @@ -131,3 +135,50 @@ func helpFunc(commands map[string]cli.CommandFactory) string { return buf.String() } + +func groupedHelpFunc(f cli.HelpFunc) cli.HelpFunc { + return func(commands map[string]cli.CommandFactory) string { + var b bytes.Buffer + tw := tabwriter.NewWriter(&b, 0, 2, 6, ' ', 0) + + fmt.Fprintf(tw, "Usage: nomad [-version] [-help] [-autocomplete-(un)install] [args]\n\n") + fmt.Fprintf(tw, "Common commands:\n") + for _, v := range commonCommands { + printCommand(tw, v, commands[v]) + } + + otherCommands := make([]string, 0, len(commands)) + for k := range commands { + found := false + for _, v := range commonCommands { + if k == v { + found = true + break + } + } + + if !found { + otherCommands = append(otherCommands, k) + } + } + sort.Strings(otherCommands) + + fmt.Fprintf(tw, "\n") + fmt.Fprintf(tw, "Other commands:\n") + for _, v := range otherCommands { + printCommand(tw, v, commands[v]) + } + + tw.Flush() + + return strings.TrimSpace(b.String()) + } +} + +func printCommand(w io.Writer, name string, cmdFn cli.CommandFactory) { + cmd, err := cmdFn() + if err != nil { + panic(fmt.Sprintf("failed to load %q command: %s", name, err)) + } + fmt.Fprintf(w, " %s\t%s\n", name, cmd.Synopsis()) +}