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