From f719d78441dc6077b88313ba21a95bbb65ca516a Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Tue, 17 Oct 2017 08:37:16 +0200 Subject: [PATCH] commands: move operator autopilot get command to separate pkg --- command/commands.go | 8 +-- .../operator_autopilot_get.go | 50 ++++++++++++------- .../operator_autopilot_get_test.go | 15 +++--- 3 files changed, 41 insertions(+), 32 deletions(-) rename command/{ => operautoget}/operator_autopilot_get.go (57%) rename command/{ => operautoget}/operator_autopilot_get_test.go (70%) diff --git a/command/commands.go b/command/commands.go index 1d4fa2c159..af0e709c33 100644 --- a/command/commands.go +++ b/command/commands.go @@ -29,6 +29,7 @@ import ( "github.com/hashicorp/consul/command/monitor" "github.com/hashicorp/consul/command/oper" "github.com/hashicorp/consul/command/operauto" + "github.com/hashicorp/consul/command/operautoget" "github.com/hashicorp/consul/command/operraft" "github.com/hashicorp/consul/command/operraftlist" "github.com/hashicorp/consul/command/operraftremove" @@ -155,12 +156,7 @@ func init() { }, "operator autopilot get-config": func() (cli.Command, error) { - return &OperatorAutopilotGetCommand{ - BaseCommand: BaseCommand{ - Flags: FlagSetHTTP, - UI: ui, - }, - }, nil + return operautoget.New(ui), nil }, "operator autopilot set-config": func() (cli.Command, error) { diff --git a/command/operator_autopilot_get.go b/command/operautoget/operator_autopilot_get.go similarity index 57% rename from command/operator_autopilot_get.go rename to command/operautoget/operator_autopilot_get.go index 7a98beac27..37c1b711be 100644 --- a/command/operator_autopilot_get.go +++ b/command/operautoget/operator_autopilot_get.go @@ -1,33 +1,45 @@ -package command +package operautoget import ( "flag" "fmt" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/command/flags" + "github.com/mitchellh/cli" ) -type OperatorAutopilotGetCommand struct { - BaseCommand +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.init() + return c } -func (c *OperatorAutopilotGetCommand) Help() string { - c.InitFlagSet() - return c.HelpCommand(` -Usage: consul operator autopilot get-config [options] - -Displays the current Autopilot configuration. - -`) +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + http *flags.HTTPFlags + usage string } -func (c *OperatorAutopilotGetCommand) Synopsis() 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 "Display the current Autopilot configuration" } -func (c *OperatorAutopilotGetCommand) Run(args []string) int { - c.InitFlagSet() - if err := c.FlagSet.Parse(args); err != nil { +func (c *cmd) Help() string { + return c.usage +} + +func (c *cmd) Run(args []string) int { + if err := c.flags.Parse(args); err != nil { if err == flag.ErrHelp { return 0 } @@ -36,7 +48,7 @@ func (c *OperatorAutopilotGetCommand) Run(args []string) int { } // Set up a client. - client, err := c.HTTPClient() + client, err := c.http.APIClient() if err != nil { c.UI.Error(fmt.Sprintf("Error initializing client: %s", err)) return 1 @@ -44,7 +56,7 @@ func (c *OperatorAutopilotGetCommand) Run(args []string) int { // Fetch the current configuration. opts := &api.QueryOptions{ - AllowStale: c.HTTPStale(), + AllowStale: c.http.Stale(), } config, err := client.Operator().AutopilotGetConfiguration(opts) if err != nil { @@ -61,3 +73,7 @@ func (c *OperatorAutopilotGetCommand) Run(args []string) int { return 0 } + +const usage = `Usage: consul operator autopilot get-config [options] + +Displays the current Autopilot configuration.` diff --git a/command/operator_autopilot_get_test.go b/command/operautoget/operator_autopilot_get_test.go similarity index 70% rename from command/operator_autopilot_get_test.go rename to command/operautoget/operator_autopilot_get_test.go index e89f232c61..d53486d9bf 100644 --- a/command/operator_autopilot_get_test.go +++ b/command/operautoget/operator_autopilot_get_test.go @@ -1,4 +1,4 @@ -package command +package operautoget import ( "strings" @@ -8,9 +8,11 @@ import ( "github.com/mitchellh/cli" ) -func TestOperator_Autopilot_Get_Implements(t *testing.T) { +func TestOperatorAutopilotGetCommand_noTabs(t *testing.T) { t.Parallel() - var _ cli.Command = &OperatorAutopilotGetCommand{} + if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { + t.Fatal("usage has tabs") + } } func TestOperator_Autopilot_Get(t *testing.T) { @@ -19,12 +21,7 @@ func TestOperator_Autopilot_Get(t *testing.T) { defer a.Shutdown() ui := cli.NewMockUi() - c := OperatorAutopilotGetCommand{ - BaseCommand: BaseCommand{ - UI: ui, - Flags: FlagSetHTTP, - }, - } + c := New(ui) args := []string{"-http-addr=" + a.HTTPAddr()} code := c.Run(args)