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

Commit

Permalink
Merge pull request #16 from derekchiang/0.2
Browse files Browse the repository at this point in the history
A set of changes to update etcdctl to use go-etcd 0.2
  • Loading branch information
philips committed Nov 12, 2013
2 parents 5aaeca1 + e8db337 commit cfc6f36
Show file tree
Hide file tree
Showing 36 changed files with 1,645 additions and 865 deletions.
42 changes: 42 additions & 0 deletions compare_and_swap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"flag"
"fmt"
)

const CompareAndSwapUsage = `usage: etcdctl [etcd flags] compareAndSwap <key> <value> [testAndSet flags]
either prevvalue or previndex needs to be given
special flags: --ttl to set a key with ttl
--prevvalue to set the previous value
--previndex to set the previous index`

var (
compareAndSwapFlag = flag.NewFlagSet("testAndSet", flag.ExitOnError)
compareAndSwapTtl = compareAndSwapFlag.Uint64("ttl", 0, "ttl of the key")
compareAndSwapPvalue = compareAndSwapFlag.String("prevvalue", "", "previous value")
compareAndSwapPindex = compareAndSwapFlag.Uint64("previndex", 0, "previous index")
)

func init() {
// The minimum number of arguments is 3 because
// there needs to be either pvalue or pindex
registerCommand("compareAndSwap", CompareAndSwapUsage, 3, 6, compareAndSwap)
}

func compareAndSwap(args []string) error {
key := args[0]
value := args[1]
compareAndSwapFlag.Parse(args[2:])
resp, err := client.CompareAndSwap(key, value,
*compareAndSwapTtl, *compareAndSwapPvalue, *compareAndSwapPindex)
if debug {
fmt.Println(<-curlChan)
}
if err != nil {
return err
}

output(resp)
return nil
}
26 changes: 23 additions & 3 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,39 @@ import (
)

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

func init() {
registerCommand("delete", DeleteUsage, 2, 2, delete)
registerCommand("delete", DeleteUsage, 1, 1, delete)
registerCommand("deleteAll", DeleteAllUsage, 1, 1, deleteAll)
}

func delete(args []string) error {
key := args[1]
key := args[0]

resp, err := client.Delete(key)
if debug {
fmt.Println(<-curlChan)
}
if err != nil {
return err
}
output(resp)

return nil
}

func deleteAll(args []string) error {
key := args[0]

resp, err := client.DeleteAll(key)
if debug {
fmt.Println(<-curlChan)
}
if err != nil {
return err
}
fmt.Println(resp.PrevValue)
output(resp)

return nil
}
46 changes: 39 additions & 7 deletions etcdctl.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,65 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"github.com/coreos/go-etcd/etcd"
"os"
)

var (
client *etcd.Client

client *etcd.Client
printVersion bool
outputFormat string
debug bool
cluster = ClusterValue{"http://localhost:4001"}
curlChan chan string
)

func output(resp *etcd.Response) {
switch outputFormat {
case "json":
b, err := json.Marshal(resp)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", b)
case "full":
fmt.Printf("Index: %v\nValue: %v\n", resp.Index, resp.Value)
case "value-only":
fmt.Println(resp.Value)
}
}

func main() {
flag.BoolVar(&printVersion, "version", false, "print the version and exit")

cluster := ClusterValue{"http://localhost:4001"}
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")
flag.StringVar(&outputFormat, "format", "json", "Output server response in the given format, either `json`, `full`, or `value-only`")
flag.BoolVar(&debug, "debug", false, "Output cURL commands which can be used to re-produce the request")
flag.Parse()

if printVersion {
fmt.Println(releaseVersion)
os.Exit(0)
}

if debug {
// Making the channel buffered to avoid potential deadlocks
curlChan = make(chan string, 10)
etcd.SetCurlChan(curlChan)
}

client = etcd.NewClient(cluster.GetMachines())

args := flag.Args()

if len(args) == 0 {
fmt.Println("Usage: etcdctl [flags] [command] [flags for command]\n")
fmt.Println("Available flags include:\n")
flag.PrintDefaults()
fmt.Println()
fmt.Println(`To see the usage for a specific command, run "etcdctl [command]"`)
os.Exit(1)
}

Expand All @@ -42,8 +72,9 @@ func main() {
os.Exit(MalformedEtcdctlArguments)
}

if len(args) > command.maxArgs || len(args) < command.minArgs {
fmt.Println("wrong arguments provided")
commandArgs := args[1:]

if len(commandArgs) > command.maxArgs || len(commandArgs) < command.minArgs {
fmt.Println(command.usage)
os.Exit(MalformedEtcdctlArguments)
}
Expand All @@ -53,9 +84,10 @@ func main() {
os.Exit(FailedToConnectToHost)
}

err := command.f(args)
err := command.f(commandArgs)

if err != nil {
fmt.Print("Error: ")
fmt.Println(err)
os.Exit(ErrorFromEtcd)
}
Expand Down
65 changes: 57 additions & 8 deletions get.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
package main

import (
"flag"
"fmt"
"github.com/coreos/go-etcd/etcd"
)

const GetUsage = `usage: etcdctl [etcd flags] get <key>`
const GetUsage = `usage: etcdctl [etcd flags] get <key> [get flags]
special flags: --sort to return the result in sorted order
--consistent to send request to the leader, thereby guranteeing that any earlier writes will be seen by the read`

const GetAllUsage = `usage: etcdctl [etcd flags] getAll <key> [getAll flags]
special flags: --sort set to true to return the result in sorted order
--consistent to send request to the leader, thereby guranteeing that any earlier writes will be seen by the read`

var (
getFlag = flag.NewFlagSet("get", flag.ExitOnError)
sorted = getFlag.Bool("sort", false,
"Return the results in sorted order or not (default to false)")
consistent = getFlag.Bool("consistent", false,
"Send the request to the leader or not (default to false)")
)

func init() {
registerCommand("get", GetUsage, 2, 2, get)
registerCommand("get", GetUsage, 1, 2, get)
registerCommand("getAll", GetAllUsage, 1, 2, getAll)
}

func get(args []string) error {
key := args[1]
resps, err := client.Get(key)
if *consistent {
client.SetConsistency(etcd.STRONG_CONSISTENCY)
} else {
client.SetConsistency(etcd.WEAK_CONSISTENCY)
}

key := args[0]
getFlag.Parse(args[1:])
resp, err := client.Get(key, *sorted)
if debug {
fmt.Println(<-curlChan)
}

if err != nil {
return err
}
for _, resp := range resps {
if resp.Value != "" {
fmt.Println(resp.Value)
}

output(resp)

return nil
}

func getAll(args []string) error {
if *consistent {
client.SetConsistency(etcd.STRONG_CONSISTENCY)
} else {
client.SetConsistency(etcd.WEAK_CONSISTENCY)
}

key := args[0]
getFlag.Parse(args[1:])
resp, err := client.GetAll(key, *sorted)
if debug {
fmt.Println(<-curlChan)
}

if err != nil {
return err
}

output(resp)

return nil
}
31 changes: 0 additions & 31 deletions set.go

This file was deleted.

Loading

0 comments on commit cfc6f36

Please sign in to comment.