diff --git a/command/commands.go b/command/commands.go index 1f5846e7fe..a3190c95e2 100644 --- a/command/commands.go +++ b/command/commands.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/consul/command/kv" "github.com/hashicorp/consul/command/kvdel" "github.com/hashicorp/consul/command/kvexp" + "github.com/hashicorp/consul/command/kvimp" "github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/version" "github.com/mitchellh/cli" @@ -147,12 +148,7 @@ func init() { }, "kv import": func() (cli.Command, error) { - return &KVImportCommand{ - BaseCommand: BaseCommand{ - Flags: FlagSetHTTP, - UI: ui, - }, - }, nil + return kvimp.New(ui), nil }, "leave": func() (cli.Command, error) { diff --git a/command/kv_import.go b/command/kvimp/kv_import.go similarity index 73% rename from command/kv_import.go rename to command/kvimp/kv_import.go index 27a0c59943..a262cbdb46 100644 --- a/command/kv_import.go +++ b/command/kvimp/kv_import.go @@ -1,64 +1,51 @@ -package command +package kvimp import ( "bytes" "encoding/base64" "encoding/json" "errors" + "flag" "fmt" "io" "io/ioutil" "os" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/command/flags" + "github.com/hashicorp/consul/command/kvimpexp" + "github.com/mitchellh/cli" ) -// KVImportCommand is a Command implementation that is used to import -// a KV tree stored as JSON -type KVImportCommand struct { - BaseCommand +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.initFlags() + return c +} + +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + http *flags.HTTPFlags // testStdin is the input for testing. testStdin io.Reader } -func (c *KVImportCommand) Synopsis() string { - return "Imports a tree stored as JSON to the KV store" -} - -func (c *KVImportCommand) Help() string { - c.InitFlagSet() - return c.HelpCommand(` -Usage: consul kv import [DATA] - - Imports key-value pairs to the key-value store from the JSON representation - generated by the "consul kv export" command. - - The data can be read from a file by prefixing the filename with the "@" - symbol. For example: - - $ consul kv import @filename.json - - Or it can be read from stdin using the "-" symbol: - - $ cat filename.json | consul kv import - - - Alternatively the data may be provided as the final parameter to the command, - though care must be taken with regards to shell escaping. - - 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 *KVImportCommand) 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 } // Check for arg validation - args = c.FlagSet.Args() + args = c.flags.Args() data, err := c.dataFromArgs(args) if err != nil { c.UI.Error(fmt.Sprintf("Error! %s", err)) @@ -66,13 +53,13 @@ func (c *KVImportCommand) 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 } - var entries []*kvExportEntry + var entries []*kvimpexp.Entry if err := json.Unmarshal([]byte(data), &entries); err != nil { c.UI.Error(fmt.Sprintf("Cannot unmarshal data: %s", err)) return 1 @@ -102,7 +89,34 @@ func (c *KVImportCommand) Run(args []string) int { return 0 } -func (c *KVImportCommand) dataFromArgs(args []string) (string, error) { +func (c *cmd) Synopsis() string { + return "Imports a tree stored as JSON to the KV store" +} + +func (c *cmd) Help() string { + s := `Usage: consul kv import [DATA] + + Imports key-value pairs to the key-value store from the JSON representation + generated by the "consul kv export" command. + + The data can be read from a file by prefixing the filename with the "@" + symbol. For example: + + $ consul kv import @filename.json + + Or it can be read from stdin using the "-" symbol: + + $ cat filename.json | consul kv import - + + Alternatively the data may be provided as the final parameter to the command, + though care must be taken with regards to shell escaping. + + 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()) +} + +func (c *cmd) dataFromArgs(args []string) (string, error) { var stdin io.Reader = os.Stdin if c.testStdin != nil { stdin = c.testStdin diff --git a/command/kv_import_test.go b/command/kvimp/kv_import_test.go similarity index 82% rename from command/kv_import_test.go rename to command/kvimp/kv_import_test.go index 86acb79a77..e0ae120adc 100644 --- a/command/kv_import_test.go +++ b/command/kvimp/kv_import_test.go @@ -1,4 +1,4 @@ -package command +package kvimp import ( "strings" @@ -8,6 +8,13 @@ import ( "github.com/mitchellh/cli" ) +func TestKVImportCommand_noTabs(t *testing.T) { + t.Parallel() + if strings.ContainsRune(New(nil).Help(), '\t') { + t.Fatal("usage has tabs") + } +} + func TestKVImportCommand_Run(t *testing.T) { t.Parallel() a := agent.NewTestAgent(t.Name(), ``) @@ -28,13 +35,8 @@ func TestKVImportCommand_Run(t *testing.T) { ]` ui := cli.NewMockUi() - c := &KVImportCommand{ - BaseCommand: BaseCommand{ - UI: ui, - Flags: FlagSetHTTP, - }, - testStdin: strings.NewReader(json), - } + c := New(ui) + c.testStdin = strings.NewReader(json) args := []string{ "-http-addr=" + a.HTTPAddr(),