From 8c14f93fb1c50c7281d98d93095ff04498376f1e Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Thu, 9 Feb 2017 19:32:22 -0500 Subject: [PATCH] Convert reload command to use base.Command --- command/reload.go | 21 +++++++----------- command/reload_test.go | 22 +++++++++++++++---- command/util_test.go | 12 +++++++--- commands.go | 5 ++++- .../source/docs/commands/reload.html.markdown | 8 ++----- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/command/reload.go b/command/reload.go index c956eaf096..1f82e16a8f 100644 --- a/command/reload.go +++ b/command/reload.go @@ -1,16 +1,15 @@ package command import ( - "flag" "fmt" - "github.com/mitchellh/cli" + "github.com/hashicorp/consul/command/base" "strings" ) // ReloadCommand is a Command implementation that instructs // the Consul agent to reload configurations type ReloadCommand struct { - Ui cli.Ui + base.Command } func (c *ReloadCommand) Help() string { @@ -20,29 +19,25 @@ Usage: consul reload Causes the agent to reload configurations. This can be used instead of sending the SIGHUP signal to the agent. -Options: +` + c.Command.Help() - -rpc-addr=127.0.0.1:8400 RPC address of the Consul agent. -` return strings.TrimSpace(helpText) } func (c *ReloadCommand) Run(args []string) int { - cmdFlags := flag.NewFlagSet("reload", flag.ContinueOnError) - cmdFlags.Usage = func() { c.Ui.Output(c.Help()) } - rpcAddr := RPCAddrFlag(cmdFlags) - if err := cmdFlags.Parse(args); err != nil { + c.Command.NewFlagSet(c) + + if err := c.Command.Parse(args); err != nil { 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.Reload(); err != nil { + if err := client.Agent().Reload(); err != nil { c.Ui.Error(fmt.Sprintf("Error reloading: %s", err)) return 1 } diff --git a/command/reload_test.go b/command/reload_test.go index f34c987496..151d05c66e 100644 --- a/command/reload_test.go +++ b/command/reload_test.go @@ -1,9 +1,11 @@ package command import ( - "github.com/mitchellh/cli" "strings" "testing" + + "github.com/hashicorp/consul/command/base" + "github.com/mitchellh/cli" ) func TestReloadCommand_implements(t *testing.T) { @@ -11,12 +13,24 @@ func TestReloadCommand_implements(t *testing.T) { } func TestReloadCommandRun(t *testing.T) { - a1 := testAgent(t) + reloadCh := make(chan chan error) + a1 := testAgentWithConfigReload(t, nil, reloadCh) defer a1.Shutdown() + // Setup a dummy response to errCh to simulate a successful reload + go func() { + errCh := <- reloadCh + errCh <- nil + }() + ui := new(cli.MockUi) - c := &ReloadCommand{Ui: ui} - args := []string{"-rpc-addr=" + a1.addr} + c := &ReloadCommand{ + Command: base.Command{ + Ui: ui, + Flags: base.FlagSetClientHTTP, + }, + } + args := []string{"-http-addr=" + a1.httpAddr} code := c.Run(args) if code != 0 { diff --git a/command/util_test.go b/command/util_test.go index 646c1a5265..9309129b90 100644 --- a/command/util_test.go +++ b/command/util_test.go @@ -44,7 +44,7 @@ func (a *agentWrapper) Shutdown() { } func testAgent(t *testing.T) *agentWrapper { - return testAgentWithConfig(t, func(c *agent.Config) {}) + return testAgentWithConfig(t, nil) } func testAgentWithAPIClient(t *testing.T) (*agentWrapper, *api.Client) { @@ -57,6 +57,10 @@ func testAgentWithAPIClient(t *testing.T) (*agentWrapper, *api.Client) { } func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper { + return testAgentWithConfigReload(t, cb, nil) +} + +func testAgentWithConfigReload(t *testing.T, cb func(c *agent.Config), reloadCh chan chan error) *agentWrapper { l, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatalf("err: %s", err) @@ -66,7 +70,9 @@ func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper { mult := io.MultiWriter(os.Stderr, lw) conf := nextConfig() - cb(conf) + if cb != nil { + cb(conf) + } dir, err := ioutil.TempDir("", "agent") if err != nil { @@ -74,7 +80,7 @@ func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper { } conf.DataDir = dir - a, err := agent.Create(conf, lw, nil, nil) + a, err := agent.Create(conf, lw, nil, reloadCh) if err != nil { os.RemoveAll(dir) t.Fatalf(fmt.Sprintf("err: %v", err)) diff --git a/commands.go b/commands.go index 396ba92336..cd1f009398 100644 --- a/commands.go +++ b/commands.go @@ -179,7 +179,10 @@ func init() { "reload": func() (cli.Command, error) { return &command.ReloadCommand{ - Ui: ui, + Command: base.Command{ + Flags: base.FlagSetClientHTTP, + Ui: ui, + }, }, nil }, diff --git a/website/source/docs/commands/reload.html.markdown b/website/source/docs/commands/reload.html.markdown index f1a59f745e..8dac42d5ec 100644 --- a/website/source/docs/commands/reload.html.markdown +++ b/website/source/docs/commands/reload.html.markdown @@ -29,10 +29,6 @@ section on the agent options page for details on which options are supported. Usage: `consul reload` -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" %>