2017-10-17 11:22:23 +00:00
|
|
|
package imp
|
2017-01-05 00:24:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2017-10-11 12:51:28 +00:00
|
|
|
"flag"
|
2017-01-05 00:24:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2017-10-11 12:51:28 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2017-10-17 11:22:23 +00:00
|
|
|
"github.com/hashicorp/consul/command/kv/impexp"
|
2017-10-11 12:51:28 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2017-01-05 00:24:09 +00:00
|
|
|
)
|
|
|
|
|
2017-10-11 12:51:28 +00:00
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
2017-10-11 22:14:27 +00:00
|
|
|
c.init()
|
2017-10-11 12:51:28 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
2017-10-17 13:44:20 +00:00
|
|
|
help string
|
2017-01-05 00:24:09 +00:00
|
|
|
|
|
|
|
// testStdin is the input for testing.
|
|
|
|
testStdin io.Reader
|
|
|
|
}
|
|
|
|
|
2017-10-11 22:14:27 +00:00
|
|
|
func (c *cmd) init() {
|
2017-10-11 12:51:28 +00:00
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
2017-10-17 22:00:01 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
2017-01-05 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:28 +00:00
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for arg validation
|
2017-10-11 12:51:28 +00:00
|
|
|
args = c.flags.Args()
|
2017-01-05 00:24:09 +00:00
|
|
|
data, err := c.dataFromArgs(args)
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error! %s", err))
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and test the HTTP client
|
2017-10-11 12:51:28 +00:00
|
|
|
client, err := c.http.APIClient()
|
2017-01-05 00:24:09 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:22:23 +00:00
|
|
|
var entries []*impexp.Entry
|
2017-01-05 00:24:09 +00:00
|
|
|
if err := json.Unmarshal([]byte(data), &entries); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Cannot unmarshal data: %s", err))
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
value, err := base64.StdEncoding.DecodeString(entry.Value)
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error base 64 decoding value for key %s: %s", entry.Key, err))
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pair := &api.KVPair{
|
|
|
|
Key: entry.Key,
|
|
|
|
Flags: entry.Flags,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
w := api.WriteOptions{Namespace: entry.Namespace}
|
|
|
|
if _, err := client.KV().Put(pair, &w); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error! Failed writing data for key %s: %s", pair.Key, err))
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Imported: %s", pair.Key))
|
2017-01-05 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:28 +00:00
|
|
|
func (c *cmd) dataFromArgs(args []string) (string, error) {
|
2017-01-05 00:24:09 +00:00
|
|
|
var stdin io.Reader = os.Stdin
|
|
|
|
if c.testStdin != nil {
|
|
|
|
stdin = c.testStdin
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
return "", errors.New("Missing DATA argument")
|
|
|
|
case 1:
|
|
|
|
default:
|
2017-08-10 07:17:37 +00:00
|
|
|
return "", fmt.Errorf("Too many arguments (expected 1, got %d)", len(args))
|
2017-01-05 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data := args[0]
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return "", errors.New("Empty DATA argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch data[0] {
|
|
|
|
case '@':
|
|
|
|
data, err := ioutil.ReadFile(data[1:])
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read file: %s", err)
|
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
case '-':
|
|
|
|
if len(data) > 1 {
|
|
|
|
return data, nil
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
if _, err := io.Copy(&b, stdin); err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read stdin: %s", err)
|
|
|
|
}
|
|
|
|
return b.String(), nil
|
2017-01-05 00:24:09 +00:00
|
|
|
default:
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 22:14:27 +00:00
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
|
|
|
}
|
|
|
|
|
|
|
|
const synopsis = "Imports a tree stored as JSON to the KV store"
|
|
|
|
const help = `
|
|
|
|
Usage: consul kv import [DATA]
|
2017-10-11 22:14:27 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
For a full list of options and examples, please see the Consul documentation.
|
|
|
|
`
|