Skip to content

Commit b45bf87

Browse files
author
Dylan Clendenin
committed
implement del command
1 parent 5000655 commit b45bf87

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ can be performed in a single transaction)
4646
```
4747
kv set --prefix env/myapp/stage YO=123 THING_TOKEN=xcnvbxcmhdf COOL_FACTOR=9000
4848
```
49+
50+
### del
51+
52+
delete provided key(s) at a given prefix (*NOTE:* in Consul 0.7 this
53+
can be performed in a single transaction)
54+
```
55+
kv del --prefix env/myapp/stage YO THING_TOKEN COOL_FACTOR
56+
```

cmd/del.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"strings"
21+
22+
"github.com/deepthawtz/kv/store"
23+
consul "github.com/hashicorp/consul/api"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
// delCmd represents the del command
28+
var delCmd = &cobra.Command{
29+
Use: "del KEY [KEY...]",
30+
Short: "delete key from prefix (e.g., env/myapp/stage)",
31+
Long: "",
32+
Run: Del,
33+
}
34+
35+
func init() {
36+
RootCmd.AddCommand(delCmd)
37+
38+
delCmd.Flags().StringVarP(&prefix, "prefix", "p", "", "prefix to get key/value pairs from")
39+
}
40+
41+
// Del deletes key/value pairs by key
42+
func Del(cmd *cobra.Command, args []string) {
43+
if prefix == "" {
44+
fmt.Println("must supply key/value path --prefix")
45+
os.Exit(-1)
46+
}
47+
48+
client := store.NewConsulClient()
49+
if err := del(client, args...); err != nil {
50+
fmt.Println(err)
51+
os.Exit(-1)
52+
}
53+
}
54+
55+
func del(client *consul.KV, args ...string) error {
56+
for _, k := range args {
57+
key := strings.Join([]string{prefix, k}, "/")
58+
_, err := client.Delete(key, nil)
59+
if err != nil {
60+
return err
61+
}
62+
}
63+
return nil
64+
}

cmd/del_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
consul "github.com/hashicorp/consul/api"
8+
)
9+
10+
func TestDel(t *testing.T) {
11+
c, s := makeClient(t)
12+
defer s.Stop()
13+
kv := c.KV()
14+
15+
prefix = "env/yo/stage"
16+
k := strings.Join([]string{prefix, "YO"}, "/")
17+
_, _ = kv.Put(&consul.KVPair{Key: k, Value: []byte("123")}, nil)
18+
19+
if err := del(kv, []string{"YO"}...); err != nil {
20+
t.Error("expected del to return true but got false")
21+
}
22+
23+
kvs, _ := get(kv, []string{"YO"}...)
24+
if len(kvs) != 0 {
25+
t.Error("expected key to be deleted")
26+
}
27+
}

0 commit comments

Comments
 (0)