common commands

This commit is contained in:
Alex Dadgar
2018-03-20 17:46:24 -07:00
parent c7fc6571d8
commit 683e271069
3 changed files with 101 additions and 48 deletions

View File

@@ -27,6 +27,7 @@ type JobRunCommand struct {
func (c *JobRunCommand) Help() string {
helpText := `
Usage: nomad job run [options] <path>
Alias: nomad run
Starts running a new job or updates an existing job using
the specification located at <path>. This is the main command

View File

@@ -15,6 +15,7 @@ type JobStopCommand struct {
func (c *JobStopCommand) Help() string {
helpText := `
Usage: nomad job stop [options] <job>
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,

147
main.go
View File

@@ -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] <command> [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())
}