From bd73c4cecf662129157afe7fd84f5d2a78217a09 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Wed, 11 Oct 2017 16:44:43 +0200 Subject: [PATCH] commands: move catalog list datacenters command to separate pkg --- .../catalog_list_datacenters.go | 65 +++++++++++-------- .../catalog_list_datacenters_test.go | 22 +++---- command/commands.go | 8 +-- 3 files changed, 48 insertions(+), 47 deletions(-) rename command/{ => catlistdc}/catalog_list_datacenters.go (53%) rename command/{ => catlistdc}/catalog_list_datacenters_test.go (76%) diff --git a/command/catalog_list_datacenters.go b/command/catlistdc/catalog_list_datacenters.go similarity index 53% rename from command/catalog_list_datacenters.go rename to command/catlistdc/catalog_list_datacenters.go index f962cccda2..86bf0adfb9 100644 --- a/command/catalog_list_datacenters.go +++ b/command/catlistdc/catalog_list_datacenters.go @@ -1,48 +1,44 @@ -package command +package catlistdc import ( + "flag" "fmt" + "github.com/hashicorp/consul/command/flags" "github.com/mitchellh/cli" ) -var _ cli.Command = (*CatalogListDatacentersCommand)(nil) - -type CatalogListDatacentersCommand struct { - BaseCommand +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.initFlags() + return c } -func (c *CatalogListDatacentersCommand) Help() string { - c.InitFlagSet() - return c.HelpCommand(` -Usage: consul catalog datacenters [options] - - Retrieves the list of all known datacenters. This datacenters are sorted in - ascending order based on the estimated median round trip time from the servers - in this datacenter to the servers in the other datacenters. - - To retrieve the list of datacenters: - - $ consul catalog datacenters - - For a full list of options and examples, please see the Consul documentation. - -`) +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + http *flags.HTTPFlags } -func (c *CatalogListDatacentersCommand) Run(args []string) int { - c.InitFlagSet() - if err := c.FlagSet.Parse(args); err != nil { +func (c *cmd) initFlags() { + c.flags = flag.NewFlagSet("", flag.ContinueOnError) + c.http = &flags.HTTPFlags{} + flags.Merge(c.flags, c.http.ClientFlags()) + flags.Merge(c.flags, c.http.ServerFlags()) +} + +func (c *cmd) Run(args []string) int { + if err := c.flags.Parse(args); err != nil { return 1 } - if l := len(c.FlagSet.Args()); l > 0 { + if l := len(c.flags.Args()); l > 0 { c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", l)) return 1 } // Create and test the HTTP client - client, err := c.HTTPClient() + client, err := c.http.APIClient() if err != nil { c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) return 1 @@ -60,6 +56,21 @@ func (c *CatalogListDatacentersCommand) Run(args []string) int { return 0 } -func (c *CatalogListDatacentersCommand) Synopsis() string { +func (c *cmd) Synopsis() string { return "Lists all known datacenters" } + +func (c *cmd) Help() string { + s := `Usage: consul catalog datacenters [options] + + Retrieves the list of all known datacenters. This datacenters are sorted in + ascending order based on the estimated median round trip time from the servers + in this datacenter to the servers in the other datacenters. + + To retrieve the list of datacenters: + + $ consul catalog datacenters + + For a full list of options and examples, please see the Consul documentation.` + return flags.Usage(s, c.flags, c.http.ClientFlags(), c.http.ServerFlags()) +} diff --git a/command/catalog_list_datacenters_test.go b/command/catlistdc/catalog_list_datacenters_test.go similarity index 76% rename from command/catalog_list_datacenters_test.go rename to command/catlistdc/catalog_list_datacenters_test.go index 821f6449e6..782bba8f52 100644 --- a/command/catalog_list_datacenters_test.go +++ b/command/catlistdc/catalog_list_datacenters_test.go @@ -1,4 +1,4 @@ -package command +package catlistdc import ( "strings" @@ -8,24 +8,17 @@ import ( "github.com/mitchellh/cli" ) -func testCatalogListDatacentersCommand(t *testing.T) (*cli.MockUi, *CatalogListDatacentersCommand) { - ui := cli.NewMockUi() - return ui, &CatalogListDatacentersCommand{ - BaseCommand: BaseCommand{ - Flags: FlagSetHTTP, - UI: ui, - }, - } -} - func TestCatalogListDatacentersCommand_noTabs(t *testing.T) { t.Parallel() - assertNoTabs(t, new(CatalogListDatacentersCommand)) + if strings.ContainsRune(New(nil).Help(), '\t') { + t.Fatal("usage has tabs") + } } func TestCatalogListDatacentersCommand_Validation(t *testing.T) { t.Parallel() - ui, c := testCatalogListDatacentersCommand(t) + ui := cli.NewMockUi() + c := New(ui) cases := map[string]struct { args []string @@ -63,7 +56,8 @@ func TestCatalogListDatacentersCommand_Run(t *testing.T) { a := agent.NewTestAgent(t.Name(), ``) defer a.Shutdown() - ui, c := testCatalogListDatacentersCommand(t) + ui := cli.NewMockUi() + c := New(ui) args := []string{ "-http-addr=" + a.HTTPAddr(), diff --git a/command/commands.go b/command/commands.go index 19773cb087..84061387e7 100644 --- a/command/commands.go +++ b/command/commands.go @@ -6,6 +6,7 @@ import ( "syscall" "github.com/hashicorp/consul/command/cat" + "github.com/hashicorp/consul/command/catlistdc" "github.com/hashicorp/consul/command/event" execmd "github.com/hashicorp/consul/command/exec" "github.com/hashicorp/consul/command/forceleave" @@ -51,12 +52,7 @@ func init() { }, "catalog datacenters": func() (cli.Command, error) { - return &CatalogListDatacentersCommand{ - BaseCommand: BaseCommand{ - Flags: FlagSetHTTP, - UI: ui, - }, - }, nil + return catlistdc.New(ui), nil }, "catalog nodes": func() (cli.Command, error) {