works for r=1 w=1
parent
b7771dee17
commit
648ce19313
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package client
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
@ -65,7 +65,7 @@ func (c *Client) Get(key string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode == http.StatusNotFound {
|
if resp.StatusCode == http.StatusNotFound {
|
||||||
return nil, nil
|
return nil, errors.New("not found")
|
||||||
}
|
}
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, errors.New("bad status on get")
|
return nil, errors.New("bad status on get")
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package client
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue