2015-06-01 11:46:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-25 22:42:22 +00:00
|
|
|
"bytes"
|
2015-06-01 11:46:21 +00:00
|
|
|
"fmt"
|
2018-03-21 00:46:24 +00:00
|
|
|
"io"
|
2015-06-01 11:46:21 +00:00
|
|
|
"os"
|
2017-07-25 22:42:22 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-03-21 00:46:24 +00:00
|
|
|
"text/tabwriter"
|
2015-06-01 11:46:21 +00:00
|
|
|
|
2017-08-16 22:42:15 +00:00
|
|
|
"github.com/hashicorp/nomad/version"
|
2015-06-01 11:46:21 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2017-02-09 00:07:33 +00:00
|
|
|
"github.com/sean-/seed"
|
2015-06-01 11:46:21 +00:00
|
|
|
)
|
|
|
|
|
2018-03-21 00:46:24 +00:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-05-03 07:28:23 +00:00
|
|
|
func init() {
|
2017-02-09 00:07:33 +00:00
|
|
|
seed.Init()
|
2016-05-03 07:28:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 11:46:21 +00:00
|
|
|
func main() {
|
|
|
|
os.Exit(Run(os.Args[1:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Run(args []string) int {
|
|
|
|
return RunCustom(args, Commands(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunCustom(args []string, commands map[string]cli.CommandFactory) int {
|
2015-09-20 01:47:04 +00:00
|
|
|
// Build the commands to include in the help now.
|
|
|
|
commandsInclude := make([]string, 0, len(commands))
|
2017-09-26 22:26:33 +00:00
|
|
|
for k := range commands {
|
2018-03-21 00:46:24 +00:00
|
|
|
commandsInclude = append(commandsInclude, k)
|
2018-03-21 00:37:28 +00:00
|
|
|
}
|
2017-09-08 18:22:42 +00:00
|
|
|
|
2015-06-01 11:46:21 +00:00
|
|
|
cli := &cli.CLI{
|
2018-03-21 00:46:24 +00:00
|
|
|
Name: "nomad",
|
|
|
|
Version: version.GetVersion().FullVersionNumber(true),
|
|
|
|
Args: args,
|
|
|
|
Commands: commands,
|
|
|
|
HiddenCommands: hidden,
|
|
|
|
Autocomplete: true,
|
|
|
|
AutocompleteNoDefaultFlags: true,
|
|
|
|
HelpFunc: groupedHelpFunc(
|
|
|
|
cli.BasicHelpFunc("nomad"),
|
|
|
|
),
|
2015-06-01 11:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exitCode, err := cli.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2015-10-28 02:39:19 +00:00
|
|
|
|
2015-06-01 11:46:21 +00:00
|
|
|
return exitCode
|
|
|
|
}
|
2017-07-25 22:42:22 +00:00
|
|
|
|
|
|
|
// helpFunc is a custom help function. At the moment it is essentially a copy of
|
|
|
|
// the cli.BasicHelpFunc that includes flags demonstrating how to use the
|
|
|
|
// autocomplete flags.
|
|
|
|
func helpFunc(commands map[string]cli.CommandFactory) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("Usage: nomad [-version] [-help] [-autocomplete-(un)install] <command> [<args>]\n\n")
|
|
|
|
buf.WriteString("Available commands are:\n")
|
|
|
|
|
|
|
|
// Get the list of keys so we can sort them, and also get the maximum
|
|
|
|
// key length so they can be aligned properly.
|
|
|
|
keys := make([]string, 0, len(commands))
|
|
|
|
maxKeyLen := 0
|
|
|
|
for key := range commands {
|
|
|
|
if len(key) > maxKeyLen {
|
|
|
|
maxKeyLen = len(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
commandFunc, ok := commands[key]
|
|
|
|
if !ok {
|
|
|
|
// This should never happen since we JUST built the list of
|
|
|
|
// keys.
|
|
|
|
panic("command not found: " + key)
|
|
|
|
}
|
|
|
|
|
|
|
|
command, err := commandFunc()
|
|
|
|
if err != nil {
|
2017-07-26 21:53:08 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "[ERR] cli: Command '%s' failed to load: %s", key, err)
|
2017-07-25 22:42:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key)))
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
2018-03-21 00:46:24 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|