Skip to content

Commit

Permalink
Merge branch 'release/v0.3.0'
Browse files Browse the repository at this point in the history
* release/v0.3.0:
  feat: add getentry command to olric-cli
  • Loading branch information
buraksezer committed Dec 9, 2020
2 parents 177c3f7 + 6604380 commit 258c655
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
1 change: 1 addition & 0 deletions cmd/olric-cli/cli/cli.go
Expand Up @@ -49,6 +49,7 @@ var completer = readline.NewPrefixCompleter(
readline.PcItem("incr"),
readline.PcItem("decr"),
readline.PcItem("expire"),
readline.PcItem("getentry"),
)

// CLI defines the command line client for Olric.
Expand Down
40 changes: 29 additions & 11 deletions cmd/olric-cli/cli/evaluate.go
Expand Up @@ -15,6 +15,7 @@
package cli

import (
"encoding/json"
"fmt"
"strconv"
"strings"
Expand All @@ -25,19 +26,34 @@ import (
)

const (
cmdPut string = "put"
cmdPutEx string = "putex"
cmdGet string = "get"
cmdDelete string = "delete"
cmdDestroy string = "destroy"
cmdExpire string = "expire"
cmdPutIf string = "putif"
cmdPutIfEx string = "putifex"
cmdIncr string = "incr"
cmdDecr string = "decr"
cmdGetPut string = "getput"
cmdPut string = "put"
cmdPutEx string = "putex"
cmdGet string = "get"
cmdDelete string = "delete"
cmdDestroy string = "destroy"
cmdExpire string = "expire"
cmdPutIf string = "putif"
cmdPutIfEx string = "putifex"
cmdIncr string = "incr"
cmdDecr string = "decr"
cmdGetPut string = "getput"
cmdGetEntry string = "getentry"
)

func (c *CLI) evalGetEntry(dm *client.DMap, fields []string) error {
key := strings.Join(fields, " ")
entry, err := dm.GetEntry(key)
if err != nil {
return err
}
text, err := json.MarshalIndent(entry, "", "\t")
if err != nil {
return err
}
c.print(fmt.Sprintf("%s\n", text))
return nil
}

func (c *CLI) evalPut(dm *client.DMap, fields []string) error {
if len(fields) < 1 {
return errInvalidCommand
Expand Down Expand Up @@ -227,6 +243,8 @@ func (c *CLI) evaluate(dmap, line string) error {
return c.evalPutIf(dm, fields)
case cmd == cmdPutIfEx:
return c.evalPutIfEx(dm, fields)
case cmd == cmdGetEntry:
return c.evalGetEntry(dm, fields)
default:
return fmt.Errorf("invalid command")
}
Expand Down
11 changes: 11 additions & 0 deletions cmd/olric-cli/cli/evaluate_test.go
Expand Up @@ -332,4 +332,15 @@ func TestEvaluate(t *testing.T) {
t.Fatalf("Expected olric.ErrKeyNotFound, Got: %v", err)
}
})

t.Run("run evalGetEntry", func(t *testing.T) {
_ = dm.Put("evalGetEntry-test", "evalGetEntry-test")
fields := []string{
"evalGetEntry-test",
}
err := c.evalGetEntry(dm, fields)
if err != nil {
t.Fatalf("Expected nil, Got: %v", err)
}
})
}
29 changes: 18 additions & 11 deletions cmd/olric-cli/cli/help.go
Expand Up @@ -98,19 +98,26 @@ func (c *CLI) helpGetPut() {
c.print("GetPut atomically sets key to value and returns the old value stored at key.\n")
}

func (c *CLI) helpGetEntry() {
c.print("# GetEntry #\n\n")
c.print(">> getentry <key>\n\n")
c.print("GetEntry gets the value for the given key. It returns ErrKeyNotFound if the DB does not contains the key. It's thread-safe.\n")
}

func (c *CLI) help(cmd string) error {
var commands = map[string]func(){
"put": c.helpPut,
"putif": c.helpPutIf,
"putex": c.helpPutEx,
"putIfEx": c.helpPutIfEx,
"get": c.helpGet,
"expire": c.helpExpire,
"delete": c.helpDelete,
"destroy": c.helpDestroy,
"incr": c.helpIncr,
"decr": c.helpDecr,
"getput": c.helpGetPut,
"put": c.helpPut,
"putif": c.helpPutIf,
"putex": c.helpPutEx,
"putIfEx": c.helpPutIfEx,
"get": c.helpGet,
"expire": c.helpExpire,
"delete": c.helpDelete,
"destroy": c.helpDestroy,
"incr": c.helpIncr,
"decr": c.helpDecr,
"getput": c.helpGetPut,
"getentry": c.helpGetEntry,
}

if cmd != "" {
Expand Down

0 comments on commit 258c655

Please sign in to comment.