From 648ce1931324011e0c468bca3d8cd92e9881ef21 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 15 Mar 2019 09:45:25 -0600 Subject: [PATCH] works for r=1 w=1 --- client/cli.go | 58 +++++++++++++++++++++++++++++++++++++++++++ client/client.go | 4 +-- client/client_test.go | 2 +- 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 client/cli.go diff --git a/client/cli.go b/client/cli.go new file mode 100644 index 0000000..6223826 --- /dev/null +++ b/client/cli.go @@ -0,0 +1,58 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strings" +) + +func main() { + client, err := New(strings.Split(os.Getenv("ADDR"), ",")...) + if err != nil { + panic(err) + } + reader := bufio.NewReader(os.Stdin) + for { + fmt.Print("> ") + line, err := reader.ReadString('\n') + if err != nil { + panic(err) + } + do(client, strings.TrimSpace(line)) + } +} + +func do(client *Client, line string) { + defer func() { + if err := recover(); err != nil { + log.Printf("err: %v", err) + } + }() + switch strings.ToLower(strings.Split(line, " ")[0]) { + case "get": + get(client, line) + case "set": + set(client, line) + case "put": + set(client, line) + default: + log.Printf("unknown command %v", line) + } +} + +func get(client *Client, line string) { + words := strings.Split(line, " ") + key := words[1] + v, err := client.Get(key) + log.Printf("set: %v: %s", err, v) +} + +func set(client *Client, line string) { + words := strings.Split(line, " ") + key := words[1] + value := strings.Join(words[2:], " ") + err := client.Set(key, []byte(value)) + log.Printf("set: %v", err) +} diff --git a/client/client.go b/client/client.go index c24420d..0e17351 100644 --- a/client/client.go +++ b/client/client.go @@ -1,4 +1,4 @@ -package client +package main import ( "bytes" @@ -65,7 +65,7 @@ func (c *Client) Get(key string) ([]byte, error) { } defer resp.Body.Close() if resp.StatusCode == http.StatusNotFound { - return nil, nil + return nil, errors.New("not found") } if resp.StatusCode != http.StatusOK { return nil, errors.New("bad status on get") diff --git a/client/client_test.go b/client/client_test.go index 92c17a9..daad964 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1,4 +1,4 @@ -package client +package main import ( "fmt"