commands: move kv import command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-11 14:51:28 +02:00 committed by Frank Schröder
parent cce364445e
commit e1785a0e8f
3 changed files with 65 additions and 53 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/hashicorp/consul/command/kv" "github.com/hashicorp/consul/command/kv"
"github.com/hashicorp/consul/command/kvdel" "github.com/hashicorp/consul/command/kvdel"
"github.com/hashicorp/consul/command/kvexp" "github.com/hashicorp/consul/command/kvexp"
"github.com/hashicorp/consul/command/kvimp"
"github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/command/validate"
"github.com/hashicorp/consul/version" "github.com/hashicorp/consul/version"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -147,12 +148,7 @@ func init() {
}, },
"kv import": func() (cli.Command, error) { "kv import": func() (cli.Command, error) {
return &KVImportCommand{ return kvimp.New(ui), nil
BaseCommand: BaseCommand{
Flags: FlagSetHTTP,
UI: ui,
},
}, nil
}, },
"leave": func() (cli.Command, error) { "leave": func() (cli.Command, error) {

View File

@ -1,64 +1,51 @@
package command package kvimp
import ( import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"github.com/hashicorp/consul/api" "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 func New(ui cli.Ui) *cmd {
// a KV tree stored as JSON c := &cmd{UI: ui}
type KVImportCommand struct { c.initFlags()
BaseCommand return c
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
// testStdin is the input for testing. // testStdin is the input for testing.
testStdin io.Reader testStdin io.Reader
} }
func (c *KVImportCommand) Synopsis() string { func (c *cmd) initFlags() {
return "Imports a tree stored as JSON to the KV store" 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) Help() string { func (c *cmd) Run(args []string) int {
c.InitFlagSet() if err := c.flags.Parse(args); err != nil {
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 *KVImportCommand) Run(args []string) int {
c.InitFlagSet()
if err := c.FlagSet.Parse(args); err != nil {
return 1 return 1
} }
// Check for arg validation // Check for arg validation
args = c.FlagSet.Args() args = c.flags.Args()
data, err := c.dataFromArgs(args) data, err := c.dataFromArgs(args)
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error! %s", err)) 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 // Create and test the HTTP client
client, err := c.HTTPClient() client, err := c.http.APIClient()
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1 return 1
} }
var entries []*kvExportEntry var entries []*kvimpexp.Entry
if err := json.Unmarshal([]byte(data), &entries); err != nil { if err := json.Unmarshal([]byte(data), &entries); err != nil {
c.UI.Error(fmt.Sprintf("Cannot unmarshal data: %s", err)) c.UI.Error(fmt.Sprintf("Cannot unmarshal data: %s", err))
return 1 return 1
@ -102,7 +89,34 @@ func (c *KVImportCommand) Run(args []string) int {
return 0 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 var stdin io.Reader = os.Stdin
if c.testStdin != nil { if c.testStdin != nil {
stdin = c.testStdin stdin = c.testStdin

View File

@ -1,4 +1,4 @@
package command package kvimp
import ( import (
"strings" "strings"
@ -8,6 +8,13 @@ import (
"github.com/mitchellh/cli" "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) { func TestKVImportCommand_Run(t *testing.T) {
t.Parallel() t.Parallel()
a := agent.NewTestAgent(t.Name(), ``) a := agent.NewTestAgent(t.Name(), ``)
@ -28,13 +35,8 @@ func TestKVImportCommand_Run(t *testing.T) {
]` ]`
ui := cli.NewMockUi() ui := cli.NewMockUi()
c := &KVImportCommand{ c := New(ui)
BaseCommand: BaseCommand{ c.testStdin = strings.NewReader(json)
UI: ui,
Flags: FlagSetHTTP,
},
testStdin: strings.NewReader(json),
}
args := []string{ args := []string{
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),