|
|
|
@ -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
|