From b1c5ee39a53b8f8de8bba3c6f55d678b1fc0e599 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Wed, 11 Oct 2017 14:51:24 +0200 Subject: [PATCH] commands: move info command to separate pkg --- command/commands.go | 8 ++--- command/{ => info}/info.go | 53 ++++++++++++++++++++------------- command/{ => info}/info_test.go | 18 +++++------ 3 files changed, 42 insertions(+), 37 deletions(-) rename command/{ => info}/info.go (61%) rename command/{ => info}/info_test.go (67%) diff --git a/command/commands.go b/command/commands.go index 2c9a2e5f4e..1a69005db4 100644 --- a/command/commands.go +++ b/command/commands.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/consul/command/event" execmd "github.com/hashicorp/consul/command/exec" + "github.com/hashicorp/consul/command/info" "github.com/hashicorp/consul/command/join" "github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/version" @@ -91,12 +92,7 @@ func init() { }, "info": func() (cli.Command, error) { - return &InfoCommand{ - BaseCommand: BaseCommand{ - Flags: FlagSetClientHTTP, - UI: ui, - }, - }, nil + return info.New(ui), nil }, "join": func() (cli.Command, error) { diff --git a/command/info.go b/command/info/info.go similarity index 61% rename from command/info.go rename to command/info/info.go index e94569a883..54fe7fb623 100644 --- a/command/info.go +++ b/command/info/info.go @@ -1,23 +1,38 @@ -package command +package info import ( + "flag" "fmt" "sort" + + "github.com/hashicorp/consul/command/flags" + "github.com/mitchellh/cli" ) -// InfoCommand is a Command implementation that queries a running -// Consul agent for various debugging statistics for operators -type InfoCommand struct { - BaseCommand +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.initFlags() + return c } -func (c *InfoCommand) Run(args []string) int { - c.InitFlagSet() - if err := c.FlagSet.Parse(args); err != nil { +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + http *flags.HTTPFlags +} + +func (c *cmd) initFlags() { + c.flags = flag.NewFlagSet("", flag.ContinueOnError) + c.http = &flags.HTTPFlags{} + flags.Merge(c.flags, c.http.ClientFlags()) +} + +func (c *cmd) Run(args []string) int { + if err := c.flags.Parse(args); err != nil { return 1 } - 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 @@ -66,16 +81,14 @@ func (c *InfoCommand) Run(args []string) int { return 0 } -func (c *InfoCommand) Help() string { - c.InitFlagSet() - return c.HelpCommand(` -Usage: consul info [options] - - Provides debugging information for operators - -`) -} - -func (c *InfoCommand) Synopsis() string { +func (c *cmd) Synopsis() string { return "Provides debugging information for operators." } + +func (c *cmd) Help() string { + s := `Usage: consul info [options] + + Provides debugging information for operators` + + return flags.Usage(s, c.flags, c.http.ClientFlags(), nil) +} diff --git a/command/info_test.go b/command/info/info_test.go similarity index 67% rename from command/info_test.go rename to command/info/info_test.go index 483d000b66..b52c06ff73 100644 --- a/command/info_test.go +++ b/command/info/info_test.go @@ -1,4 +1,4 @@ -package command +package info import ( "strings" @@ -8,9 +8,10 @@ import ( "github.com/mitchellh/cli" ) -func TestInfoCommand_implements(t *testing.T) { - t.Parallel() - var _ cli.Command = &InfoCommand{} +func TestInfoCommand_noTabs(t *testing.T) { + if strings.ContainsRune(New(nil).Help(), '\t') { + t.Fatal("usage has tabs") + } } func TestInfoCommandRun(t *testing.T) { @@ -19,15 +20,10 @@ func TestInfoCommandRun(t *testing.T) { defer a1.Shutdown() ui := cli.NewMockUi() - c := &InfoCommand{ - BaseCommand: BaseCommand{ - UI: ui, - Flags: FlagSetClientHTTP, - }, - } + cmd := New(ui) args := []string{"-http-addr=" + a1.HTTPAddr()} - code := c.Run(args) + code := cmd.Run(args) if code != 0 { t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) }