Skip to content
This repository was archived by the owner on Jun 4, 2019. It is now read-only.

Commit cfc6f36

Browse files
committed
Merge pull request #16 from derekchiang/0.2
A set of changes to update etcdctl to use go-etcd 0.2
2 parents 5aaeca1 + e8db337 commit cfc6f36

36 files changed

Lines changed: 1645 additions & 865 deletions

compare_and_swap.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
const CompareAndSwapUsage = `usage: etcdctl [etcd flags] compareAndSwap <key> <value> [testAndSet flags]
9+
either prevvalue or previndex needs to be given
10+
special flags: --ttl to set a key with ttl
11+
--prevvalue to set the previous value
12+
--previndex to set the previous index`
13+
14+
var (
15+
compareAndSwapFlag = flag.NewFlagSet("testAndSet", flag.ExitOnError)
16+
compareAndSwapTtl = compareAndSwapFlag.Uint64("ttl", 0, "ttl of the key")
17+
compareAndSwapPvalue = compareAndSwapFlag.String("prevvalue", "", "previous value")
18+
compareAndSwapPindex = compareAndSwapFlag.Uint64("previndex", 0, "previous index")
19+
)
20+
21+
func init() {
22+
// The minimum number of arguments is 3 because
23+
// there needs to be either pvalue or pindex
24+
registerCommand("compareAndSwap", CompareAndSwapUsage, 3, 6, compareAndSwap)
25+
}
26+
27+
func compareAndSwap(args []string) error {
28+
key := args[0]
29+
value := args[1]
30+
compareAndSwapFlag.Parse(args[2:])
31+
resp, err := client.CompareAndSwap(key, value,
32+
*compareAndSwapTtl, *compareAndSwapPvalue, *compareAndSwapPindex)
33+
if debug {
34+
fmt.Println(<-curlChan)
35+
}
36+
if err != nil {
37+
return err
38+
}
39+
40+
output(resp)
41+
return nil
42+
}

delete.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,39 @@ import (
55
)
66

77
const DeleteUsage = `usage: etcdctl [etcd flags] delete <key>`
8+
const DeleteAllUsage = `usage: etcdctl [etcd flags] deleteAll <key>`
89

910
func init() {
10-
registerCommand("delete", DeleteUsage, 2, 2, delete)
11+
registerCommand("delete", DeleteUsage, 1, 1, delete)
12+
registerCommand("deleteAll", DeleteAllUsage, 1, 1, deleteAll)
1113
}
1214

1315
func delete(args []string) error {
14-
key := args[1]
16+
key := args[0]
1517

1618
resp, err := client.Delete(key)
19+
if debug {
20+
fmt.Println(<-curlChan)
21+
}
22+
if err != nil {
23+
return err
24+
}
25+
output(resp)
26+
27+
return nil
28+
}
29+
30+
func deleteAll(args []string) error {
31+
key := args[0]
32+
33+
resp, err := client.DeleteAll(key)
34+
if debug {
35+
fmt.Println(<-curlChan)
36+
}
1737
if err != nil {
1838
return err
1939
}
20-
fmt.Println(resp.PrevValue)
40+
output(resp)
2141

2242
return nil
2343
}

etcdctl.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,65 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"flag"
56
"fmt"
67
"github.com/coreos/go-etcd/etcd"
78
"os"
89
)
910

1011
var (
11-
client *etcd.Client
12-
12+
client *etcd.Client
1313
printVersion bool
14+
outputFormat string
15+
debug bool
16+
cluster = ClusterValue{"http://localhost:4001"}
17+
curlChan chan string
1418
)
1519

