From 2a824f0575f77a67e5d480e1dc1af41f1d362502 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Tue, 17 Oct 2017 08:19:25 +0200 Subject: [PATCH] commands: move operator raft list-peers command to separate pkg --- command/commands.go | 8 +-- command/operator_raft_list_test.go | 45 ---------------- .../{ => operraftlist}/operator_raft_list.go | 51 ++++++++++++------- .../operraftlist/operator_raft_list_test.go | 40 +++++++++++++++ 4 files changed, 75 insertions(+), 69 deletions(-) delete mode 100644 command/operator_raft_list_test.go rename command/{ => operraftlist}/operator_raft_list.go (58%) create mode 100644 command/operraftlist/operator_raft_list_test.go diff --git a/command/commands.go b/command/commands.go index 8e7f79a808..634b2491a2 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/operraft" + "github.com/hashicorp/consul/command/operraftlist" "github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/version" "github.com/mitchellh/cli" @@ -179,12 +180,7 @@ func init() { }, "operator raft list-peers": func() (cli.Command, error) { - return &OperatorRaftListCommand{ - BaseCommand: BaseCommand{ - Flags: FlagSetHTTP, - UI: ui, - }, - }, nil + return operraftlist.New(ui), nil }, "operator raft remove-peer": func() (cli.Command, error) { diff --git a/command/operator_raft_list_test.go b/command/operator_raft_list_test.go deleted file mode 100644 index b36b20bd49..0000000000 --- a/command/operator_raft_list_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package command - -import ( - "fmt" - "strings" - "testing" - - "github.com/hashicorp/consul/agent" - "github.com/mitchellh/cli" -) - -func TestOperator_Raft_ListPeers_Implements(t *testing.T) { - t.Parallel() - var _ cli.Command = &OperatorRaftListCommand{} -} - -func TestOperator_Raft_ListPeers(t *testing.T) { - t.Parallel() - a := agent.NewTestAgent(t.Name(), ``) - defer a.Shutdown() - - expected := fmt.Sprintf("%s %s 127.0.0.1:%d leader true 3", - a.Config.NodeName, a.Config.NodeID, a.Config.ServerPort) - - // Test the list-peers subcommand directly - { - ui := cli.NewMockUi() - c := OperatorRaftListCommand{ - BaseCommand: BaseCommand{ - UI: ui, - Flags: FlagSetHTTP, - }, - } - args := []string{"-http-addr=" + a.HTTPAddr()} - - code := c.Run(args) - if code != 0 { - t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) - } - output := strings.TrimSpace(ui.OutputWriter.String()) - if !strings.Contains(output, expected) { - t.Fatalf("bad: %q, %q", output, expected) - } - } -} diff --git a/command/operator_raft_list.go b/command/operraftlist/operator_raft_list.go similarity index 58% rename from command/operator_raft_list.go rename to command/operraftlist/operator_raft_list.go index 1325b72c9f..b6e71c7861 100644 --- a/command/operator_raft_list.go +++ b/command/operraftlist/operator_raft_list.go @@ -1,34 +1,46 @@ -package command +package operraftlist import ( "flag" "fmt" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/command/flags" + "github.com/mitchellh/cli" "github.com/ryanuber/columnize" ) -type OperatorRaftListCommand struct { - BaseCommand +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.init() + return c } -func (c *OperatorRaftListCommand) Help() string { - c.InitFlagSet() - return c.HelpCommand(` -Usage: consul operator raft list-peers [options] - -Displays the current Raft peer configuration. - -`) +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + http *flags.HTTPFlags + usage string } -func (c *OperatorRaftListCommand) 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 Raft peer configuration" } -func (c *OperatorRaftListCommand) 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 } @@ -37,14 +49,14 @@ func (c *OperatorRaftListCommand) 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 } // Fetch the current configuration. - result, err := raftListPeers(client, c.HTTPStale()) + result, err := raftListPeers(client, c.http.Stale()) if err != nil { c.UI.Error(fmt.Sprintf("Error getting peers: %v", err)) return 1 @@ -55,7 +67,6 @@ func (c *OperatorRaftListCommand) Run(args []string) int { } func raftListPeers(client *api.Client, stale bool) (string, error) { - q := &api.QueryOptions{ AllowStale: stale, } @@ -82,3 +93,7 @@ func raftListPeers(client *api.Client, stale bool) (string, error) { return columnize.SimpleFormat(result), nil } + +const usage = `Usage: consul operator raft list-peers [options] + +Displays the current Raft peer configuration.` diff --git a/command/operraftlist/operator_raft_list_test.go b/command/operraftlist/operator_raft_list_test.go new file mode 100644 index 0000000000..6098b5db50 --- /dev/null +++ b/command/operraftlist/operator_raft_list_test.go @@ -0,0 +1,40 @@ +package operraftlist + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/consul/agent" + "github.com/mitchellh/cli" +) + +func TestOperatorRaftListPeersCommand_noTabs(t *testing.T) { + t.Parallel() + if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { + t.Fatal("usage has tabs") + } +} + +func TestOperatorRaftListPeersCommandRun(t *testing.T) { + t.Parallel() + a := agent.NewTestAgent(t.Name(), ``) + defer a.Shutdown() + + expected := fmt.Sprintf("%s %s 127.0.0.1:%d leader true 3", + a.Config.NodeName, a.Config.NodeID, a.Config.ServerPort) + + // Test the list-peers subcommand directly + ui := cli.NewMockUi() + c := New(ui) + args := []string{"-http-addr=" + a.HTTPAddr()} + + code := c.Run(args) + if code != 0 { + t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) + } + output := strings.TrimSpace(ui.OutputWriter.String()) + if !strings.Contains(output, expected) { + t.Fatalf("bad: %q, %q", output, expected) + } +}