From aa85b61fc874814f8958453ee3a79e32250a8c47 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 21 Mar 2018 10:16:13 -0700 Subject: [PATCH] Deprecated commands --- commands.go => command/commands.go | 316 +++++++++++++++++++---------- command/helpers.go | 20 ++ main.go | 9 +- 3 files changed, 230 insertions(+), 115 deletions(-) rename commands.go => command/commands.go (60%) diff --git a/commands.go b/command/commands.go similarity index 60% rename from commands.go rename to command/commands.go index 02abb39a4..40b9c0317 100644 --- a/commands.go +++ b/command/commands.go @@ -1,19 +1,51 @@ -package main +package command import ( + "fmt" "os" - "github.com/hashicorp/nomad/command" "github.com/hashicorp/nomad/command/agent" "github.com/hashicorp/nomad/version" "github.com/mitchellh/cli" ) +// DeprecatedCommand is a command that wraps an existing command and prints a +// deprecation notice and points the user to the new command. Deprecated +// commands are always hidden from help output. +type DeprecatedCommand struct { + cli.Command + Meta + + // Old is the old command name, New is the new command name. + Old, New string +} + +// Help wraps the embedded Help command and prints a warning about deprecations. +func (c *DeprecatedCommand) Help() string { + c.warn() + return c.Command.Help() +} + +// Run wraps the embedded Run command and prints a warning about deprecation. +func (c *DeprecatedCommand) Run(args []string) int { + c.warn() + return c.Command.Run(args) +} + +func (c *DeprecatedCommand) warn() { + c.Ui.Warn(wrapAtLength(fmt.Sprintf( + "WARNING! The \"nomad %s\" command is deprecated. Please use \"nomad %s\" "+ + "instead. This command will be removed in Nomad 0.10 (or later).", + c.Old, + c.New))) + c.Ui.Warn("") +} + // Commands returns the mapping of CLI commands for Nomad. The meta // parameter lets you set meta options for all commands. -func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory { +func Commands(metaPtr *Meta) map[string]cli.CommandFactory { if metaPtr == nil { - metaPtr = new(command.Meta) + metaPtr = new(Meta) } meta := *metaPtr @@ -25,94 +57,94 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory { } } - return map[string]cli.CommandFactory{ + all := map[string]cli.CommandFactory{ "acl": func() (cli.Command, error) { - return &command.ACLCommand{ + return &ACLCommand{ Meta: meta, }, nil }, "acl bootstrap": func() (cli.Command, error) { - return &command.ACLBootstrapCommand{ + return &ACLBootstrapCommand{ Meta: meta, }, nil }, "acl policy": func() (cli.Command, error) { - return &command.ACLPolicyCommand{ + return &ACLPolicyCommand{ Meta: meta, }, nil }, "acl policy apply": func() (cli.Command, error) { - return &command.ACLPolicyApplyCommand{ + return &ACLPolicyApplyCommand{ Meta: meta, }, nil }, "acl policy delete": func() (cli.Command, error) { - return &command.ACLPolicyDeleteCommand{ + return &ACLPolicyDeleteCommand{ Meta: meta, }, nil }, "acl policy info": func() (cli.Command, error) { - return &command.ACLPolicyInfoCommand{ + return &ACLPolicyInfoCommand{ Meta: meta, }, nil }, "acl policy list": func() (cli.Command, error) { - return &command.ACLPolicyListCommand{ + return &ACLPolicyListCommand{ Meta: meta, }, nil }, "acl token": func() (cli.Command, error) { - return &command.ACLTokenCommand{ + return &ACLTokenCommand{ Meta: meta, }, nil }, "acl token create": func() (cli.Command, error) { - return &command.ACLTokenCreateCommand{ + return &ACLTokenCreateCommand{ Meta: meta, }, nil }, "acl token update": func() (cli.Command, error) { - return &command.ACLTokenUpdateCommand{ + return &ACLTokenUpdateCommand{ Meta: meta, }, nil }, "acl token delete": func() (cli.Command, error) { - return &command.ACLTokenDeleteCommand{ + return &ACLTokenDeleteCommand{ Meta: meta, }, nil }, "acl token info": func() (cli.Command, error) { - return &command.ACLTokenInfoCommand{ + return &ACLTokenInfoCommand{ Meta: meta, }, nil }, "acl token self": func() (cli.Command, error) { - return &command.ACLTokenSelfCommand{ + return &ACLTokenSelfCommand{ Meta: meta, }, nil }, "alloc": func() (cli.Command, error) { - return &command.AllocCommand{ + return &AllocCommand{ Meta: meta, }, nil }, "alloc fs": func() (cli.Command, error) { - return &command.AllocFSCommand{ + return &AllocFSCommand{ Meta: meta, }, nil }, "alloc logs": func() (cli.Command, error) { - return &command.AllocLogsCommand{ + return &AllocLogsCommand{ Meta: meta, }, nil }, "alloc status": func() (cli.Command, error) { - return &command.AllocStatusCommand{ + return &AllocStatusCommand{ Meta: meta, }, nil }, "alloc-status": func() (cli.Command, error) { - return &command.AllocStatusCommand{ + return &AllocStatusCommand{ Meta: meta, }, nil }, @@ -124,424 +156,492 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory { }, nil }, "agent-info": func() (cli.Command, error) { - return &command.AgentInfoCommand{ + return &AgentInfoCommand{ Meta: meta, }, nil }, "check": func() (cli.Command, error) { - return &command.AgentCheckCommand{ - Meta: meta, - }, nil - }, - "client-config": func() (cli.Command, error) { - return &command.NodeConfigCommand{ + return &AgentCheckCommand{ Meta: meta, }, nil }, "deployment": func() (cli.Command, error) { - return &command.DeploymentCommand{ + return &DeploymentCommand{ Meta: meta, }, nil }, "deployment fail": func() (cli.Command, error) { - return &command.DeploymentFailCommand{ + return &DeploymentFailCommand{ Meta: meta, }, nil }, "deployment list": func() (cli.Command, error) { - return &command.DeploymentListCommand{ + return &DeploymentListCommand{ Meta: meta, }, nil }, "deployment pause": func() (cli.Command, error) { - return &command.DeploymentPauseCommand{ + return &DeploymentPauseCommand{ Meta: meta, }, nil }, "deployment promote": func() (cli.Command, error) { - return &command.DeploymentPromoteCommand{ + return &DeploymentPromoteCommand{ Meta: meta, }, nil }, "deployment resume": func() (cli.Command, error) { - return &command.DeploymentResumeCommand{ + return &DeploymentResumeCommand{ Meta: meta, }, nil }, "deployment status": func() (cli.Command, error) { - return &command.DeploymentStatusCommand{ + return &DeploymentStatusCommand{ Meta: meta, }, nil }, "eval": func() (cli.Command, error) { - return &command.EvalCommand{ + return &EvalCommand{ Meta: meta, }, nil }, "eval status": func() (cli.Command, error) { - return &command.EvalStatusCommand{ + return &EvalStatusCommand{ Meta: meta, }, nil }, "eval-status": func() (cli.Command, error) { - return &command.EvalStatusCommand{ + return &EvalStatusCommand{ Meta: meta, }, nil }, "executor": func() (cli.Command, error) { - return &command.ExecutorPluginCommand{ + return &ExecutorPluginCommand{ Meta: meta, }, nil }, "fs": func() (cli.Command, error) { - return &command.AllocFSCommand{ + return &AllocFSCommand{ Meta: meta, }, nil }, "init": func() (cli.Command, error) { - return &command.JobInitCommand{ + return &JobInitCommand{ Meta: meta, }, nil }, "inspect": func() (cli.Command, error) { - return &command.JobInspectCommand{ + return &JobInspectCommand{ Meta: meta, }, nil }, "keygen": func() (cli.Command, error) { - return &command.OperatorKeygenCommand{ + return &OperatorKeygenCommand{ Meta: meta, }, nil }, "keyring": func() (cli.Command, error) { - return &command.OperatorKeyringCommand{ + return &OperatorKeyringCommand{ Meta: meta, }, nil }, "job": func() (cli.Command, error) { - return &command.JobCommand{ + return &JobCommand{ Meta: meta, }, nil }, "job deployments": func() (cli.Command, error) { - return &command.JobDeploymentsCommand{ + return &JobDeploymentsCommand{ Meta: meta, }, nil }, "job dispatch": func() (cli.Command, error) { - return &command.JobDispatchCommand{ + return &JobDispatchCommand{ Meta: meta, }, nil }, "job history": func() (cli.Command, error) { - return &command.JobHistoryCommand{ + return &JobHistoryCommand{ Meta: meta, }, nil }, "job init": func() (cli.Command, error) { - return &command.JobInitCommand{ + return &JobInitCommand{ Meta: meta, }, nil }, "job inspect": func() (cli.Command, error) { - return &command.JobInspectCommand{ + return &JobInspectCommand{ Meta: meta, }, nil }, "job plan": func() (cli.Command, error) { - return &command.JobPlanCommand{ + return &JobPlanCommand{ Meta: meta, }, nil }, "job promote": func() (cli.Command, error) { - return &command.JobPromoteCommand{ + return &JobPromoteCommand{ Meta: meta, }, nil }, "job revert": func() (cli.Command, error) { - return &command.JobRevertCommand{ + return &JobRevertCommand{ Meta: meta, }, nil }, "job run": func() (cli.Command, error) { - return &command.JobRunCommand{ + return &JobRunCommand{ Meta: meta, }, nil }, "job status": func() (cli.Command, error) { - return &command.JobStatusCommand{ + return &JobStatusCommand{ Meta: meta, }, nil }, "job stop": func() (cli.Command, error) { - return &command.JobStopCommand{ + return &JobStopCommand{ Meta: meta, }, nil }, "job validate": func() (cli.Command, error) { - return &command.JobValidateCommand{ + return &JobValidateCommand{ Meta: meta, }, nil }, "logs": func() (cli.Command, error) { - return &command.AllocLogsCommand{ + return &AllocLogsCommand{ Meta: meta, }, nil }, "namespace": func() (cli.Command, error) { - return &command.NamespaceCommand{ + return &NamespaceCommand{ Meta: meta, }, nil }, "namespace apply": func() (cli.Command, error) { - return &command.NamespaceApplyCommand{ + return &NamespaceApplyCommand{ Meta: meta, }, nil }, "namespace delete": func() (cli.Command, error) { - return &command.NamespaceDeleteCommand{ + return &NamespaceDeleteCommand{ Meta: meta, }, nil }, "namespace inspect": func() (cli.Command, error) { - return &command.NamespaceInspectCommand{ + return &NamespaceInspectCommand{ Meta: meta, }, nil }, "namespace list": func() (cli.Command, error) { - return &command.NamespaceListCommand{ + return &NamespaceListCommand{ Meta: meta, }, nil }, "namespace status": func() (cli.Command, error) { - return &command.NamespaceStatusCommand{ + return &NamespaceStatusCommand{ Meta: meta, }, nil }, "node": func() (cli.Command, error) { - return &command.NodeCommand{ + return &NodeCommand{ Meta: meta, }, nil }, "node config": func() (cli.Command, error) { - return &command.NodeConfigCommand{ + return &NodeConfigCommand{ Meta: meta, }, nil }, "node-drain": func() (cli.Command, error) { - return &command.NodeDrainCommand{ + return &NodeDrainCommand{ Meta: meta, }, nil }, "node drain": func() (cli.Command, error) { - return &command.NodeDrainCommand{ + return &NodeDrainCommand{ Meta: meta, }, nil }, "node eligibility": func() (cli.Command, error) { - return &command.NodeEligibilityCommand{ + return &NodeEligibilityCommand{ Meta: meta, }, nil }, "node-status": func() (cli.Command, error) { - return &command.NodeStatusCommand{ + return &NodeStatusCommand{ Meta: meta, }, nil }, "node status": func() (cli.Command, error) { - return &command.NodeStatusCommand{ + return &NodeStatusCommand{ Meta: meta, }, nil }, "operator": func() (cli.Command, error) { - return &command.OperatorCommand{ + return &OperatorCommand{ Meta: meta, }, nil }, "operator autopilot": func() (cli.Command, error) { - return &command.OperatorAutopilotCommand{ + return &OperatorAutopilotCommand{ Meta: meta, }, nil }, "operator autopilot get-config": func() (cli.Command, error) { - return &command.OperatorAutopilotGetCommand{ + return &OperatorAutopilotGetCommand{ Meta: meta, }, nil }, "operator autopilot set-config": func() (cli.Command, error) { - return &command.OperatorAutopilotSetCommand{ + return &OperatorAutopilotSetCommand{ Meta: meta, }, nil }, "operator keygen": func() (cli.Command, error) { - return &command.OperatorKeygenCommand{ + return &OperatorKeygenCommand{ Meta: meta, }, nil }, "operator keyring": func() (cli.Command, error) { - return &command.OperatorKeyringCommand{ + return &OperatorKeyringCommand{ Meta: meta, }, nil }, "operator raft": func() (cli.Command, error) { - return &command.OperatorRaftCommand{ + return &OperatorRaftCommand{ Meta: meta, }, nil }, "operator raft list-peers": func() (cli.Command, error) { - return &command.OperatorRaftListCommand{ + return &OperatorRaftListCommand{ Meta: meta, }, nil }, "operator raft remove-peer": func() (cli.Command, error) { - return &command.OperatorRaftRemoveCommand{ + return &OperatorRaftRemoveCommand{ Meta: meta, }, nil }, "plan": func() (cli.Command, error) { - return &command.JobPlanCommand{ + return &JobPlanCommand{ Meta: meta, }, nil }, "quota": func() (cli.Command, error) { - return &command.QuotaCommand{ + return &QuotaCommand{ Meta: meta, }, nil }, "quota apply": func() (cli.Command, error) { - return &command.QuotaApplyCommand{ + return &QuotaApplyCommand{ Meta: meta, }, nil }, "quota delete": func() (cli.Command, error) { - return &command.QuotaDeleteCommand{ + return &QuotaDeleteCommand{ Meta: meta, }, nil }, "quota init": func() (cli.Command, error) { - return &command.QuotaInitCommand{ + return &QuotaInitCommand{ Meta: meta, }, nil }, "quota inspect": func() (cli.Command, error) { - return &command.QuotaInspectCommand{ + return &QuotaInspectCommand{ Meta: meta, }, nil }, "quota list": func() (cli.Command, error) { - return &command.QuotaListCommand{ + return &QuotaListCommand{ Meta: meta, }, nil }, "quota status": func() (cli.Command, error) { - return &command.QuotaStatusCommand{ + return &QuotaStatusCommand{ Meta: meta, }, nil }, "run": func() (cli.Command, error) { - return &command.JobRunCommand{ + return &JobRunCommand{ Meta: meta, }, nil }, "sentinel": func() (cli.Command, error) { - return &command.SentinelCommand{ + return &SentinelCommand{ Meta: meta, }, nil }, "sentinel list": func() (cli.Command, error) { - return &command.SentinelListCommand{ + return &SentinelListCommand{ Meta: meta, }, nil }, "sentinel apply": func() (cli.Command, error) { - return &command.SentinelApplyCommand{ + return &SentinelApplyCommand{ Meta: meta, }, nil }, "sentinel delete": func() (cli.Command, error) { - return &command.SentinelDeleteCommand{ + return &SentinelDeleteCommand{ Meta: meta, }, nil }, "sentinel read": func() (cli.Command, error) { - return &command.SentinelReadCommand{ + return &SentinelReadCommand{ Meta: meta, }, nil }, "server": func() (cli.Command, error) { - return &command.ServerCommand{ + return &ServerCommand{ Meta: meta, }, nil }, "server force-leave": func() (cli.Command, error) { - return &command.ServerForceLeaveCommand{ + return &ServerForceLeaveCommand{ Meta: meta, }, nil }, "server join": func() (cli.Command, error) { - return &command.ServerJoinCommand{ + return &ServerJoinCommand{ Meta: meta, }, nil }, "server members": func() (cli.Command, error) { - return &command.ServerMembersCommand{ + return &ServerMembersCommand{ Meta: meta, }, nil }, "server-force-leave": func() (cli.Command, error) { - return &command.ServerForceLeaveCommand{ + return &ServerForceLeaveCommand{ Meta: meta, }, nil }, "server-join": func() (cli.Command, error) { - return &command.ServerJoinCommand{ + return &ServerJoinCommand{ Meta: meta, }, nil }, "server-members": func() (cli.Command, error) { - return &command.ServerMembersCommand{ + return &ServerMembersCommand{ Meta: meta, }, nil }, "status": func() (cli.Command, error) { - return &command.StatusCommand{ + return &StatusCommand{ Meta: meta, }, nil }, "stop": func() (cli.Command, error) { - return &command.JobStopCommand{ + return &JobStopCommand{ Meta: meta, }, nil }, "ui": func() (cli.Command, error) { - return &command.UiCommand{ + return &UiCommand{ Meta: meta, }, nil }, "validate": func() (cli.Command, error) { - return &command.JobValidateCommand{ + return &JobValidateCommand{ Meta: meta, }, nil }, "version": func() (cli.Command, error) { - return &command.VersionCommand{ + return &VersionCommand{ Version: version.GetVersion(), Ui: meta.Ui, }, nil }, } + + deprecated := map[string]cli.CommandFactory{ + "client-config": func() (cli.Command, error) { + return &DeprecatedCommand{ + Old: "client-config", + New: "node config", + Meta: meta, + Command: &NodeConfigCommand{ + Meta: meta, + }, + }, nil + }, + + "keygen": func() (cli.Command, error) { + return &DeprecatedCommand{ + Old: "keygen", + New: "operator keygen", + Meta: meta, + Command: &OperatorKeygenCommand{ + Meta: meta, + }, + }, nil + }, + + "keyring": func() (cli.Command, error) { + return &DeprecatedCommand{ + Old: "keyring", + New: "operator keyring", + Meta: meta, + Command: &OperatorKeyringCommand{ + Meta: meta, + }, + }, nil + }, + + "server-force-leave": func() (cli.Command, error) { + return &DeprecatedCommand{ + Old: "server-force-leave", + New: "server force-leave", + Meta: meta, + Command: &ServerForceLeaveCommand{ + Meta: meta, + }, + }, nil + }, + + "server-join": func() (cli.Command, error) { + return &DeprecatedCommand{ + Old: "server-join", + New: "server join", + Meta: meta, + Command: &ServerJoinCommand{ + Meta: meta, + }, + }, nil + }, + + "server-members": func() (cli.Command, error) { + return &DeprecatedCommand{ + Old: "server-members", + New: "server members", + Meta: meta, + Command: &ServerMembersCommand{ + Meta: meta, + }, + }, nil + }, + } + + for k, v := range deprecated { + all[k] = v + } + return all } diff --git a/command/helpers.go b/command/helpers.go index 9b1b6fed8..025dc6555 100644 --- a/command/helpers.go +++ b/command/helpers.go @@ -13,11 +13,15 @@ import ( gg "github.com/hashicorp/go-getter" "github.com/hashicorp/nomad/api" "github.com/hashicorp/nomad/jobspec" + "github.com/kr/text" "github.com/posener/complete" "github.com/ryanuber/columnize" ) +// maxLineLength is the maximum width of any line. +const maxLineLength int = 78 + // formatKV takes a set of strings and formats them into properly // aligned k = v pairs using the columnize library. func formatKV(in []string) string { @@ -53,6 +57,22 @@ func limit(s string, length int) string { return s[:length] } +// wrapAtLengthWithPadding wraps the given text at the maxLineLength, taking +// into account any provided left padding. +func wrapAtLengthWithPadding(s string, pad int) string { + wrapped := text.Wrap(s, maxLineLength-pad) + lines := strings.Split(wrapped, "\n") + for i, line := range lines { + lines[i] = strings.Repeat(" ", pad) + line + } + return strings.Join(lines, "\n") +} + +// wrapAtLength wraps the given text to maxLineLength. +func wrapAtLength(s string) string { + return wrapAtLengthWithPadding(s, 0) +} + // formatTime formats the time to string based on RFC822 func formatTime(t time.Time) string { if t.Unix() < 1 { diff --git a/main.go b/main.go index af74edc7d..13a07c02b 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "strings" "text/tabwriter" + "github.com/hashicorp/nomad/command" "github.com/hashicorp/nomad/version" "github.com/mitchellh/cli" "github.com/sean-/seed" @@ -62,16 +63,10 @@ func main() { } func Run(args []string) int { - return RunCustom(args, Commands(nil)) + return RunCustom(args, command.Commands(nil)) } 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 { - commandsInclude = append(commandsInclude, k) - } - cli := &cli.CLI{ Name: "nomad", Version: version.GetVersion().FullVersionNumber(true),