From 49206da96a63a670a82d22f476df877b688b187b Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Wed, 15 Dec 2021 22:21:01 -0800 Subject: [PATCH 1/3] Add: search and help commands --- cli/completer.go | 2 ++ cli/executor.go | 4 +++- cli/grammar.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/cli/completer.go b/cli/completer.go index 1b17342..270a940 100644 --- a/cli/completer.go +++ b/cli/completer.go @@ -31,9 +31,11 @@ func cmdCompleter(d prompt.Document, cmd string) []prompt.Suggest { {Text: "ttl", Description: "ttl command"}, {Text: "show", Description: "show info"}, {Text: "keys", Description: "iterate keys"}, + {Text: "search", Description: "search for string in all values"}, {Text: "use", Description: "change db"}, {Text: "exit", Description: "exit buntdb shell client"}, {Text: "drop", Description: "drop the index"}, + {Text: "help", Description: "show available commands"}, } tx, _ := db.GetCurrentTransaction() if tx == nil { diff --git a/cli/executor.go b/cli/executor.go index 21db9d3..a99c039 100644 --- a/cli/executor.go +++ b/cli/executor.go @@ -7,7 +7,7 @@ import ( "strings" ) -type transactionRequireType int64 +type transactionRequireType uint8 const ( noNeed transactionRequireType = iota @@ -38,10 +38,12 @@ func BuntdbExecutor(s string) { } grammar := NewGrammar() + k := kong.Must( grammar, kong.Exit(grammar.ExitWrapper), ) + ctx, err := k.Parse(args) if grammar.Exit { return diff --git a/cli/grammar.go b/cli/grammar.go index fe41ead..d503690 100644 --- a/cli/grammar.go +++ b/cli/grammar.go @@ -6,6 +6,7 @@ import ( "github.com/alecthomas/kong" "github.com/tidwall/buntdb" "os" + "strings" "time" ) @@ -103,6 +104,44 @@ func (k KeysGrammar) Run(ctx *kong.Context, tx *buntdb.Tx) error { }) } +type SearchGrammar struct { + Pattern string `arg:"" help:"the match pattern "` + Delete bool `optional:"" short:"c" help:"delete all keys found (DANGEROUS)"` +} + +func (s SearchGrammar) Run(ctx *kong.Context, tx *buntdb.Tx) error { + var keys []string + err := tx.AscendKeys("*", func(key, value string) bool { + if strings.Contains(value, s.Pattern) { + keys = append(keys, key) + fmt.Fprintf(ctx.Stdout, "%v : %v\n", key, value) + } + return true + }) + + if err != nil { + return err + } + + if !s.Delete { + return nil + } + + fmt.Fprintf(ctx.Stdout, "\n\n-------------------\n\nFound %d keys, written above. \nSleeping 10 seconds before we delete.", len(keys)) + for n := 0; n != 10; n++ { + time.Sleep(1 * time.Second) + fmt.Fprintf(ctx.Stdout, ".") + } + + for _, k := range keys { + _, err := tx.Delete(k) + if err == nil { + fmt.Fprintln(ctx.Stdout, "Deleted: %v", k) + } + } + return nil +} + type UseGrammar struct { Path string `arg:"" help:"the new db path"` Create bool `optional:"" short:"c" help:"create new db if path doesn't exist'"` @@ -215,6 +254,18 @@ func (s *DropIndexGrammar) Run(ctx *kong.Context, tx *buntdb.Tx) error { return tx.DropIndex(s.Name) } +type HelpGrammar struct { + // +} + +func (h *HelpGrammar) Run(ctx *kong.Context) (err error) { + commands := "get\nset\ndel\nttl\nrbegin (begin a readonly transaction)\nrwbegin (begin a read/write transaction)\ncommit\nrollback\nshow\nkeys\nsearch\nuse\nshrink" + _, err = fmt.Fprintln(ctx.Stdout, + "Commands available:\n----------\n"+commands+"\n----------\nFor more help, try running a command with the -h switch.", + ) + return +} + type Grammar struct { Get GetGrammar `cmd:"" help:"get a value from key, return the value if key exists, or if non-exists."` Set SetGrammar `cmd:"" help:"set a key-value [ttl], return the old value, or if old value doesn't exist."` @@ -230,6 +281,8 @@ type Grammar struct { Shrink ShrinkGrammar `cmd:"" help:"run database shrink command"` Save SaveGrammar `cmd:"" help:"save the db to file"` Drop DropGrammar `cmd:"" help:"drop command"` + Search SearchGrammar `cmd:"" help:"Search for a string contained in any values"` + Help HelpGrammar `cmd:""` Exit bool `kong:"-"` } From 47a77c69ac5ecfee8b820c93a3416ddd7a781845 Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Thu, 16 Dec 2021 00:14:07 -0800 Subject: [PATCH 2/3] Fix printf issues --- cli/grammar.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cli/grammar.go b/cli/grammar.go index d503690..762ddd7 100644 --- a/cli/grammar.go +++ b/cli/grammar.go @@ -127,16 +127,19 @@ func (s SearchGrammar) Run(ctx *kong.Context, tx *buntdb.Tx) error { return nil } - fmt.Fprintf(ctx.Stdout, "\n\n-------------------\n\nFound %d keys, written above. \nSleeping 10 seconds before we delete.", len(keys)) + fmt.Fprintf(ctx.Stdout, "\n\n-------------------\n\nFound %d keys, written above. \nSleeping 10 seconds before we delete.\n", len(keys)) for n := 0; n != 10; n++ { time.Sleep(1 * time.Second) fmt.Fprintf(ctx.Stdout, ".") } - for _, k := range keys { + for i, k := range keys { + if i == 1 { + fmt.Fprint(ctx.Stdout, "\n") + } _, err := tx.Delete(k) if err == nil { - fmt.Fprintln(ctx.Stdout, "Deleted: %v", k) + fmt.Fprintf(ctx.Stdout, "Deleted: %v\n", k) } } return nil From de7735df1fb28205c925766bc05d8b72044a7df9 Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Thu, 16 Dec 2021 00:27:26 -0800 Subject: [PATCH 3/3] Fix embarassing index comparison --- cli/grammar.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/grammar.go b/cli/grammar.go index 762ddd7..93d5fc2 100644 --- a/cli/grammar.go +++ b/cli/grammar.go @@ -127,14 +127,14 @@ func (s SearchGrammar) Run(ctx *kong.Context, tx *buntdb.Tx) error { return nil } - fmt.Fprintf(ctx.Stdout, "\n\n-------------------\n\nFound %d keys, written above. \nSleeping 10 seconds before we delete.\n", len(keys)) + fmt.Fprintf(ctx.Stdout, "\n-------------------\n\nFound %d keys, written above. \nSleeping 10 seconds before we delete.\n", len(keys)) for n := 0; n != 10; n++ { time.Sleep(1 * time.Second) fmt.Fprintf(ctx.Stdout, ".") } for i, k := range keys { - if i == 1 { + if i == 0 { fmt.Fprint(ctx.Stdout, "\n") } _, err := tx.Delete(k)