20+
func output(resp *etcd.Response) {
21+
switch outputFormat {
22+
case "json":
23+
b, err := json.Marshal(resp)
24+
if err != nil {
25+
panic(err)
26+
}
27+
fmt.Printf("%s\n", b)
28+
case "full":
29+
fmt.Printf("Index: %v\nValue: %v\n", resp.Index, resp.Value)
30+
case "value-only":
31+
fmt.Println(resp.Value)
32+
}
33+
}
34+
1635
func main() {
1736
flag.BoolVar(&printVersion, "version", false, "print the version and exit")
18-
19-
cluster := ClusterValue{"http://localhost:4001"}
2037
flag.Var(&cluster, "C", "a comma seperated list of machine addresses in the cluster e.g. 127.0.0.1:4001,127.0.0.1:4002")
38+
flag.StringVar(&outputFormat, "format", "json", "Output server response in the given format, either `json`, `full`, or `value-only`")
39+
flag.BoolVar(&debug, "debug", false, "Output cURL commands which can be used to re-produce the request")
2140
flag.Parse()
2241

2342
if printVersion {
2443
fmt.Println(releaseVersion)
2544
os.Exit(0)
2645
}
2746

47+
if debug {
48+
// Making the channel buffered to avoid potential deadlocks
49+
curlChan = make(chan string, 10)
50+
etcd.SetCurlChan(curlChan)
51+
}
52+
2853
client = etcd.NewClient(cluster.GetMachines())
2954

3055
args := flag.Args()
3156

3257
if len(args) == 0 {
58+
fmt.Println("Usage: etcdctl [flags] [command] [flags for command]\n")
59+
fmt.Println("Available flags include:\n")
60+
flag.PrintDefaults()
61+
fmt.Println()
62+
fmt.Println(`To see the usage for a specific command, run "etcdctl [command]"`)
3363
os.Exit(1)
3464
}
3565

@@ -42,8 +72,9 @@ func main() {
4272
os.Exit(MalformedEtcdctlArguments)
4373
}
4474

45-
if len(args) > command.maxArgs || len(args) < command.minArgs {
46-
fmt.Println("wrong arguments provided")
75+
commandArgs := args[1:]
76+
77+
if len(commandArgs) > command.maxArgs || len(commandArgs) < command.minArgs {
4778
fmt.Println(command.usage)
4879
os.Exit(MalformedEtcdctlArguments)
4980
}
@@ -53,9 +84,10 @@ func main() {
5384
os.Exit(FailedToConnectToHost)
5485
}
5586

56-
err := command.f(args)
87+
err := command.f(commandArgs)
5788

5889
if err != nil {
90+
fmt.Print("Error: ")
5991
fmt.Println(err)
6092
os.Exit(ErrorFromEtcd)
6193
}

get.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,74 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
6+
"github.com/coreos/go-etcd/etcd"
57
)
68

7-
const GetUsage = `usage: etcdctl [etcd flags] get <key>`
9+
const GetUsage = `usage: etcdctl [etcd flags] get <key> [get flags]
10+
special flags: --sort to return the result in sorted order
11+
--consistent to send request to the leader, thereby guranteeing that any earlier writes will be seen by the read`
12+
13+
const GetAllUsage = `usage: etcdctl [etcd flags] getAll <key> [getAll flags]
14+
special flags: --sort set to true to return the result in sorted order
15+
--consistent to send request to the leader, thereby guranteeing that any earlier writes will be seen by the read`
16+
17+
var (
18+
getFlag = flag.NewFlagSet("get", flag.ExitOnError)
19+
sorted = getFlag.Bool("sort", false,
20+
"Return the results in sorted order or not (default to false)")
21+
consistent = getFlag.Bool("consistent", false,
22+
"Send the request to the leader or not (default to false)")
23+
)
824

925
func init() {
10-
registerCommand("get", GetUsage, 2, 2, get)
26+
registerCommand("get", GetUsage, 1, 2, get)
27+
registerCommand("getAll", GetAllUsage, 1, 2, getAll)
1128
}
1229

1330
func get(args []string) error {
14-
key := args[1]
15-
resps, err := client.Get(key)
31+
if *consistent {
32+
client.SetConsistency(etcd.STRONG_CONSISTENCY)
33+
} else {
34+
client.SetConsistency(etcd.WEAK_CONSISTENCY)
35+
}
36+
37+
key := args[0]
38+
getFlag.Parse(args[1:])
39+
resp, err := client.Get(key, *sorted)
40+
if debug {
41+
fmt.Println(<-curlChan)
42+
}
43+
1644
if err != nil {
1745
return err
1846
}
19-
for _, resp := range resps {
20-
if resp.Value != "" {
21-
fmt.Println(resp.Value)
22-
}
47+
48+
output(resp)
49+
50+
return nil
51+
}
52+
53+
func getAll(args []string) error {
54+
if *consistent {
55+
client.SetConsistency(etcd.STRONG_CONSISTENCY)
56+
} else {
57+
client.SetConsistency(etcd.WEAK_CONSISTENCY)
58+
}
59+
60+
key := args[0]
61+
getFlag.Parse(args[1:])
62+
resp, err := client.GetAll(key, *sorted)
63+
if debug {
64+
fmt.Println(<-curlChan)
65+
}
66+
67+
if err != nil {
68+
return err
2369
}
70+
71+
output(resp)
72+
2473
return nil
2574
}

set.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)