mirror of https://github.com/hashicorp/consul
Fix KV CLI subcommands to build help string in constructor
parent
b1d5f99a58
commit
71887d1709
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
func New(ui cli.Ui) *cmd {
|
||||
c := &cmd{UI: ui}
|
||||
c.initFlags()
|
||||
c.init()
|
||||
return c
|
||||
}
|
||||
|
||||
|
@ -19,12 +19,13 @@ type cmd struct {
|
|||
UI cli.Ui
|
||||
flags *flag.FlagSet
|
||||
http *flags.HTTPFlags
|
||||
usage string
|
||||
cas bool
|
||||
modifyIndex uint64
|
||||
recurse bool
|
||||
}
|
||||
|
||||
func (c *cmd) initFlags() {
|
||||
func (c *cmd) init() {
|
||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
c.flags.BoolVar(&c.cas, "cas", false,
|
||||
"Perform a Check-And-Set operation. Specifying this value also requires "+
|
||||
|
@ -38,6 +39,7 @@ func (c *cmd) initFlags() {
|
|||
c.http = &flags.HTTPFlags{}
|
||||
flags.Merge(c.flags, c.http.ClientFlags())
|
||||
flags.Merge(c.flags, c.http.ServerFlags())
|
||||
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||
}
|
||||
|
||||
func (c *cmd) Run(args []string) int {
|
||||
|
@ -140,7 +142,10 @@ func (c *cmd) Synopsis() string {
|
|||
}
|
||||
|
||||
func (c *cmd) Help() string {
|
||||
s := `Usage: consul kv delete [options] KEY_OR_PREFIX
|
||||
return c.usage
|
||||
}
|
||||
|
||||
const usage = `Usage: consul kv delete [options] KEY_OR_PREFIX
|
||||
|
||||
Removes the value from Consul's key-value store at the given path. If no
|
||||
key exists at the path, no action is taken.
|
||||
|
@ -155,5 +160,3 @@ func (c *cmd) Help() string {
|
|||
|
||||
This will delete the keys named "foo", "food", and "foo/bar/zip" if they
|
||||
existed. `
|
||||
return flags.Usage(s, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ func TestKVDeleteCommand_Validation(t *testing.T) {
|
|||
}
|
||||
|
||||
for name, tc := range cases {
|
||||
c.initFlags()
|
||||
c.init()
|
||||
// Ensure our buffer is always clear
|
||||
if ui.ErrorWriter != nil {
|
||||
ui.ErrorWriter.Reset()
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
|
||||
func New(ui cli.Ui) *cmd {
|
||||
c := &cmd{UI: ui}
|
||||
c.initFlags()
|
||||
c.init()
|
||||
return c
|
||||
}
|
||||
|
||||
|
@ -21,13 +21,15 @@ type cmd struct {
|
|||
UI cli.Ui
|
||||
flags *flag.FlagSet
|
||||
http *flags.HTTPFlags
|
||||
usage string
|
||||
}
|
||||
|
||||
func (c *cmd) initFlags() {
|
||||
func (c *cmd) init() {
|
||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
c.http = &flags.HTTPFlags{}
|
||||
flags.Merge(c.flags, c.http.ClientFlags())
|
||||
flags.Merge(c.flags, c.http.ServerFlags())
|
||||
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||
}
|
||||
|
||||
func (c *cmd) Run(args []string) int {
|
||||
|
@ -91,7 +93,10 @@ func (c *cmd) Synopsis() string {
|
|||
}
|
||||
|
||||
func (c *cmd) Help() string {
|
||||
s := `Usage: consul kv export [KEY_OR_PREFIX]
|
||||
return c.usage
|
||||
}
|
||||
|
||||
const usage = `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
|
||||
|
@ -100,6 +105,3 @@ func (c *cmd) Help() string {
|
|||
$ 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())
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ import (
|
|||
|
||||
func New(ui cli.Ui) *cmd {
|
||||
c := &cmd{UI: ui}
|
||||
c.initFlags()
|
||||
c.init()
|
||||
return c
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ type cmd struct {
|
|||
UI cli.Ui
|
||||
flags *flag.FlagSet
|
||||
http *flags.HTTPFlags
|
||||
usage string
|
||||
base64encode bool
|
||||
detailed bool
|
||||
keys bool
|
||||
|
@ -30,7 +31,7 @@ type cmd struct {
|
|||
separator string
|
||||
}
|
||||
|
||||
func (c *cmd) initFlags() {
|
||||
func (c *cmd) init() {
|
||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
c.flags.BoolVar(&c.base64encode, "base64", false,
|
||||
"Base64 encode the value. The default value is false.")
|
||||
|
@ -53,6 +54,7 @@ func (c *cmd) initFlags() {
|
|||
c.http = &flags.HTTPFlags{}
|
||||
flags.Merge(c.flags, c.http.ClientFlags())
|
||||
flags.Merge(c.flags, c.http.ServerFlags())
|
||||
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||
}
|
||||
|
||||
func (c *cmd) Run(args []string) int {
|
||||
|
@ -177,7 +179,30 @@ func (c *cmd) Synopsis() string {
|
|||
}
|
||||
|
||||
func (c *cmd) Help() string {
|
||||
s := `Usage: consul kv get [options] [KEY_OR_PREFIX]
|
||||
return c.usage
|
||||
}
|
||||
|
||||
func prettyKVPair(w io.Writer, pair *api.KVPair, base64EncodeValue bool) error {
|
||||
tw := tabwriter.NewWriter(w, 0, 2, 6, ' ', 0)
|
||||
fmt.Fprintf(tw, "CreateIndex\t%d\n", pair.CreateIndex)
|
||||
fmt.Fprintf(tw, "Flags\t%d\n", pair.Flags)
|
||||
fmt.Fprintf(tw, "Key\t%s\n", pair.Key)
|
||||
fmt.Fprintf(tw, "LockIndex\t%d\n", pair.LockIndex)
|
||||
fmt.Fprintf(tw, "ModifyIndex\t%d\n", pair.ModifyIndex)
|
||||
if pair.Session == "" {
|
||||
fmt.Fprint(tw, "Session\t-\n")
|
||||
} else {
|
||||
fmt.Fprintf(tw, "Session\t%s\n", pair.Session)
|
||||
}
|
||||
if base64EncodeValue {
|
||||
fmt.Fprintf(tw, "Value\t%s", base64.StdEncoding.EncodeToString(pair.Value))
|
||||
} else {
|
||||
fmt.Fprintf(tw, "Value\t%s", pair.Value)
|
||||
}
|
||||
return tw.Flush()
|
||||
}
|
||||
|
||||
const usage = `Usage: consul kv get [options] [KEY_OR_PREFIX]
|
||||
|
||||
Retrieves the value from Consul's key-value store at the given key name. If no
|
||||
key exists with that name, an error is returned. If a key exists with that
|
||||
|
@ -206,26 +231,3 @@ func (c *cmd) Help() string {
|
|||
$ consul kv get -keys foo
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
func prettyKVPair(w io.Writer, pair *api.KVPair, base64EncodeValue bool) error {
|
||||
tw := tabwriter.NewWriter(w, 0, 2, 6, ' ', 0)
|
||||
fmt.Fprintf(tw, "CreateIndex\t%d\n", pair.CreateIndex)
|
||||
fmt.Fprintf(tw, "Flags\t%d\n", pair.Flags)
|
||||
fmt.Fprintf(tw, "Key\t%s\n", pair.Key)
|
||||
fmt.Fprintf(tw, "LockIndex\t%d\n", pair.LockIndex)
|
||||
fmt.Fprintf(tw, "ModifyIndex\t%d\n", pair.ModifyIndex)
|
||||
if pair.Session == "" {
|
||||
fmt.Fprint(tw, "Session\t-\n")
|
||||
} else {
|
||||
fmt.Fprintf(tw, "Session\t%s\n", pair.Session)
|
||||
}
|
||||
if base64EncodeValue {
|
||||
fmt.Fprintf(tw, "Value\t%s", base64.StdEncoding.EncodeToString(pair.Value))
|
||||
} else {
|
||||
fmt.Fprintf(tw, "Value\t%s", pair.Value)
|
||||
}
|
||||
return tw.Flush()
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ import (
|
|||
|
||||
func New(ui cli.Ui) *cmd {
|
||||
c := &cmd{UI: ui}
|
||||
c.initFlags()
|
||||
c.init()
|
||||
return c
|
||||
}
|
||||
|
||||
|
@ -27,16 +27,18 @@ type cmd struct {
|
|||
UI cli.Ui
|
||||
flags *flag.FlagSet
|
||||
http *flags.HTTPFlags
|
||||
usage string
|
||||
|
||||
// testStdin is the input for testing.
|
||||
testStdin io.Reader
|
||||
}
|
||||
|
||||
func (c *cmd) initFlags() {
|
||||
func (c *cmd) init() {
|
||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
c.http = &flags.HTTPFlags{}
|
||||
flags.Merge(c.flags, c.http.ClientFlags())
|
||||
flags.Merge(c.flags, c.http.ServerFlags())
|
||||
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||
}
|
||||
|
||||
func (c *cmd) Run(args []string) int {
|
||||
|
@ -94,26 +96,7 @@ func (c *cmd) Synopsis() string {
|
|||
}
|
||||
|
||||
func (c *cmd) Help() string {
|
||||
s := `Usage: consul kv import [DATA]
|
||||
|
||||
Imports key-value pairs to the key-value store from the JSON representation
|
||||
generated by the "consul kv export" command.
|
||||
|
||||
The data can be read from a file by prefixing the filename with the "@"
|
||||
symbol. For example:
|
||||
|
||||
$ consul kv import @filename.json
|
||||
|
||||
Or it can be read from stdin using the "-" symbol:
|
||||
|
||||
$ cat filename.json | consul kv import -
|
||||
|
||||
Alternatively the data may be provided as the final parameter to the command,
|
||||
though care must be taken with regards to shell escaping.
|
||||
|
||||
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())
|
||||
return c.usage
|
||||
}
|
||||
|
||||
func (c *cmd) dataFromArgs(args []string) (string, error) {
|
||||
|
@ -156,3 +139,22 @@ func (c *cmd) dataFromArgs(args []string) (string, error) {
|
|||
return data, nil
|
||||
}
|
||||
}
|
||||
|
||||
const usage = `Usage: consul kv import [DATA]
|
||||
|
||||
Imports key-value pairs to the key-value store from the JSON representation
|
||||
generated by the "consul kv export" command.
|
||||
|
||||
The data can be read from a file by prefixing the filename with the "@"
|
||||
symbol. For example:
|
||||
|
||||
$ consul kv import @filename.json
|
||||
|
||||
Or it can be read from stdin using the "-" symbol:
|
||||
|
||||
$ cat filename.json | consul kv import -
|
||||
|
||||
Alternatively the data may be provided as the final parameter to the command,
|
||||
though care must be taken with regards to shell escaping.
|
||||
|
||||
For a full list of options and examples, please see the Consul documentation.`
|
||||
|
|
|
@ -16,7 +16,7 @@ import (
|
|||
|
||||
func New(ui cli.Ui) *cmd {
|
||||
c := &cmd{UI: ui}
|
||||
c.initFlags()
|
||||
c.init()
|
||||
return c
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,7 @@ type cmd struct {
|
|||
UI cli.Ui
|
||||
flags *flag.FlagSet
|
||||
http *flags.HTTPFlags
|
||||
usage string
|
||||
|
||||
// flags
|
||||
cas bool
|
||||
|
@ -38,7 +39,7 @@ type cmd struct {
|
|||
testStdin io.Reader
|
||||
}
|
||||
|
||||
func (c *cmd) initFlags() {
|
||||
func (c *cmd) init() {
|
||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
c.flags.BoolVar(&c.cas, "cas", false,
|
||||
"Perform a Check-And-Set operation. Specifying this value also "+
|
||||
|
@ -69,6 +70,7 @@ func (c *cmd) initFlags() {
|
|||
c.http = &flags.HTTPFlags{}
|
||||
flags.Merge(c.flags, c.http.ClientFlags())
|
||||
flags.Merge(c.flags, c.http.ServerFlags())
|
||||
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||
}
|
||||
|
||||
func (c *cmd) Run(args []string) int {
|
||||
|
@ -175,39 +177,7 @@ func (c *cmd) Synopsis() string {
|
|||
}
|
||||
|
||||
func (c *cmd) Help() string {
|
||||
s := `Usage: consul kv put [options] KEY [DATA]
|
||||
|
||||
Writes the data to the given path in the key-value store. The data can be of
|
||||
any type.
|
||||
|
||||
$ consul kv put config/redis/maxconns 5
|
||||
|
||||
The data can also be consumed from a file on disk by prefixing with the "@"
|
||||
symbol. For example:
|
||||
|
||||
$ consul kv put config/program/license @license.lic
|
||||
|
||||
Or it can be read from stdin using the "-" symbol:
|
||||
|
||||
$ echo "abcd1234" | consul kv put config/program/license -
|
||||
|
||||
The DATA argument itself is optional. If omitted, this will create an empty
|
||||
key-value pair at the specified path:
|
||||
|
||||
$ 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
|
||||
appropriate -modify-index flag corresponding to the key you want to perform
|
||||
the CAS operation on:
|
||||
|
||||
$ consul kv put -cas -modify-index=844 config/redis/maxconns 5
|
||||
|
||||
Additional flags and more advanced use cases are detailed below.`
|
||||
|
||||
return flags.Usage(s, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||
return c.usage
|
||||
}
|
||||
|
||||
func (c *cmd) dataFromArgs(args []string) (string, string, error) {
|
||||
|
@ -254,3 +224,35 @@ func (c *cmd) dataFromArgs(args []string) (string, string, error) {
|
|||
return key, data, nil
|
||||
}
|
||||
}
|
||||
|
||||
const usage = `Usage: consul kv put [options] KEY [DATA]
|
||||
|
||||
Writes the data to the given path in the key-value store. The data can be of
|
||||
any type.
|
||||
|
||||
$ consul kv put config/redis/maxconns 5
|
||||
|
||||
The data can also be consumed from a file on disk by prefixing with the "@"
|
||||
symbol. For example:
|
||||
|
||||
$ consul kv put config/program/license @license.lic
|
||||
|
||||
Or it can be read from stdin using the "-" symbol:
|
||||
|
||||
$ echo "abcd1234" | consul kv put config/program/license -
|
||||
|
||||
The DATA argument itself is optional. If omitted, this will create an empty
|
||||
key-value pair at the specified path:
|
||||
|
||||
$ 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
|
||||
appropriate -modify-index flag corresponding to the key you want to perform
|
||||
the CAS operation on:
|
||||
|
||||
$ consul kv put -cas -modify-index=844 config/redis/maxconns 5
|
||||
|
||||
Additional flags and more advanced use cases are detailed below.`
|
||||
|
|
|
@ -54,7 +54,7 @@ func TestKVPutCommand_Validation(t *testing.T) {
|
|||
}
|
||||
|
||||
for name, tc := range cases {
|
||||
c.initFlags()
|
||||
c.init()
|
||||
// Ensure our buffer is always clear
|
||||
if ui.ErrorWriter != nil {
|
||||
ui.ErrorWriter.Reset()
|
||||
|
|
Loading…
Reference in New Issue