Fix KV CLI subcommands to build help string in constructor

pull/3584/head
Preetha Appan 2017-10-11 17:14:27 -05:00 committed by Frank Schröder
parent b1d5f99a58
commit 71887d1709
7 changed files with 107 additions and 96 deletions

View File

@ -11,7 +11,7 @@ import (
func New(ui cli.Ui) *cmd { func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui} c := &cmd{UI: ui}
c.initFlags() c.init()
return c return c
} }
@ -19,12 +19,13 @@ type cmd struct {
UI cli.Ui UI cli.Ui
flags *flag.FlagSet flags *flag.FlagSet
http *flags.HTTPFlags http *flags.HTTPFlags
usage string
cas bool cas bool
modifyIndex uint64 modifyIndex uint64
recurse bool recurse bool
} }
func (c *cmd) initFlags() { func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError) c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.BoolVar(&c.cas, "cas", false, c.flags.BoolVar(&c.cas, "cas", false,
"Perform a Check-And-Set operation. Specifying this value also requires "+ "Perform a Check-And-Set operation. Specifying this value also requires "+
@ -38,6 +39,7 @@ func (c *cmd) initFlags() {
c.http = &flags.HTTPFlags{} c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags()) flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags()) 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 { func (c *cmd) Run(args []string) int {
@ -140,7 +142,10 @@ func (c *cmd) Synopsis() string {
} }
func (c *cmd) Help() 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 Removes the value from Consul's key-value store at the given path. If no
key exists at the path, no action is taken. 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 This will delete the keys named "foo", "food", and "foo/bar/zip" if they
existed. ` existed. `
return flags.Usage(s, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
}

View File

@ -49,7 +49,7 @@ func TestKVDeleteCommand_Validation(t *testing.T) {
} }
for name, tc := range cases { for name, tc := range cases {
c.initFlags() c.init()
// Ensure our buffer is always clear // Ensure our buffer is always clear
if ui.ErrorWriter != nil { if ui.ErrorWriter != nil {
ui.ErrorWriter.Reset() ui.ErrorWriter.Reset()

View File

@ -13,7 +13,7 @@ import (
func New(ui cli.Ui) *cmd { func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui} c := &cmd{UI: ui}
c.initFlags() c.init()
return c return c
} }
@ -21,13 +21,15 @@ type cmd struct {
UI cli.Ui UI cli.Ui
flags *flag.FlagSet flags *flag.FlagSet
http *flags.HTTPFlags http *flags.HTTPFlags
usage string
} }
func (c *cmd) initFlags() { func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError) c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.http = &flags.HTTPFlags{} c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags()) flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags()) 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 { func (c *cmd) Run(args []string) int {
@ -91,7 +93,10 @@ func (c *cmd) Synopsis() string {
} }
func (c *cmd) Help() 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, 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 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 $ consul kv export vault
For a full list of options and examples, please see the Consul documentation.` 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())
}

View File

@ -15,7 +15,7 @@ import (
func New(ui cli.Ui) *cmd { func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui} c := &cmd{UI: ui}
c.initFlags() c.init()
return c return c
} }
@ -23,6 +23,7 @@ type cmd struct {
UI cli.Ui UI cli.Ui
flags *flag.FlagSet flags *flag.FlagSet
http *flags.HTTPFlags http *flags.HTTPFlags
usage string
base64encode bool base64encode bool
detailed bool detailed bool
keys bool keys bool
@ -30,7 +31,7 @@ type cmd struct {
separator string separator string
} }
func (c *cmd) initFlags() { func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError) c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.BoolVar(&c.base64encode, "base64", false, c.flags.BoolVar(&c.base64encode, "base64", false,
"Base64 encode the value. The default value is false.") "Base64 encode the value. The default value is false.")
@ -53,6 +54,7 @@ func (c *cmd) initFlags() {
c.http = &flags.HTTPFlags{} c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags()) flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags()) 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 { func (c *cmd) Run(args []string) int {
@ -177,7 +179,30 @@ func (c *cmd) Synopsis() string {
} }
func (c *cmd) Help() 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 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 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 $ consul kv get -keys foo
For a full list of options and examples, please see the Consul documentation. ` 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()
}

View File

@ -19,7 +19,7 @@ import (
func New(ui cli.Ui) *cmd { func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui} c := &cmd{UI: ui}
c.initFlags() c.init()
return c return c
} }
@ -27,16 +27,18 @@ type cmd struct {
UI cli.Ui UI cli.Ui
flags *flag.FlagSet flags *flag.FlagSet
http *flags.HTTPFlags http *flags.HTTPFlags
usage string
// testStdin is the input for testing. // testStdin is the input for testing.
testStdin io.Reader testStdin io.Reader
} }
func (c *cmd) initFlags() { func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError) c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.http = &flags.HTTPFlags{} c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags()) flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags()) 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 { func (c *cmd) Run(args []string) int {
@ -94,26 +96,7 @@ func (c *cmd) Synopsis() string {
} }
func (c *cmd) Help() string { func (c *cmd) Help() string {
s := `Usage: consul kv import [DATA] return c.usage
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())
} }
func (c *cmd) dataFromArgs(args []string) (string, error) { func (c *cmd) dataFromArgs(args []string) (string, error) {
@ -156,3 +139,22 @@ func (c *cmd) dataFromArgs(args []string) (string, error) {
return data, nil 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.`

View File

@ -16,7 +16,7 @@ import (
func New(ui cli.Ui) *cmd { func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui} c := &cmd{UI: ui}
c.initFlags() c.init()
return c return c
} }
@ -24,6 +24,7 @@ type cmd struct {
UI cli.Ui UI cli.Ui
flags *flag.FlagSet flags *flag.FlagSet
http *flags.HTTPFlags http *flags.HTTPFlags
usage string
// flags // flags
cas bool cas bool
@ -38,7 +39,7 @@ type cmd struct {
testStdin io.Reader testStdin io.Reader
} }
func (c *cmd) initFlags() { func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError) c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.BoolVar(&c.cas, "cas", false, c.flags.BoolVar(&c.cas, "cas", false,
"Perform a Check-And-Set operation. Specifying this value also "+ "Perform a Check-And-Set operation. Specifying this value also "+
@ -69,6 +70,7 @@ func (c *cmd) initFlags() {
c.http = &flags.HTTPFlags{} c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags()) flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags()) 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 { func (c *cmd) Run(args []string) int {
@ -175,39 +177,7 @@ func (c *cmd) Synopsis() string {
} }
func (c *cmd) Help() string { func (c *cmd) Help() string {
s := `Usage: consul kv put [options] KEY [DATA] return c.usage
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())
} }
func (c *cmd) dataFromArgs(args []string) (string, string, error) { 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 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.`

View File

@ -54,7 +54,7 @@ func TestKVPutCommand_Validation(t *testing.T) {
} }
for name, tc := range cases { for name, tc := range cases {
c.initFlags() c.init()
// Ensure our buffer is always clear // Ensure our buffer is always clear
if ui.ErrorWriter != nil { if ui.ErrorWriter != nil {
ui.ErrorWriter.Reset() ui.ErrorWriter.Reset()