2017-01-05 00:24:09 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2017-02-09 00:26:24 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2017-01-05 00:24:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// KVExportCommand is a Command implementation that is used to export
|
|
|
|
// a KV tree as JSON
|
|
|
|
type KVExportCommand struct {
|
2017-02-09 00:26:24 +00:00
|
|
|
base.Command
|
2017-01-05 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KVExportCommand) Synopsis() string {
|
|
|
|
return "Exports a tree from the KV store as JSON"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KVExportCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: consul kv export [KEY_OR_PREFIX]
|
|
|
|
|
|
|
|
Retrieves key-value pairs for the given prefix from Consul's key-value store,
|
|
|
|
and writes a JSON representation to stdout. This can be used with the command
|
|
|
|
"consul kv import" to move entire trees between Consul clusters.
|
|
|
|
|
|
|
|
$ consul kv export vault
|
|
|
|
|
|
|
|
For a full list of options and examples, please see the Consul documentation.
|
|
|
|
|
2017-02-09 00:26:24 +00:00
|
|
|
` + c.Command.Help()
|
2017-01-05 00:24:09 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KVExportCommand) Run(args []string) int {
|
2017-02-09 00:26:24 +00:00
|
|
|
f := c.Command.NewFlagSet(c)
|
|
|
|
if err := c.Command.Parse(args); err != nil {
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
key := ""
|
|
|
|
// Check for arg validation
|
2017-02-09 00:26:24 +00:00
|
|
|
args = f.Args()
|
2017-01-05 00:24:09 +00:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
key = ""
|
|
|
|
case 1:
|
|
|
|
key = args[0]
|
|
|
|
default:
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args)))
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is just a "nice" thing to do. Since pairs cannot start with a /, but
|
|
|
|
// users will likely put "/" or "/foo", lets go ahead and strip that for them
|
|
|
|
// here.
|
|
|
|
if len(key) > 0 && key[0] == '/' {
|
|
|
|
key = key[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and test the HTTP client
|
2017-02-09 00:26:24 +00:00
|
|
|
client, err := c.Command.HTTPClient()
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pairs, _, err := client.KV().List(key, &api.QueryOptions{
|
2017-02-09 00:26:24 +00:00
|
|
|
AllowStale: c.Command.HTTPStale(),
|
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 querying Consul agent: %s", err))
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
exported := make([]*kvExportEntry, len(pairs))
|
|
|
|
for i, pair := range pairs {
|
|
|
|
exported[i] = toExportEntry(pair)
|
|
|
|
}
|
|
|
|
|
|
|
|
marshaled, err := json.MarshalIndent(exported, "", "\t")
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error exporting KV data: %s", err))
|
2017-01-05 00:24:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(string(marshaled))
|
2017-01-05 00:24:09 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
type kvExportEntry struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Flags uint64 `json:"flags"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func toExportEntry(pair *api.KVPair) *kvExportEntry {
|
|
|
|
return &kvExportEntry{
|
|
|
|
Key: pair.Key,
|
|
|
|
Flags: pair.Flags,
|
|
|
|
Value: base64.StdEncoding.EncodeToString(pair.Value),
|
|
|
|
}
|
|
|
|
}
|