From e385af8eeb84e2ff7ccde5c699c6b9a51e5feb0d Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Thu, 9 Feb 2017 16:41:17 -0500 Subject: [PATCH] Convert leave command to use base.Command --- command/leave.go | 24 +++++++------------ command/leave_test.go | 21 +++++++++++----- commands.go | 5 +++- ....html.markdown => leave.html.markdown.erb} | 10 +++----- 4 files changed, 31 insertions(+), 29 deletions(-) rename website/source/docs/commands/{leave.html.markdown => leave.html.markdown.erb} (69%) diff --git a/command/leave.go b/command/leave.go index 819e10d67..c2a770109 100644 --- a/command/leave.go +++ b/command/leave.go @@ -1,53 +1,47 @@ package command import ( - "flag" "fmt" - "github.com/mitchellh/cli" + "github.com/hashicorp/consul/command/base" "strings" ) // LeaveCommand is a Command implementation that instructs // the Consul agent to gracefully leave the cluster type LeaveCommand struct { - Ui cli.Ui + base.Command } func (c *LeaveCommand) Help() string { helpText := ` -Usage: consul leave +Usage: consul leave [options] Causes the agent to gracefully leave the Consul cluster and shutdown. -Options: +` + c.Command.Help() - -rpc-addr=127.0.0.1:8400 RPC address of the Consul agent. -` return strings.TrimSpace(helpText) } func (c *LeaveCommand) Run(args []string) int { - cmdFlags := flag.NewFlagSet("leave", flag.ContinueOnError) - cmdFlags.Usage = func() { c.Ui.Output(c.Help()) } - rpcAddr := RPCAddrFlag(cmdFlags) - if err := cmdFlags.Parse(args); err != nil { + f := c.Command.NewFlagSet(c) + if err := c.Command.Parse(args); err != nil { return 1 } - nonFlagArgs := cmdFlags.Args() + nonFlagArgs := f.Args() if len(nonFlagArgs) > 0 { c.Ui.Error(fmt.Sprintf("Error found unexpected args: %v", nonFlagArgs)) c.Ui.Output(c.Help()) return 1 } - client, err := RPCClient(*rpcAddr) + client, err := c.Command.HTTPClient() if err != nil { c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) return 1 } - defer client.Close() - if err := client.Leave(); err != nil { + if err := client.Agent().Leave(); err != nil { c.Ui.Error(fmt.Sprintf("Error leaving: %s", err)) return 1 } diff --git a/command/leave_test.go b/command/leave_test.go index 865df8e75..804e9eb07 100644 --- a/command/leave_test.go +++ b/command/leave_test.go @@ -1,11 +1,22 @@ package command import ( + "github.com/hashicorp/consul/command/base" "github.com/mitchellh/cli" "strings" "testing" ) +func testLeaveCommand(t *testing.T) (*cli.MockUi, *LeaveCommand) { + ui := new(cli.MockUi) + return ui, &LeaveCommand{ + Command: base.Command{ + Ui: ui, + Flags: base.FlagSetClientHTTP, + }, + } +} + func TestLeaveCommand_implements(t *testing.T) { var _ cli.Command = &LeaveCommand{} } @@ -14,9 +25,8 @@ func TestLeaveCommandRun(t *testing.T) { a1 := testAgent(t) defer a1.Shutdown() - ui := new(cli.MockUi) - c := &LeaveCommand{Ui: ui} - args := []string{"-rpc-addr=" + a1.addr} + ui, c := testLeaveCommand(t) + args := []string{"-http-addr=" + a1.httpAddr} code := c.Run(args) if code != 0 { @@ -32,9 +42,8 @@ func TestLeaveCommandFailOnNonFlagArgs(t *testing.T) { a1 := testAgent(t) defer a1.Shutdown() - ui := new(cli.MockUi) - c := &LeaveCommand{Ui: ui} - args := []string{"-rpc-addr=" + a1.addr, "appserver1"} + _, c := testLeaveCommand(t) + args := []string{"-http-addr=" + a1.httpAddr, "appserver1"} code := c.Run(args) if code == 0 { diff --git a/commands.go b/commands.go index 3a9ee3967..ba39f8dfd 100644 --- a/commands.go +++ b/commands.go @@ -117,7 +117,10 @@ func init() { "leave": func() (cli.Command, error) { return &command.LeaveCommand{ - Ui: ui, + Command: base.Command{ + Flags: base.FlagSetClientHTTP, + Ui: ui, + }, }, nil }, diff --git a/website/source/docs/commands/leave.html.markdown b/website/source/docs/commands/leave.html.markdown.erb similarity index 69% rename from website/source/docs/commands/leave.html.markdown rename to website/source/docs/commands/leave.html.markdown.erb index be55e61cb..1b436ff1b 100644 --- a/website/source/docs/commands/leave.html.markdown +++ b/website/source/docs/commands/leave.html.markdown.erb @@ -21,12 +21,8 @@ non-graceful leave can affect cluster availability. ## Usage -Usage: `consul leave` +Usage: `consul leave [options]` -The command-line flags are all optional. The list of available flags are: - -* `-rpc-addr` - Address to the RPC server of the agent you want to contact - to send this command. If this isn't specified, the command checks the - CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC - address will be set to "127.0.0.1:8400". +#### API Options +<%= partial "docs/commands/http_api_options_client" %>