2017-10-11 12:51:28 +00:00
|
|
|
package kvimp
|
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"
|
|
|
|
"github.com/hashicorp/consul/command/kvimpexp"
|
|
|
|
"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-11 22:14:27 +00:00
|
|
|
usage 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-11 22:14:27 +00:00
|
|
|
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
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-11 12:51:28 +00:00
|
|
|
var entries []*kvimpexp.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,
|
|
|
|
}
|
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
if _, err := client.KV().Put(pair, nil); 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) Synopsis() string {
|
|
|
|
return "Imports a tree stored as JSON to the KV store"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
2017-10-11 22:14:27 +00:00
|
|
|
return c.usage
|
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
|
|
|
|
|
|
|
const usage = `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.`
|