From 3acbc278f0cdc87bf3edffc245dc43b05b710b8b Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Tue, 17 Oct 2017 10:06:16 +0200 Subject: [PATCH] commands: move version command to separate pkg --- command/commands.go | 6 ++---- command/{ => version}/version.go | 27 +++++++++++++++------------ command/version/version_test.go | 15 +++++++++++++++ command/version_test.go | 12 ------------ 4 files changed, 32 insertions(+), 28 deletions(-) rename command/{ => version}/version.go (66%) create mode 100644 command/version/version_test.go delete mode 100644 command/version_test.go diff --git a/command/commands.go b/command/commands.go index c15ce0509b..21b7e1f468 100644 --- a/command/commands.go +++ b/command/commands.go @@ -39,6 +39,7 @@ import ( "github.com/hashicorp/consul/command/snapshot" "github.com/hashicorp/consul/command/snapshotinspect" "github.com/hashicorp/consul/command/validate" + versioncmd "github.com/hashicorp/consul/command/version" "github.com/hashicorp/consul/version" "github.com/mitchellh/cli" ) @@ -219,10 +220,7 @@ func init() { }, "version": func() (cli.Command, error) { - return &VersionCommand{ - HumanVersion: version.GetHumanVersion(), - UI: ui, - }, nil + return versioncmd.New(ui, version.GetHumanVersion()), nil }, "watch": func() (cli.Command, error) { diff --git a/command/version.go b/command/version/version.go similarity index 66% rename from command/version.go rename to command/version/version.go index 4fcda5693f..d21d1a05ac 100644 --- a/command/version.go +++ b/command/version/version.go @@ -1,4 +1,4 @@ -package command +package version import ( "fmt" @@ -8,18 +8,25 @@ import ( "github.com/mitchellh/cli" ) -// VersionCommand is a Command implementation prints the version. -type VersionCommand struct { - HumanVersion string - UI cli.Ui +func New(ui cli.Ui, version string) *cmd { + return &cmd{UI: ui, version: version} } -func (c *VersionCommand) Help() string { +type cmd struct { + UI cli.Ui + version string +} + +func (c *cmd) Synopsis() string { + return "Prints the Consul version" +} + +func (c *cmd) Help() string { return "" } -func (c *VersionCommand) Run(_ []string) int { - c.UI.Output(fmt.Sprintf("Consul %s", c.HumanVersion)) +func (c *cmd) Run(_ []string) int { + c.UI.Output(fmt.Sprintf("Consul %s", c.version)) rpcProtocol, err := config.DefaultRPCProtocol() if err != nil { @@ -36,7 +43,3 @@ func (c *VersionCommand) Run(_ []string) int { return 0 } - -func (c *VersionCommand) Synopsis() string { - return "Prints the Consul version" -} diff --git a/command/version/version_test.go b/command/version/version_test.go new file mode 100644 index 0000000000..54cbd20cc0 --- /dev/null +++ b/command/version/version_test.go @@ -0,0 +1,15 @@ +package version + +import ( + "strings" + "testing" + + "github.com/mitchellh/cli" +) + +func TestVersionCommand_noTabs(t *testing.T) { + t.Parallel() + if strings.ContainsRune(New(cli.NewMockUi(), "").Help(), '\t') { + t.Fatal("usage has tabs") + } +} diff --git a/command/version_test.go b/command/version_test.go deleted file mode 100644 index 07af3a0069..0000000000 --- a/command/version_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package command - -import ( - "testing" - - "github.com/mitchellh/cli" -) - -func TestVersionCommand_implements(t *testing.T) { - t.Parallel() - var _ cli.Command = &VersionCommand{} -}