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
|
|
|
|
2019-09-18 03:55:07 +00:00
|
|
|
// These packages have init() funcs which check os.Args and drop directly
|
2019-09-18 13:11:08 +00:00
|
|
|
// into their command logic. This is because they are run as separate
|
2019-09-18 03:55:07 +00:00
|
|
|
// processes along side of a task. By early importing them we can avoid
|
2022-10-11 17:23:03 +00:00
|
|
|
// additional code being imported and thus reserving memory.
|
2022-12-07 22:02:25 +00:00
|
|
|
_ "github.com/hashicorp/nomad/client/allocrunner/taskrunner/getter"
|
2019-09-18 03:55:07 +00:00
|
|
|
_ "github.com/hashicorp/nomad/client/logmon"
|
|
|
|
_ "github.com/hashicorp/nomad/drivers/docker/docklog"
|
|
|
|
_ "github.com/hashicorp/nomad/drivers/shared/executor"
|
2022-10-11 17:23:03 +00:00
|
|
|
|
|
|
|
// Don't move any other code imports above the import block above!
|
|
|
|
"github.com/hashicorp/nomad/command"
|
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"
|
|
|
|
)
|
|
|
|
|
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",
|
2020-08-11 19:39:44 +00:00
|
|
|
"debug",
|
2018-03-21 00:46:24 +00:00
|
|
|
"eval-status",
|
|
|
|
"executor",
|
2018-09-24 18:37:45 +00:00
|
|
|
"logmon",
|
2018-03-21 00:46:24 +00:00
|
|
|
"node-drain",
|
|
|
|
"node-status",
|
|
|
|
"server-force-leave",
|
|
|
|
"server-join",
|
|
|
|
"server-members",
|
|
|
|
"syslog",
|
2019-09-18 03:55:07 +00:00
|
|
|
"docker_logger",
|
2020-08-21 21:16:42 +00:00
|
|
|
"operator raft _info",
|
|
|
|
"operator raft _logs",
|
|
|
|
"operator raft _state",
|
2021-11-04 14:16:12 +00:00
|
|
|
"operator snapshot _state",
|
2018-03-29 18:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// aliases is the list of aliases we want users to be aware of. We hide
|
|
|
|
// these form the help output but autocomplete them.
|
|
|
|
aliases = []string{
|
|
|
|
"fs",
|
|
|
|
"init",
|
|
|
|
"inspect",
|
|
|
|
"logs",
|
|
|
|
"plan",
|
2018-03-21 00:46:24 +00:00
|
|
|
"validate",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common commands are grouped separately to call them out to operators.
|
|
|
|
commonCommands = []string{
|
|
|
|
"run",
|
|
|
|
"stop",
|
|
|
|
"status",
|
|
|
|
"alloc",
|
|
|
|
"job",
|
|
|
|
"node",
|
|
|
|
"agent",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-06-01 11:46:21 +00:00
|
|
|
func main() {
|
|
|
|
os.Exit(Run(os.Args[1:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Run(args []string) int {
|
2018-03-21 18:18:26 +00:00
|
|
|
// Create the meta object
|
|
|
|
metaPtr := new(command.Meta)
|
2021-10-06 14:02:42 +00:00
|
|
|
metaPtr.SetupUi(args)
|
2018-03-21 18:18:26 +00:00
|
|
|
|
2018-03-30 18:42:11 +00:00
|
|
|
// The Nomad agent never outputs color
|
|
|
|
agentUi := &cli.BasicUi{
|
|
|
|
Reader: os.Stdin,
|
|
|
|
Writer: os.Stdout,
|
|
|
|
ErrorWriter: os.Stderr,
|
|
|
|
}
|
|
|
|
|
|
|
|
commands := command.Commands(metaPtr, agentUi)
|
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"),
|
|
|
|
),
|
2018-03-21 18:18:26 +00:00
|
|
|
HelpWriter: os.Stdout,
|
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
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
2018-03-29 18:26:12 +00:00
|
|
|
// Filter out common commands and aliased commands from the other
|
|
|
|
// commands output
|
2018-03-21 00:46:24 +00:00
|
|
|
otherCommands := make([]string, 0, len(commands))
|
|
|
|
for k := range commands {
|
|
|
|
found := false
|
|
|
|
for _, v := range commonCommands {
|
|
|
|
if k == v {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 18:26:12 +00:00
|
|
|
for _, v := range aliases {
|
|
|
|
if k == v {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:46:24 +00:00
|
|
|
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())
|
|
|
|
}
|