commands: move kv delete command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-11 14:51:26 +02:00 committed by Frank Schröder
parent 200199a875
commit 5e3371eee1
3 changed files with 60 additions and 66 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/hashicorp/consul/command/join" "github.com/hashicorp/consul/command/join"
"github.com/hashicorp/consul/command/keygen" "github.com/hashicorp/consul/command/keygen"
"github.com/hashicorp/consul/command/kv" "github.com/hashicorp/consul/command/kv"
"github.com/hashicorp/consul/command/kvdel"
"github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/command/validate"
"github.com/hashicorp/consul/version" "github.com/hashicorp/consul/version"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -119,12 +120,7 @@ func init() {
}, },
"kv delete": func() (cli.Command, error) { "kv delete": func() (cli.Command, error) {
return &KVDeleteCommand{ return kvdel.New(ui), nil
BaseCommand: BaseCommand{
Flags: FlagSetHTTP,
UI: ui,
},
}, nil
}, },
"kv get": func() (cli.Command, error) { "kv get": func() (cli.Command, error) {

View File

@ -1,66 +1,54 @@
package command package kvdel
import ( import (
"flag"
"fmt" "fmt"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
) )
// KVDeleteCommand is a Command implementation that is used to delete a key or func New(ui cli.Ui) *cmd {
// prefix of keys from the key-value store. c := &cmd{UI: ui}
type KVDeleteCommand struct { c.initFlags()
BaseCommand return c
}
// flags type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
cas bool cas bool
modifyIndex uint64 modifyIndex uint64
recurse bool recurse bool
} }
func (c *KVDeleteCommand) initFlags() { func (c *cmd) initFlags() {
c.InitFlagSet() c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.FlagSet.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 "+
"the -modify-index flag to be set. The default value is false.") "the -modify-index flag to be set. The default value is false.")
c.FlagSet.Uint64Var(&c.modifyIndex, "modify-index", 0, c.flags.Uint64Var(&c.modifyIndex, "modify-index", 0,
"Unsigned integer representing the ModifyIndex of the key. This is "+ "Unsigned integer representing the ModifyIndex of the key. This is "+
"used in combination with the -cas flag.") "used in combination with the -cas flag.")
c.FlagSet.BoolVar(&c.recurse, "recurse", false, c.flags.BoolVar(&c.recurse, "recurse", false,
"Recursively delete all keys with the path. The default value is false.") "Recursively delete all keys with the path. The default value is false.")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
} }
func (c *KVDeleteCommand) Help() string { func (c *cmd) Run(args []string) int {
c.initFlags() if err := c.flags.Parse(args); err != nil {
return c.HelpCommand(`
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.
To delete the value for the key named "foo" in the key-value store:
$ consul kv delete foo
To delete all keys which start with "foo", specify the -recurse option:
$ consul kv delete -recurse foo
This will delete the keys named "foo", "food", and "foo/bar/zip" if they
existed.
`)
}
func (c *KVDeleteCommand) Run(args []string) int {
c.initFlags()
if err := c.FlagSet.Parse(args); err != nil {
return 1 return 1
} }
key := "" key := ""
// Check for arg validation // Check for arg validation
args = c.FlagSet.Args() args = c.flags.Args()
switch len(args) { switch len(args) {
case 0: case 0:
key = "" key = ""
@ -103,7 +91,7 @@ func (c *KVDeleteCommand) Run(args []string) int {
} }
// Create and test the HTTP client // Create and test the HTTP client
client, err := c.HTTPClient() client, err := c.http.APIClient()
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1 return 1
@ -147,6 +135,25 @@ func (c *KVDeleteCommand) Run(args []string) int {
} }
} }
func (c *KVDeleteCommand) Synopsis() string { func (c *cmd) Synopsis() string {
return "Removes data from the KV store" return "Removes data from the KV store"
} }
func (c *cmd) Help() string {
s := `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.
To delete the value for the key named "foo" in the key-value store:
$ consul kv delete foo
To delete all keys which start with "foo", specify the -recurse option:
$ consul kv delete -recurse foo
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())
}

View File

@ -1,4 +1,4 @@
package command package kvdel
import ( import (
"strconv" "strconv"
@ -10,29 +10,17 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func testKVDeleteCommand(t *testing.T) (*cli.MockUi, *KVDeleteCommand) {
ui := cli.NewMockUi()
return ui, &KVDeleteCommand{
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetHTTP,
},
}
}
func TestKVDeleteCommand_implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &KVDeleteCommand{}
}
func TestKVDeleteCommand_noTabs(t *testing.T) { func TestKVDeleteCommand_noTabs(t *testing.T) {
t.Parallel() t.Parallel()
assertNoTabs(t, new(KVDeleteCommand)) if strings.ContainsRune(New(nil).Help(), '\t') {
t.Fatal("usage has tabs")
}
} }
func TestKVDeleteCommand_Validation(t *testing.T) { func TestKVDeleteCommand_Validation(t *testing.T) {
t.Parallel() t.Parallel()
ui, c := testKVDeleteCommand(t) ui := cli.NewMockUi()
c := New(ui)
cases := map[string]struct { cases := map[string]struct {
args []string args []string
@ -87,7 +75,8 @@ func TestKVDeleteCommand_Run(t *testing.T) {
defer a.Shutdown() defer a.Shutdown()
client := a.Client() client := a.Client()
ui, c := testKVDeleteCommand(t) ui := cli.NewMockUi()
c := New(ui)
pair := &api.KVPair{ pair := &api.KVPair{
Key: "foo", Key: "foo",
@ -123,7 +112,8 @@ func TestKVDeleteCommand_Recurse(t *testing.T) {
defer a.Shutdown() defer a.Shutdown()
client := a.Client() client := a.Client()
ui, c := testKVDeleteCommand(t) ui := cli.NewMockUi()
c := New(ui)
keys := []string{"foo/a", "foo/b", "food"} keys := []string{"foo/a", "foo/b", "food"}
@ -166,7 +156,8 @@ func TestKVDeleteCommand_CAS(t *testing.T) {
defer a.Shutdown() defer a.Shutdown()
client := a.Client() client := a.Client()
ui, c := testKVDeleteCommand(t) ui := cli.NewMockUi()
c := New(ui)
pair := &api.KVPair{ pair := &api.KVPair{
Key: "foo", Key: "foo",