2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-10-17 11:22:23 +00:00
|
|
|
package exp
|
2017-01-05 00:24:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-10-11 12:51:27 +00:00
|
|
|
"flag"
|
2017-01-05 00:24:09 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2017-10-11 12:51:27 +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:27 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2017-01-05 00:24:09 +00:00
|
|
|
)
|
|
|
|
|
2017-10-11 12:51:27 +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:27 +00:00
|
|
|
return c
|
2017-01-05 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-11 22:14:27 +00:00
|
|
|
func (c *cmd) init() {
|
2017-10-11 12:51:27 +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())
|
2021-07-21 19:45:24 +00:00
|
|
|
flags.Merge(c.flags, c.http.MultiTenancyFlags())
|
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:27 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
key := ""
|
|
|
|
// Check for arg validation
|
2017-10-11 12:51:27 +00:00
|
|
|
args = c.flags.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-10-11 12:51:27 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
pairs, _, err := client.KV().List(key, &api.QueryOptions{
|
2017-10-11 12:51:27 +00:00
|
|
|
AllowStale: c.http.Stale(),
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:22:23 +00:00
|
|
|
exported := make([]*impexp.Entry, len(pairs))
|
2017-01-05 00:24:09 +00:00
|
|
|
for i, pair := range pairs {
|
2017-10-17 11:22:23 +00:00
|
|
|
exported[i] = impexp.ToEntry(pair)
|
2017-01-05 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:27 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
2017-10-17 13:44:20 +00:00
|
|
|
return synopsis
|
2017-10-11 12:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
2017-10-17 13:44:20 +00:00
|
|
|
return c.help
|
2017-10-11 22:14:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 19:45:24 +00:00
|
|
|
const (
|
|
|
|
synopsis = "Exports a tree from the KV store as JSON"
|
|
|
|
help = `
|
2017-10-17 13:44:20 +00:00
|
|
|
Usage: consul kv export [KEY_OR_PREFIX]
|
2017-10-11 12:51:27 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
For a full list of options and examples, please see the Consul documentation.
|
|
|
|
`
|
2021-07-21 19:45:24 +00:00
|
|
|
)
|