|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "flag" |
4 | 5 | "fmt" |
| 6 | + "github.com/coreos/go-etcd/etcd" |
5 | 7 | ) |
6 | 8 |
|
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 | +) |
8 | 24 |
|
9 | 25 | func init() { |
10 | | - registerCommand("get", GetUsage, 2, 2, get) |
| 26 | + registerCommand("get", GetUsage, 1, 2, get) |
| 27 | + registerCommand("getAll", GetAllUsage, 1, 2, getAll) |
11 | 28 | } |
12 | 29 |
|
13 | 30 | 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 | + |
16 | 44 | if err != nil { |
17 | 45 | return err |
18 | 46 | } |
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 |
23 | 69 | } |
| 70 | + |
| 71 | + output(resp) |
| 72 | + |
24 | 73 | return nil |
25 | 74 | } |
0 commit comments