From 49d91a8a61ca894792b3da0aeb2c26409b1d6083 Mon Sep 17 00:00:00 2001 From: Osipov Konstantin Date: Mon, 9 Dec 2019 03:24:36 +0300 Subject: [PATCH] Added ability to escape quotes and small refactoring --- .gitignore | 4 ++-- README.md | 11 +++++++---- cliutil/cliutil.go | 6 +++--- commands/closeCommand.go | 6 +++--- commands/deleteCommand.go | 10 +++++----- commands/error.go | 31 ++++++++++++++++--------------- commands/getCommand.go | 10 +++++----- commands/openCommand.go | 6 +++--- commands/setCommand.go | 10 +++++----- commands/showCommand.go | 20 ++++++++++---------- go.mod | 11 +++++++++++ go.sum | 27 +++++++++++++++++++++++++++ main.go | 34 +++++++++++++++++----------------- 13 files changed, 114 insertions(+), 72 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index 209552a..f2ecd3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/.idea -*.iml \ No newline at end of file +.idea +vendor/ diff --git a/README.md b/README.md index 817794f..415cd9e 100644 --- a/README.md +++ b/README.md @@ -35,11 +35,14 @@ testdb» set key200 value200 Success testdb» set key300 value300 Success +testdb» set "key \"123" value +Success testdb» show prefix key -Key | Value -key100 | value100 -key200 | value200 -key300 | value300 +Key | Value +key100 | value100 +key200 | value200 +key300 | value300 +key \"123 | value testdb» show range key2 key3 Key | Value diff --git a/cliutil/cliutil.go b/cliutil/cliutil.go index 61e9ecf..449ee4e 100644 --- a/cliutil/cliutil.go +++ b/cliutil/cliutil.go @@ -7,10 +7,10 @@ package cliutil import ( - "gopkg.in/mgo.v2/bson" - "github.com/TomiHiltunen/geohash-golang" - "fmt" "encoding/binary" + "fmt" + "github.com/TomiHiltunen/geohash-golang" + "gopkg.in/mgo.v2/bson" "math" ) diff --git a/commands/closeCommand.go b/commands/closeCommand.go index ece0c7f..78a8ab3 100644 --- a/commands/closeCommand.go +++ b/commands/closeCommand.go @@ -15,14 +15,14 @@ import ( // // Returns a string containing information about the result of the operation. func Close() string { - if (!isConnected) { - return AppError(ERR_DB_DOES_NOT_OPEN) + if !isConnected { + return AppError(ErrDbDoesNotOpen) } err := dbh.Close() if err != nil { return fmt.Sprintf( - AppError(ERR_COULD_NOT_CLOSE_DATABASE), + AppError(ErrCouldNotCloseDatabase), err.Error(), ) } diff --git a/commands/deleteCommand.go b/commands/deleteCommand.go index 6362d5a..0555653 100644 --- a/commands/deleteCommand.go +++ b/commands/deleteCommand.go @@ -15,18 +15,18 @@ import ( // // Returns a string containing information about the result of the operation. func Delete(key string) string { - if (!isConnected) { - return AppError(ERR_DB_DOES_NOT_OPEN) + if !isConnected { + return AppError(ErrDbDoesNotOpen) } - if (key == "") { - return AppError(ERR_KEY_IS_EMPTY) + if key == "" { + return AppError(ErrKeyIsEmpty) } err := dbh.Delete([]byte(key), nil) if err != nil { return fmt.Sprintf( - AppError(ERR_UNABLE_TO_DELETE), + AppError(ErrUnableToDelete), err.Error(), ) } diff --git a/commands/error.go b/commands/error.go index c0d8e07..73cf8c9 100644 --- a/commands/error.go +++ b/commands/error.go @@ -5,32 +5,33 @@ // This software provides a console interface to leveldb. package commands + import "fmt" -const ERR_DB_DOES_NOT_OPEN = 1001 -const ERR_OPENING_DATABASE = 1002 -const ERR_UNABLE_TO_WRITE = 1003 -const ERR_KEY_IS_EMPTY = 1004 -const ERR_UNABLE_TO_DELETE = 1005 -const ERR_COULD_NOT_CLOSE_DATABASE = 1006 -const ERR_KEY_NOT_FOUND = 1007 +const ErrDbDoesNotOpen = 1001 +const ErrOpeningDatabase = 1002 +const ErrUnableToWrite = 1003 +const ErrKeyIsEmpty = 1004 +const ErrUnableToDelete = 1005 +const ErrCouldNotCloseDatabase = 1006 +const ErrKeyNotFound = 1007 // Error messages list var errorMessages = map[int]string{ - ERR_DB_DOES_NOT_OPEN: "Database does not open", - ERR_OPENING_DATABASE: "Error opening database `%s`", - ERR_UNABLE_TO_WRITE: "Unable to write [`%s`]", - ERR_KEY_IS_EMPTY: "Key is exmpty", - ERR_UNABLE_TO_DELETE: "Unable to delete [`%s`]", - ERR_COULD_NOT_CLOSE_DATABASE: "Could not close database [`%s`]", - ERR_KEY_NOT_FOUND: "Key not found", + ErrDbDoesNotOpen: "Database does not open", + ErrOpeningDatabase: "Error opening database `%s`", + ErrUnableToWrite: "Unable to write [`%s`]", + ErrKeyIsEmpty: "Key is exmpty", + ErrUnableToDelete: "Unable to delete [`%s`]", + ErrCouldNotCloseDatabase: "Could not close database [`%s`]", + ErrKeyNotFound: "Key not found", } // The wrapper for outputting errors in the application // Returns the text of the error func AppError(code int) string { msg, ok := errorMessages[code] - if (ok) { + if ok { return fmt.Sprintf("Error %d: %s!", code, msg) } diff --git a/commands/getCommand.go b/commands/getCommand.go index d2cbe85..cdd61bb 100644 --- a/commands/getCommand.go +++ b/commands/getCommand.go @@ -15,17 +15,17 @@ import ( // // Returns a string containing information about the result of the operation. func Get(key, format string) string { - if (!isConnected) { - return AppError(ERR_DB_DOES_NOT_OPEN) + if !isConnected { + return AppError(ErrDbDoesNotOpen) } - if (key == "") { - return AppError(ERR_KEY_IS_EMPTY) + if key == "" { + return AppError(ErrKeyIsEmpty) } value, err := dbh.Get([]byte(key), nil) if err != nil { - return AppError(ERR_KEY_NOT_FOUND) + return AppError(ErrKeyNotFound) } return cliutil.ToString(format, value) diff --git a/commands/openCommand.go b/commands/openCommand.go index f6c4f84..edcac82 100644 --- a/commands/openCommand.go +++ b/commands/openCommand.go @@ -7,8 +7,8 @@ package commands import ( - "github.com/syndtr/goleveldb/leveldb" "fmt" + "github.com/syndtr/goleveldb/leveldb" ) // Database connection @@ -24,8 +24,8 @@ var isConnected bool func Open(file string) string { var err error dbh, err = leveldb.OpenFile(file, nil) - if (err != nil) { - return fmt.Sprintf(AppError(ERR_OPENING_DATABASE), file) + if err != nil { + return fmt.Sprintf(AppError(ErrOpeningDatabase), file) } isConnected = true diff --git a/commands/setCommand.go b/commands/setCommand.go index 1df2221..689f663 100644 --- a/commands/setCommand.go +++ b/commands/setCommand.go @@ -15,18 +15,18 @@ import ( // // Returns a string containing information about the result of the operation. func Set(key, value string) string { - if (!isConnected) { - return AppError(ERR_DB_DOES_NOT_OPEN) + if !isConnected { + return AppError(ErrDbDoesNotOpen) } - if (key == "") { - return AppError(ERR_KEY_IS_EMPTY) + if key == "" { + return AppError(ErrKeyIsEmpty) } err := dbh.Put([]byte(key), []byte(value), nil) if err != nil { return fmt.Sprintf( - AppError(ERR_UNABLE_TO_WRITE), + AppError(ErrUnableToWrite), err.Error(), ) } diff --git a/commands/showCommand.go b/commands/showCommand.go index ba4b89e..9e60ac9 100644 --- a/commands/showCommand.go +++ b/commands/showCommand.go @@ -7,13 +7,13 @@ package commands import ( - "github.com/syndtr/goleveldb/leveldb/util" - "github.com/syndtr/goleveldb/leveldb/iterator" + "bufio" + "bytes" "fmt" - "text/tabwriter" "github.com/liderman/leveldb-cli/cliutil" - "bytes" - "bufio" + "github.com/syndtr/goleveldb/leveldb/iterator" + "github.com/syndtr/goleveldb/leveldb/util" + "text/tabwriter" ) // It shows the contents of the database prefix filtering. @@ -22,8 +22,8 @@ import ( // // Returns a string containing information about the result of the operation. func ShowByPrefix(prefix, format string) string { - if (!isConnected) { - return AppError(ERR_DB_DOES_NOT_OPEN) + if !isConnected { + return AppError(ErrDbDoesNotOpen) } return showByIterator( @@ -38,8 +38,8 @@ func ShowByPrefix(prefix, format string) string { // // Returns a string containing information about the result of the operation. func ShowByRange(start, limit, format string) string { - if (!isConnected) { - return AppError(ERR_DB_DOES_NOT_OPEN) + if !isConnected { + return AppError(ErrDbDoesNotOpen) } return showByIterator( @@ -73,7 +73,7 @@ func showByIterator(iter iterator.Iterator, format string) string { iter.Release() err := iter.Error() - if (err != nil) { + if err != nil { return "Error iterator!" } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1a0e6ab --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/liderman/leveldb-cli + +go 1.12 + +require ( + bitbucket.org/creachadair/shell v0.0.6 + github.com/TomiHiltunen/geohash-golang v0.0.0-20150112065804-b3e4e625abfb + github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e + github.com/syndtr/goleveldb v1.0.0 + gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1d4ef7e --- /dev/null +++ b/go.sum @@ -0,0 +1,27 @@ +bitbucket.org/creachadair/shell v0.0.6 h1:reJflDbKqnlnqb4Oo2pQ1/BqmY/eCWcNGHrIUO8qIzc= +bitbucket.org/creachadair/shell v0.0.6/go.mod h1:8Qqi/cYk7vPnsOePHroKXDJYmb5x7ENhtiFtfZq8K+M= +github.com/TomiHiltunen/geohash-golang v0.0.0-20150112065804-b3e4e625abfb h1:wumPkzt4zaxO4rHPBrjDK8iZMR41C1qs7njNqlacwQg= +github.com/TomiHiltunen/geohash-golang v0.0.0-20150112065804-b3e4e625abfb/go.mod h1:QiYsIBRQEO+Z4Rz7GoI+dsHVneZNONvhczuA+llOZNM= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w= +github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= +github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw= +gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 365c15c..f8a71c8 100644 --- a/main.go +++ b/main.go @@ -11,18 +11,18 @@ package main import ( + "bitbucket.org/creachadair/shell" "fmt" "github.com/chzyer/readline" - "strconv" - "strings" "github.com/liderman/leveldb-cli/commands" "os" "path" "runtime" + "strconv" ) // Software version number -const VERSION = "0.1.8" +const VERSION = "0.2.0" var completer = readline.NewPrefixCompleter( readline.PcItem("show", @@ -41,7 +41,7 @@ var completer = readline.NewPrefixCompleter( // Main function func main() { - l, err := readline.NewEx(&readline.Config { + l, err := readline.NewEx(&readline.Config{ Prompt: "\033[31m»\033[0m ", HistoryFile: "/tmp/leveldb-cli.tmp", AutoComplete: completer, @@ -64,7 +64,7 @@ func main() { break } - args := strings.Split(line, " ") + args, _ := shell.Split(line) switch { // Command: version case line == "version": @@ -88,12 +88,12 @@ func main() { switch args[1] { // Sub-command: range case "range": - if (len(args) < 4 || len(args) > 5) { + if len(args) < 4 || len(args) > 5 { fmt.Println("Bad format. Please use 'show range START LIMIT [FORMAT]'") break } - format := "" + format := "" if len(args) == 5 { format = args[4] } @@ -102,13 +102,13 @@ func main() { break // Sub-command: prefix case "prefix": - if (len(args) < 3 || len(args) > 4) { + if len(args) < 3 || len(args) > 4 { fmt.Println("Bad format. Please use 'show prefix PREFIX [FORMAT]'") break } - format := "" - if (len(args) == 4) { + format := "" + if len(args) == 4 { format = args[3] } @@ -129,13 +129,13 @@ func main() { break // Command: get case args[0] == "get": - if (len(args) < 2 || len(args) > 3) { + if len(args) < 2 || len(args) > 3 { fmt.Println("Bad format. Please use 'get KEY FORMAT'") break } - format := "" - if (len(args) == 3) { + format := "" + if len(args) == 3 { format = args[2] } @@ -144,7 +144,7 @@ func main() { // Command: delete case args[0] == "delete": if len(args) != 2 { - fmt.Printf("Bad format. Please use 'delete KEY'", args[0]) + fmt.Print("Bad format. Please use 'delete KEY'") break } @@ -153,7 +153,7 @@ func main() { // Command: close case args[0] == "close": if len(args) != 1 { - fmt.Printf("Bad format. Please use 'close'", args[0]) + fmt.Print("Bad format. Please use 'close'") break } @@ -180,5 +180,5 @@ func main() { } } - exit: -} \ No newline at end of file +exit: +}