From 3a02ce9ebc2ff5872162bc2eefd5fdccd32185f2 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Tue, 17 Oct 2017 09:03:41 +0200 Subject: [PATCH] commands: move snapshot inspect command to separate pkg --- command/commands.go | 10 +-- .../{ => snapshotinspect}/snapshot_inspect.go | 62 +++++++++++-------- .../snapshot_inspect_test.go | 29 +++------ 3 files changed, 49 insertions(+), 52 deletions(-) rename command/{ => snapshotinspect}/snapshot_inspect.go (67%) rename command/{ => snapshotinspect}/snapshot_inspect_test.go (78%) diff --git a/command/commands.go b/command/commands.go index b08f9a66d7..c15ce0509b 100644 --- a/command/commands.go +++ b/command/commands.go @@ -36,8 +36,9 @@ import ( "github.com/hashicorp/consul/command/operraftremove" "github.com/hashicorp/consul/command/reload" "github.com/hashicorp/consul/command/rtt" + "github.com/hashicorp/consul/command/snapshot" + "github.com/hashicorp/consul/command/snapshotinspect" "github.com/hashicorp/consul/command/validate" - "github.com/hashicorp/consul/snapshot" "github.com/hashicorp/consul/version" "github.com/mitchellh/cli" ) @@ -210,12 +211,7 @@ func init() { }, "snapshot inspect": func() (cli.Command, error) { - return &SnapshotInspectCommand{ - BaseCommand: BaseCommand{ - Flags: FlagSetNone, - UI: ui, - }, - }, nil + return snapshotinspect.New(ui), nil }, "validate": func() (cli.Command, error) { diff --git a/command/snapshot_inspect.go b/command/snapshotinspect/snapshot_inspect.go similarity index 67% rename from command/snapshot_inspect.go rename to command/snapshotinspect/snapshot_inspect.go index a26041f558..0276e259c4 100644 --- a/command/snapshot_inspect.go +++ b/command/snapshotinspect/snapshot_inspect.go @@ -1,44 +1,50 @@ -package command +package snapshotinspect import ( "bytes" + "flag" "fmt" "os" "text/tabwriter" + "github.com/hashicorp/consul/command/flags" "github.com/hashicorp/consul/snapshot" + "github.com/mitchellh/cli" ) -// SnapshotInspectCommand is a Command implementation that is used to display -// metadata about a snapshot file -type SnapshotInspectCommand struct { - BaseCommand +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.init() + return c } -func (c *SnapshotInspectCommand) Help() string { - c.InitFlagSet() - return c.HelpCommand(` -Usage: consul snapshot inspect [options] FILE - - Displays information about a snapshot file on disk. - - To inspect the file "backup.snap": - - $ consul snapshot inspect backup.snap - - For a full list of options and examples, please see the Consul documentation. -`) +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + usage string } -func (c *SnapshotInspectCommand) Run(args []string) int { - c.InitFlagSet() - if err := c.FlagSet.Parse(args); err != nil { +func (c *cmd) init() { + c.flags = flag.NewFlagSet("", flag.ContinueOnError) + c.usage = flags.Usage(usage, c.flags, nil, nil) +} + +func (c *cmd) Synopsis() string { + return "Displays information about a Consul snapshot file" +} + +func (c *cmd) Help() string { + return c.usage +} + +func (c *cmd) Run(args []string) int { + if err := c.flags.Parse(args); err != nil { return 1 } var file string - args = c.FlagSet.Args() + args = c.flags.Args() switch len(args) { case 0: c.UI.Error("Missing FILE argument") @@ -79,6 +85,12 @@ func (c *SnapshotInspectCommand) Run(args []string) int { return 0 } -func (c *SnapshotInspectCommand) Synopsis() string { - return "Displays information about a Consul snapshot file" -} +const usage = `Usage: consul snapshot inspect [options] FILE + + Displays information about a snapshot file on disk. + + To inspect the file "backup.snap": + + $ consul snapshot inspect backup.snap + + For a full list of options and examples, please see the Consul documentation.` diff --git a/command/snapshot_inspect_test.go b/command/snapshotinspect/snapshot_inspect_test.go similarity index 78% rename from command/snapshot_inspect_test.go rename to command/snapshotinspect/snapshot_inspect_test.go index d6dd4cc6c1..3bcc43c830 100644 --- a/command/snapshot_inspect_test.go +++ b/command/snapshotinspect/snapshot_inspect_test.go @@ -1,4 +1,4 @@ -package command +package snapshotinspect import ( "io" @@ -12,29 +12,17 @@ import ( "github.com/mitchellh/cli" ) -func testSnapshotInspectCommand(t *testing.T) (*cli.MockUi, *SnapshotInspectCommand) { - ui := cli.NewMockUi() - return ui, &SnapshotInspectCommand{ - BaseCommand: BaseCommand{ - UI: ui, - Flags: FlagSetNone, - }, +func TestSnapshotInpectCommand_noTabs(t *testing.T) { + t.Parallel() + if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { + t.Fatal("usage has tabs") } } -func TestSnapshotInspectCommand_implements(t *testing.T) { - t.Parallel() - var _ cli.Command = &SnapshotInspectCommand{} -} - -func TestSnapshotInspectCommand_noTabs(t *testing.T) { - t.Parallel() - assertNoTabs(t, new(SnapshotInspectCommand)) -} - func TestSnapshotInspectCommand_Validation(t *testing.T) { t.Parallel() - ui, c := testSnapshotInspectCommand(t) + ui := cli.NewMockUi() + c := New(ui) cases := map[string]struct { args []string @@ -102,7 +90,8 @@ func TestSnapshotInspectCommand_Run(t *testing.T) { } // Inspect the snapshot - ui, c := testSnapshotInspectCommand(t) + ui := cli.NewMockUi() + c := New(ui) args := []string{file} code := c.Run(args)