Merge pull request #9792 from dzeban/kv-import-prefix

command/kv: Add prefix option to kv import command
This commit is contained in:
Kyle Havlovitz 2021-03-11 09:47:53 -08:00 committed by GitHub
commit 7053fcdd0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 5 deletions

3
.changelog/9792.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:feature
cli: Add prefix option to kv import command
```

View File

@ -10,6 +10,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
@ -24,10 +25,11 @@ func New(ui cli.Ui) *cmd {
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
help string
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
help string
prefix string
// testStdin is the input for testing.
testStdin io.Reader
@ -35,6 +37,7 @@ type cmd struct {
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.StringVar(&c.prefix, "prefix", "", "Key prefix for imported data")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
@ -76,7 +79,7 @@ func (c *cmd) Run(args []string) int {
}
pair := &api.KVPair{
Key: entry.Key,
Key: filepath.Join(c.prefix, entry.Key),
Flags: entry.Flags,
Value: value,
}

View File

@ -70,3 +70,55 @@ func TestKVImportCommand(t *testing.T) {
t.Fatalf("bad: expected: baz, got %s", pair.Value)
}
}
func TestKVImportPrefixCommand(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
t.Parallel()
a := agent.NewTestAgent(t, ``)
defer a.Shutdown()
client := a.Client()
const json = `[
{
"key": "foo",
"flags": 0,
"value": "YmFyCg=="
}
]`
ui := cli.NewMockUi()
c := New(ui)
c.testStdin = strings.NewReader(json)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-prefix=" + "sub/",
"-",
}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
pair, _, err := client.KV().Get("foo", nil)
if err != nil {
t.Fatal(err)
}
if pair != nil {
t.Fatalf("bad: expected: nil, got %+v", pair)
}
pair, _, err = client.KV().Get("sub/foo", nil)
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(string(pair.Value)) != "bar" {
t.Fatalf("bad: expected: bar, got %s", pair.Value)
}
}

View File

@ -21,6 +21,11 @@ Usage: `consul kv import [options] [DATA]`
@include 'http_api_options_server.mdx'
#### KV Import Options
- `-prefix` - Key prefix for imported data. The default value is empty meaning
root.
#### Enterprise Options
@include 'http_api_namespace_options.mdx'
@ -48,3 +53,10 @@ escaping:
$ consul kv import "$(cat values.json)"
# Output
```
To import under prefix, use `-prefix` option:
```shell-session
$ cat values.json | consul kv import -prefix=sub/dir/ -
# Output
```