Skip to content

Commit

Permalink
Added ability to escape quotes and small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
liderman committed Dec 9, 2019
1 parent a64483a commit 49d91a8
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 72 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/.idea
*.iml
.idea
vendor/
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cliutil/cliutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
6 changes: 3 additions & 3 deletions commands/closeCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
Expand Down
10 changes: 5 additions & 5 deletions commands/deleteCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
Expand Down
31 changes: 16 additions & 15 deletions commands/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
10 changes: 5 additions & 5 deletions commands/getCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions commands/openCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package commands

import (
"github.com/syndtr/goleveldb/leveldb"
"fmt"
"github.com/syndtr/goleveldb/leveldb"
)

// Database connection
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions commands/setCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
Expand Down
20 changes: 10 additions & 10 deletions commands/showCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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!"
}

Expand Down
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
27 changes: 27 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Loading

0 comments on commit 49d91a8

Please sign in to comment.