diff --git a/command/commands.go b/command/commands.go index 20ccd45935..1f5846e7fe 100644 --- a/command/commands.go +++ b/command/commands.go @@ -15,6 +15,7 @@ import ( "github.com/hashicorp/consul/command/keygen" "github.com/hashicorp/consul/command/kv" "github.com/hashicorp/consul/command/kvdel" + "github.com/hashicorp/consul/command/kvexp" "github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/version" "github.com/mitchellh/cli" @@ -142,12 +143,7 @@ func init() { }, "kv export": func() (cli.Command, error) { - return &KVExportCommand{ - BaseCommand: BaseCommand{ - Flags: FlagSetHTTP, - UI: ui, - }, - }, nil + return kvexp.New(ui), nil }, "kv import": func() (cli.Command, error) { diff --git a/command/kv_export.go b/command/kvexp/kv_export.go similarity index 69% rename from command/kv_export.go rename to command/kvexp/kv_export.go index e3ef23b411..e2565180e9 100644 --- a/command/kv_export.go +++ b/command/kvexp/kv_export.go @@ -1,48 +1,43 @@ -package command +package kvexp import ( "encoding/base64" "encoding/json" + "flag" "fmt" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/command/flags" + "github.com/mitchellh/cli" ) -// KVExportCommand is a Command implementation that is used to export -// a KV tree as JSON -type KVExportCommand struct { - BaseCommand +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.initFlags() + return c } -func (c *KVExportCommand) Synopsis() string { - return "Exports a tree from the KV store as JSON" +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + http *flags.HTTPFlags } -func (c *KVExportCommand) Help() string { - c.InitFlagSet() - return c.HelpCommand(` -Usage: consul kv export [KEY_OR_PREFIX] - - Retrieves key-value pairs for the given prefix from Consul's key-value store, - and writes a JSON representation to stdout. This can be used with the command - "consul kv import" to move entire trees between Consul clusters. - - $ consul kv export vault - - For a full list of options and examples, please see the Consul documentation. - -`) +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 *KVExportCommand) Run(args []string) int { - c.InitFlagSet() - if err := c.FlagSet.Parse(args); err != nil { +func (c *cmd) Run(args []string) int { + if err := c.flags.Parse(args); err != nil { return 1 } key := "" // Check for arg validation - args = c.FlagSet.Args() + args = c.flags.Args() switch len(args) { case 0: key = "" @@ -61,14 +56,14 @@ func (c *KVExportCommand) Run(args []string) int { } // 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 } pairs, _, err := client.KV().List(key, &api.QueryOptions{ - AllowStale: c.HTTPStale(), + AllowStale: c.http.Stale(), }) if err != nil { c.UI.Error(fmt.Sprintf("Error querying Consul agent: %s", err)) @@ -91,6 +86,24 @@ func (c *KVExportCommand) Run(args []string) int { return 0 } +func (c *cmd) Synopsis() string { + return "Exports a tree from the KV store as JSON" +} + +func (c *cmd) Help() string { + s := `Usage: consul kv export [KEY_OR_PREFIX] + + Retrieves key-value pairs for the given prefix from Consul's key-value store, + and writes a JSON representation to stdout. This can be used with the command + "consul kv import" to move entire trees between Consul clusters. + + $ consul kv export vault + + 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()) +} + type kvExportEntry struct { Key string `json:"key"` Flags uint64 `json:"flags"` diff --git a/command/kv_export_test.go b/command/kvexp/kv_export_test.go similarity index 81% rename from command/kv_export_test.go rename to command/kvexp/kv_export_test.go index 0bc2e2aa6a..7ebf1f51ba 100644 --- a/command/kv_export_test.go +++ b/command/kvexp/kv_export_test.go @@ -1,15 +1,24 @@ -package command +package kvexp import ( "encoding/base64" "encoding/json" + "strings" "testing" "github.com/hashicorp/consul/agent" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/command/kvimpexp" "github.com/mitchellh/cli" ) +func TestKVExportCommand_noTabs(t *testing.T) { + t.Parallel() + if strings.ContainsRune(New(nil).Help(), '\t') { + t.Fatal("usage has tabs") + } +} + func TestKVExportCommand_Run(t *testing.T) { t.Parallel() a := agent.NewTestAgent(t.Name(), ``) @@ -17,12 +26,7 @@ func TestKVExportCommand_Run(t *testing.T) { client := a.Client() ui := cli.NewMockUi() - c := KVExportCommand{ - BaseCommand: BaseCommand{ - UI: ui, - Flags: FlagSetHTTP, - }, - } + c := New(ui) keys := map[string]string{ "foo/a": "a", @@ -49,7 +53,7 @@ func TestKVExportCommand_Run(t *testing.T) { output := ui.OutputWriter.String() - var exported []*kvExportEntry + var exported []*kvimpexp.Entry err := json.Unmarshal([]byte(output), &exported) if err != nil { t.Fatalf("bad: %d", code)