From 1ba816b0ae9b0f6e484fe82e14be43950c289f50 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Tue, 17 Oct 2017 08:47:09 +0200 Subject: [PATCH] commands: move reload command to separate pkg --- command/commands.go | 8 +--- command/reload.go | 47 --------------------- command/reload/reload.go | 63 +++++++++++++++++++++++++++++ command/{ => reload}/reload_test.go | 15 +++---- 4 files changed, 71 insertions(+), 62 deletions(-) delete mode 100644 command/reload.go create mode 100644 command/reload/reload.go rename command/{ => reload}/reload_test.go (76%) diff --git a/command/commands.go b/command/commands.go index e1890ee03..fb72255ba 100644 --- a/command/commands.go +++ b/command/commands.go @@ -34,6 +34,7 @@ import ( "github.com/hashicorp/consul/command/operraft" "github.com/hashicorp/consul/command/operraftlist" "github.com/hashicorp/consul/command/operraftremove" + "github.com/hashicorp/consul/command/reload" "github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/version" "github.com/mitchellh/cli" @@ -177,12 +178,7 @@ func init() { }, "reload": func() (cli.Command, error) { - return &ReloadCommand{ - BaseCommand: BaseCommand{ - Flags: FlagSetClientHTTP, - UI: ui, - }, - }, nil + return reload.New(ui), nil }, "rtt": func() (cli.Command, error) { diff --git a/command/reload.go b/command/reload.go deleted file mode 100644 index 88cccbe04..000000000 --- a/command/reload.go +++ /dev/null @@ -1,47 +0,0 @@ -package command - -import ( - "fmt" -) - -// ReloadCommand is a Command implementation that instructs -// the Consul agent to reload configurations -type ReloadCommand struct { - BaseCommand -} - -func (c *ReloadCommand) Help() string { - c.InitFlagSet() - return c.HelpCommand(` -Usage: consul reload - - Causes the agent to reload configurations. This can be used instead - of sending the SIGHUP signal to the agent. - -`) -} - -func (c *ReloadCommand) Run(args []string) int { - c.InitFlagSet() - if err := c.FlagSet.Parse(args); err != nil { - return 1 - } - - client, err := c.HTTPClient() - if err != nil { - c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) - return 1 - } - - if err := client.Agent().Reload(); err != nil { - c.UI.Error(fmt.Sprintf("Error reloading: %s", err)) - return 1 - } - - c.UI.Output("Configuration reload triggered") - return 0 -} - -func (c *ReloadCommand) Synopsis() string { - return "Triggers the agent to reload configuration files" -} diff --git a/command/reload/reload.go b/command/reload/reload.go new file mode 100644 index 000000000..e9b4354ef --- /dev/null +++ b/command/reload/reload.go @@ -0,0 +1,63 @@ +package reload + +import ( + "flag" + "fmt" + + "github.com/hashicorp/consul/command/flags" + "github.com/mitchellh/cli" +) + +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.init() + return c +} + +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + http *flags.HTTPFlags + usage string +} + +func (c *cmd) init() { + c.flags = flag.NewFlagSet("", flag.ContinueOnError) + c.http = &flags.HTTPFlags{} + flags.Merge(c.flags, c.http.ClientFlags()) + flags.Merge(c.flags, c.http.ServerFlags()) + c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags()) +} + +func (c *cmd) Synopsis() string { + return "Triggers the agent to reload configuration files" +} + +func (c *cmd) Help() string { + return c.usage +} + +func (c *cmd) Run(args []string) int { + if err := c.flags.Parse(args); err != nil { + return 1 + } + + client, err := c.http.APIClient() + if err != nil { + c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) + return 1 + } + + if err := client.Agent().Reload(); err != nil { + c.UI.Error(fmt.Sprintf("Error reloading: %s", err)) + return 1 + } + + c.UI.Output("Configuration reload triggered") + return 0 +} + +const usage = `Usage: consul reload + + Causes the agent to reload configurations. This can be used instead + of sending the SIGHUP signal to the agent.` diff --git a/command/reload_test.go b/command/reload/reload_test.go similarity index 76% rename from command/reload_test.go rename to command/reload/reload_test.go index 23e19125a..9571d0082 100644 --- a/command/reload_test.go +++ b/command/reload/reload_test.go @@ -1,4 +1,4 @@ -package command +package reload import ( "strings" @@ -8,9 +8,11 @@ import ( "github.com/mitchellh/cli" ) -func TestReloadCommand_implements(t *testing.T) { +func TestReloadCommand_noTabs(t *testing.T) { t.Parallel() - var _ cli.Command = &ReloadCommand{} + if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { + t.Fatal("usage has tabs") + } } func TestReloadCommandRun(t *testing.T) { @@ -25,12 +27,7 @@ func TestReloadCommandRun(t *testing.T) { }() ui := cli.NewMockUi() - c := &ReloadCommand{ - BaseCommand: BaseCommand{ - UI: ui, - Flags: FlagSetClientHTTP, - }, - } + c := New(ui) args := []string{"-http-addr=" + a.HTTPAddr()} code := c.Run(args)