mirror of https://github.com/status-im/consul.git
commands: move kv export command to separate pkg
This commit is contained in:
parent
5e3371eee1
commit
585b5b8d4e
|
@ -15,6 +15,7 @@ import (
|
||||||
"github.com/hashicorp/consul/command/keygen"
|
"github.com/hashicorp/consul/command/keygen"
|
||||||
"github.com/hashicorp/consul/command/kv"
|
"github.com/hashicorp/consul/command/kv"
|
||||||
"github.com/hashicorp/consul/command/kvdel"
|
"github.com/hashicorp/consul/command/kvdel"
|
||||||
|
"github.com/hashicorp/consul/command/kvexp"
|
||||||
"github.com/hashicorp/consul/command/validate"
|
"github.com/hashicorp/consul/command/validate"
|
||||||
"github.com/hashicorp/consul/version"
|
"github.com/hashicorp/consul/version"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
|
@ -142,12 +143,7 @@ func init() {
|
||||||
},
|
},
|
||||||
|
|
||||||
"kv export": func() (cli.Command, error) {
|
"kv export": func() (cli.Command, error) {
|
||||||
return &KVExportCommand{
|
return kvexp.New(ui), nil
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
Flags: FlagSetHTTP,
|
|
||||||
UI: ui,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"kv import": func() (cli.Command, error) {
|
"kv import": func() (cli.Command, error) {
|
||||||
|
|
|
@ -1,48 +1,43 @@
|
||||||
package command
|
package kvexp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/api"
|
"github.com/hashicorp/consul/api"
|
||||||
|
"github.com/hashicorp/consul/command/flags"
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
// KVExportCommand is a Command implementation that is used to export
|
func New(ui cli.Ui) *cmd {
|
||||||
// a KV tree as JSON
|
c := &cmd{UI: ui}
|
||||||
type KVExportCommand struct {
|
c.initFlags()
|
||||||
BaseCommand
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *KVExportCommand) Synopsis() string {
|
type cmd struct {
|
||||||
return "Exports a tree from the KV store as JSON"
|
UI cli.Ui
|
||||||
|
flags *flag.FlagSet
|
||||||
|
http *flags.HTTPFlags
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *KVExportCommand) Help() string {
|
func (c *cmd) initFlags() {
|
||||||
c.InitFlagSet()
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
return c.HelpCommand(`
|
c.http = &flags.HTTPFlags{}
|
||||||
Usage: consul kv export [KEY_OR_PREFIX]
|
flags.Merge(c.flags, c.http.ClientFlags())
|
||||||
|
flags.Merge(c.flags, c.http.ServerFlags())
|
||||||
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.
|
|
||||||
|
|
||||||
`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *KVExportCommand) Run(args []string) int {
|
func (c *cmd) Run(args []string) int {
|
||||||
c.InitFlagSet()
|
if err := c.flags.Parse(args); err != nil {
|
||||||
if err := c.FlagSet.Parse(args); err != nil {
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
key := ""
|
key := ""
|
||||||
// Check for arg validation
|
// Check for arg validation
|
||||||
args = c.FlagSet.Args()
|
args = c.flags.Args()
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 0:
|
case 0:
|
||||||
key = ""
|
key = ""
|
||||||
|
@ -61,14 +56,14 @@ func (c *KVExportCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and test the HTTP client
|
// Create and test the HTTP client
|
||||||
client, err := c.HTTPClient()
|
client, err := c.http.APIClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
pairs, _, err := client.KV().List(key, &api.QueryOptions{
|
pairs, _, err := client.KV().List(key, &api.QueryOptions{
|
||||||
AllowStale: c.HTTPStale(),
|
AllowStale: c.http.Stale(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.UI.Error(fmt.Sprintf("Error querying Consul agent: %s", err))
|
c.UI.Error(fmt.Sprintf("Error querying Consul agent: %s", err))
|
||||||
|
@ -91,6 +86,24 @@ func (c *KVExportCommand) Run(args []string) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Synopsis() string {
|
||||||
|
return "Exports a tree from the KV store as JSON"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Help() string {
|
||||||
|
s := `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.`
|
||||||
|
|
||||||
|
return flags.Usage(s, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||||
|
}
|
||||||
|
|
||||||
type kvExportEntry struct {
|
type kvExportEntry struct {
|
||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
Flags uint64 `json:"flags"`
|
Flags uint64 `json:"flags"`
|
|
@ -1,15 +1,24 @@
|
||||||
package command
|
package kvexp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent"
|
"github.com/hashicorp/consul/agent"
|
||||||
"github.com/hashicorp/consul/api"
|
"github.com/hashicorp/consul/api"
|
||||||
|
"github.com/hashicorp/consul/command/kvimpexp"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestKVExportCommand_noTabs(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
if strings.ContainsRune(New(nil).Help(), '\t') {
|
||||||
|
t.Fatal("usage has tabs")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestKVExportCommand_Run(t *testing.T) {
|
func TestKVExportCommand_Run(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
a := agent.NewTestAgent(t.Name(), ``)
|
a := agent.NewTestAgent(t.Name(), ``)
|
||||||
|
@ -17,12 +26,7 @@ func TestKVExportCommand_Run(t *testing.T) {
|
||||||
client := a.Client()
|
client := a.Client()
|
||||||
|
|
||||||
ui := cli.NewMockUi()
|
ui := cli.NewMockUi()
|
||||||
c := KVExportCommand{
|
c := New(ui)
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
UI: ui,
|
|
||||||
Flags: FlagSetHTTP,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
keys := map[string]string{
|
keys := map[string]string{
|
||||||
"foo/a": "a",
|
"foo/a": "a",
|
||||||
|
@ -49,7 +53,7 @@ func TestKVExportCommand_Run(t *testing.T) {
|
||||||
|
|
||||||
output := ui.OutputWriter.String()
|
output := ui.OutputWriter.String()
|
||||||
|
|
||||||
var exported []*kvExportEntry
|
var exported []*kvimpexp.Entry
|
||||||
err := json.Unmarshal([]byte(output), &exported)
|
err := json.Unmarshal([]byte(output), &exported)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("bad: %d", code)
|
t.Fatalf("bad: %d", code)
|
Loading…
Reference in New Issue