From 6c4d4f9a32ebe6b82d9d307f76e893efcba676a4 Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Thu, 9 Feb 2017 16:50:45 -0500 Subject: [PATCH] Convert maint command to use base.Command --- command/maint.go | 36 ++++++------------- command/maint_test.go | 32 +++++++++-------- commands.go | 5 ++- ....html.markdown => maint.html.markdown.erb} | 12 +++---- 4 files changed, 36 insertions(+), 49 deletions(-) rename website/source/docs/commands/{maint.html.markdown => maint.html.markdown.erb} (84%) diff --git a/command/maint.go b/command/maint.go index 0151465650..d225f30e9b 100644 --- a/command/maint.go +++ b/command/maint.go @@ -1,18 +1,16 @@ package command import ( - "flag" "fmt" "strings" - "github.com/hashicorp/consul/api" - "github.com/mitchellh/cli" + "github.com/hashicorp/consul/command/base" ) // MaintCommand is a Command implementation that enables or disables // node or service maintenance mode. type MaintCommand struct { - Ui cli.Ui + base.Command } func (c *MaintCommand) Help() string { @@ -40,15 +38,8 @@ Usage: consul maint [options] If no arguments are given, the agent's maintenance status will be shown. This will return blank if nothing is currently under maintenance. -Options: +` + c.Command.Help() - -enable Enable maintenance mode. - -disable Disable maintenance mode. - -reason= Text string describing the maintenance reason - -service= Control maintenance mode for a specific service ID - -token="" ACL token to use. Defaults to that of agent. - -http-addr=127.0.0.1:8500 HTTP address of the Consul agent. -` return strings.TrimSpace(helpText) } @@ -57,19 +48,15 @@ func (c *MaintCommand) Run(args []string) int { var disable bool var reason string var serviceID string - var token string - cmdFlags := flag.NewFlagSet("maint", flag.ContinueOnError) - cmdFlags.Usage = func() { c.Ui.Output(c.Help()) } + f := c.Command.NewFlagSet(c) - cmdFlags.BoolVar(&enable, "enable", false, "enable maintenance mode") - cmdFlags.BoolVar(&disable, "disable", false, "disable maintenance mode") - cmdFlags.StringVar(&reason, "reason", "", "maintenance reason") - cmdFlags.StringVar(&serviceID, "service", "", "service maintenance") - cmdFlags.StringVar(&token, "token", "", "") - httpAddr := HTTPAddrFlag(cmdFlags) + f.BoolVar(&enable, "enable", false, "Enable maintenance mode.") + f.BoolVar(&disable, "disable", false, "Disable maintenance mode.") + f.StringVar(&reason, "reason", "", "Text describing the maintenance reason.") + f.StringVar(&serviceID, "service", "", "Control maintenance mode for a specific service ID.") - if err := cmdFlags.Parse(args); err != nil { + if err := c.Command.Parse(args); err != nil { return 1 } @@ -88,10 +75,7 @@ func (c *MaintCommand) Run(args []string) int { } // Create and test the HTTP client - conf := api.DefaultConfig() - conf.Address = *httpAddr - conf.Token = token - client, err := api.NewClient(conf) + client, err := c.Command.HTTPClient() if err != nil { c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) return 1 diff --git a/command/maint_test.go b/command/maint_test.go index 0ac534e13a..83b3805493 100644 --- a/command/maint_test.go +++ b/command/maint_test.go @@ -4,17 +4,27 @@ import ( "strings" "testing" + "github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/consul/structs" "github.com/mitchellh/cli" ) +func testMaintCommand(t *testing.T) (*cli.MockUi, *MaintCommand) { + ui := new(cli.MockUi) + return ui, &MaintCommand{ + Command: base.Command{ + Ui: ui, + Flags: base.FlagSetClientHTTP, + }, + } +} + func TestMaintCommand_implements(t *testing.T) { var _ cli.Command = &MaintCommand{} } func TestMaintCommandRun_ConflictingArgs(t *testing.T) { - ui := new(cli.MockUi) - c := &MaintCommand{Ui: ui} + _, c := testMaintCommand(t) if code := c.Run([]string{"-enable", "-disable"}); code != 1 { t.Fatalf("expected return code 1, got %d", code) @@ -53,8 +63,7 @@ func TestMaintCommandRun_NoArgs(t *testing.T) { a1.agent.EnableNodeMaintenance("broken 2", "") // Run consul maint with no args (list mode) - ui := new(cli.MockUi) - c := &MaintCommand{Ui: ui} + ui, c := testMaintCommand(t) args := []string{"-http-addr=" + a1.httpAddr} code := c.Run(args) @@ -84,8 +93,7 @@ func TestMaintCommandRun_EnableNodeMaintenance(t *testing.T) { a1 := testAgent(t) defer a1.Shutdown() - ui := new(cli.MockUi) - c := &MaintCommand{Ui: ui} + ui, c := testMaintCommand(t) args := []string{ "-http-addr=" + a1.httpAddr, @@ -106,8 +114,7 @@ func TestMaintCommandRun_DisableNodeMaintenance(t *testing.T) { a1 := testAgent(t) defer a1.Shutdown() - ui := new(cli.MockUi) - c := &MaintCommand{Ui: ui} + ui, c := testMaintCommand(t) args := []string{ "-http-addr=" + a1.httpAddr, @@ -136,8 +143,7 @@ func TestMaintCommandRun_EnableServiceMaintenance(t *testing.T) { t.Fatalf("err: %v", err) } - ui := new(cli.MockUi) - c := &MaintCommand{Ui: ui} + ui, c := testMaintCommand(t) args := []string{ "-http-addr=" + a1.httpAddr, @@ -168,8 +174,7 @@ func TestMaintCommandRun_DisableServiceMaintenance(t *testing.T) { t.Fatalf("err: %v", err) } - ui := new(cli.MockUi) - c := &MaintCommand{Ui: ui} + ui, c := testMaintCommand(t) args := []string{ "-http-addr=" + a1.httpAddr, @@ -190,8 +195,7 @@ func TestMaintCommandRun_ServiceMaintenance_NoService(t *testing.T) { a1 := testAgent(t) defer a1.Shutdown() - ui := new(cli.MockUi) - c := &MaintCommand{Ui: ui} + ui, c := testMaintCommand(t) args := []string{ "-http-addr=" + a1.httpAddr, diff --git a/commands.go b/commands.go index ba39f8dfd8..8f24dfc542 100644 --- a/commands.go +++ b/commands.go @@ -136,7 +136,10 @@ func init() { "maint": func() (cli.Command, error) { return &command.MaintCommand{ - Ui: ui, + Command: base.Command{ + Flags: base.FlagSetClientHTTP, + Ui: ui, + }, }, nil }, diff --git a/website/source/docs/commands/maint.html.markdown b/website/source/docs/commands/maint.html.markdown.erb similarity index 84% rename from website/source/docs/commands/maint.html.markdown rename to website/source/docs/commands/maint.html.markdown.erb index f906332e23..f1f9dcd793 100644 --- a/website/source/docs/commands/maint.html.markdown +++ b/website/source/docs/commands/maint.html.markdown.erb @@ -24,9 +24,11 @@ health check. Usage: `consul maint [options]` -All of the command line arguments are optional. +#### API Options -The list of available flags are: +<%= partial "docs/commands/http_api_options_client" %> + +#### Command Options * `-enable` - Enable maintenance mode on a given service or node. If combined with the `-service` flag, we operate on a specific service ID. @@ -44,12 +46,6 @@ The list of available flags are: providing this flag, the `-enable` and `-disable` flags functionality is modified to operate on the given service ID. -* `-token` - ACL token to use. Defaults to that of agent. - -* `-http-addr` - Address to the HTTP server of the agent you want to contact - to send this command. If this isn't specified, the command will contact - "127.0.0.1:8500" which is the default HTTP address of a Consul agent. - ## List mode If neither `-enable` nor `-disable` are passed, the `maint` command will