Merge pull request #2632 from hashicorp/kv-put-base64

cli: Add -base64 option to `consul kv put`
This commit is contained in:
James Nugent 2017-01-04 16:12:42 -06:00 committed by GitHub
commit 0346e3e64f
3 changed files with 63 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package command
import ( import (
"bytes" "bytes"
"encoding/base64"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -45,6 +46,9 @@ Usage: consul kv put [options] KEY [DATA]
$ consul kv put webapp/beta/active $ consul kv put webapp/beta/active
If the -base64 flag is specified, the data will be treated as base 64
encoded.
To perform a Check-And-Set operation, specify the -cas flag with the To perform a Check-And-Set operation, specify the -cas flag with the
appropriate -modify-index flag corresponding to the key you want to perform appropriate -modify-index flag corresponding to the key you want to perform
the CAS operation on: the CAS operation on:
@ -62,6 +66,9 @@ KV Put Options:
lock. The session must already exist and be specified lock. The session must already exist and be specified
via the -session flag. The default value is false. via the -session flag. The default value is false.
-base64 Treat the data as base 64 encoded. The default value
is false.
-cas Perform a Check-And-Set operation. Specifying this -cas Perform a Check-And-Set operation. Specifying this
value also requires the -modify-index flag to be set. value also requires the -modify-index flag to be set.
The default value is false. The default value is false.
@ -95,6 +102,7 @@ func (c *KVPutCommand) Run(args []string) int {
token := cmdFlags.String("token", "", "") token := cmdFlags.String("token", "", "")
cas := cmdFlags.Bool("cas", false, "") cas := cmdFlags.Bool("cas", false, "")
flags := cmdFlags.Uint64("flags", 0, "") flags := cmdFlags.Uint64("flags", 0, "")
base64encoded := cmdFlags.Bool("base64", false, "")
modifyIndex := cmdFlags.Uint64("modify-index", 0, "") modifyIndex := cmdFlags.Uint64("modify-index", 0, "")
session := cmdFlags.String("session", "", "") session := cmdFlags.String("session", "", "")
acquire := cmdFlags.Bool("acquire", false, "") acquire := cmdFlags.Bool("acquire", false, "")
@ -111,6 +119,14 @@ func (c *KVPutCommand) Run(args []string) int {
return 1 return 1
} }
dataBytes := []byte(data)
if *base64encoded {
dataBytes, err = base64.StdEncoding.DecodeString(data)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error! Cannot base 64 decode data: %s", err))
}
}
// Session is reauired for release or acquire // Session is reauired for release or acquire
if (*release || *acquire) && *session == "" { if (*release || *acquire) && *session == "" {
c.Ui.Error("Error! Missing -session (required with -acquire and -release)") c.Ui.Error("Error! Missing -session (required with -acquire and -release)")
@ -137,7 +153,7 @@ func (c *KVPutCommand) Run(args []string) int {
Key: key, Key: key,
ModifyIndex: *modifyIndex, ModifyIndex: *modifyIndex,
Flags: *flags, Flags: *flags,
Value: []byte(data), Value: dataBytes,
Session: *session, Session: *session,
} }

View File

@ -2,6 +2,7 @@ package command
import ( import (
"bytes" "bytes"
"encoding/base64"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
@ -100,6 +101,42 @@ func TestKVPutCommand_Run(t *testing.T) {
} }
} }
func TestKVPutCommand_RunBase64(t *testing.T) {
srv, client := testAgentWithAPIClient(t)
defer srv.Shutdown()
waitForLeader(t, srv.httpAddr)
ui := new(cli.MockUi)
c := &KVPutCommand{Ui: ui}
const encodedString = "aGVsbG8gd29ybGQK"
args := []string{
"-http-addr=" + srv.httpAddr,
"-base64",
"foo", encodedString,
}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
data, _, err := client.KV().Get("foo", nil)
if err != nil {
t.Fatal(err)
}
expected, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data.Value, []byte(expected)) {
t.Errorf("bad: %#v, %s", data.Value, data.Value)
}
}
func TestKVPutCommand_File(t *testing.T) { func TestKVPutCommand_File(t *testing.T) {
srv, client := testAgentWithAPIClient(t) srv, client := testAgentWithAPIClient(t)
defer srv.Shutdown() defer srv.Shutdown()

View File

@ -24,6 +24,8 @@ Usage: `consul kv put [options] KEY [DATA]`
operation will create the key and obtain the lock. The session must already operation will create the key and obtain the lock. The session must already
exist and be specified via the -session flag. The default value is false. exist and be specified via the -session flag. The default value is false.
* `-base64` - Treat the data as base 64 encoded. The default value is false.
* `-cas` - Perform a Check-And-Set operation. Specifying this value also * `-cas` - Perform a Check-And-Set operation. Specifying this value also
requires the -modify-index flag to be set. The default value is false. requires the -modify-index flag to be set. The default value is false.
@ -60,6 +62,13 @@ $ consul kv put redis/config/connections
Success! Data written to: redis/config/connections Success! Data written to: redis/config/connections
``` ```
If the `-base64` flag is set, the data will be decoded before writing:
```
$ consul kv put -base64 foo/encoded aGVsbG8gd29ybGQK
Success! Data written to: foo/encoded
```
!> **Be careful when overwriting data!** The above operation would overwrite !> **Be careful when overwriting data!** The above operation would overwrite
the value at the key to the empty value. the value at the key to the empty value.