diff --git a/bin/.gitignore b/bin/.gitignore deleted file mode 100644 index d6b7ef3..0000000 --- a/bin/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/leveldb.nimble b/leveldb.nimble index c0ee77f..7b0eabf 100644 --- a/leveldb.nimble +++ b/leveldb.nimble @@ -6,8 +6,7 @@ description = "LevelDB wrapper for Nim" license = "MIT" srcDir = "src" installExt = @["nim"] -binDir = "bin" -bin = @["leveldb"] +bin = @["leveldbtool"] # Dependencies diff --git a/src/leveldb.nim b/src/leveldb.nim index 6644f54..1303259 100644 --- a/src/leveldb.nim +++ b/src/leveldb.nim @@ -1,4 +1,5 @@ -import options, os, strutils, leveldbpkg/raw +import options, os, strutils +import leveldb/raw type LevelDb* = ref object @@ -239,130 +240,3 @@ proc repairDb*(name: string) = var errPtr: cstring = nil leveldb_repair_db(options, name, addr errPtr) checkError(errPtr) - -when isMainModule: - import os - - proc usage() = - echo "LevelDB client" - echo "" - echo "Usage:" - echo " leveldb [-d ] create" - echo " leveldb [-d ] get [-x | --hex]" - echo " leveldb [-d ] put [-x | --hex]" - echo " leveldb [-d ] list [-x | --hex]" - echo " leveldb [-d ] keys" - echo " leveldb [-d ] delete " - echo " leveldb [-d ] repair" - echo " leveldb -h | --help" - echo " leveldb -v | --version" - echo "" - echo "Options:" - echo " -d --database Database path" - echo " -x --hex binary value in uppercase hex" - echo " -h --help Show this screen" - echo " -v --version Show version" - quit() - - var args = commandLineParams() - - if "-h" in args or "--help" in args or len(args) == 0: - usage() - - if "-v" in args or "--version" in args: - echo "leveldb.nim ", version - let (major, minor) = getLibVersion() - echo "leveldb ", major, ".", minor - quit() - - proc findArg(s: seq[string], item: string): int = - result = find(s, item) - let stop = find(s, "--") - if stop >= 0 and stop <= result: - result = -1 - - var dbPath = "./" - var i = findArg(args, "-d") - var j = findArg(args, "--database") - if i >= 0 and j >= 0: - quit("Please specify database path one time only.") - i = max(i, j) - if i >= 0: - if (i + 1) < len(args): - dbPath = args[i+1] - args.delete(i+1) - args.delete(i) - else: - quit("Please specify database path.") - - var hex = false - i = findArg(args, "-x") - j = findArg(args, "--hex") - if i >= 0: - hex = true - args.delete(i) - if j >= 0: - hex = true - args.delete(j) - - # drop stop word - if "--" in args: - args.delete(args.find("--")) - - if len(args) == 0: - usage() - - proc checkCommand(args: seq[string], requires: int) = - if len(args) < requires + 1: - quit("Command " & args[0] & " requires at least " & $(requires) & " arguments.") - - import strutils - - var db: LevelDb - var key, value: string - if args[0] == "create": - db = open(dbPath) - db.close() - elif args[0] == "get": - checkCommand(args, 1) - db = open(dbPath) - key = args[1] - let val = db.get(key) - if val.isNone(): - quit() - else: - if hex: - echo val.get().toHex() - else: - echo val.get() - db.close() - elif args[0] == "put": - checkCommand(args, 2) - db = open(dbPath) - key = args[1] - value = args[2] - if hex: - value = parseHexStr(value) - db.put(key, value) - db.close() - elif args[0] == "list": - db = open(dbPath) - for key, value in db.iter(): - if hex: - echo key, " ", value.toHex() - else: - echo key, " ", value - db.close() - elif args[0] == "keys": - db = open(dbPath) - for key, value in db.iter(): - echo key - db.close() - elif args[0] == "delete": - checkCommand(args, 1) - db = open(dbPath) - key = args[1] - db.delete(key) - db.close() - elif args[0] == "repair": - repairDb(dbPath) diff --git a/src/leveldbpkg/raw.nim b/src/leveldb/raw.nim similarity index 100% rename from src/leveldbpkg/raw.nim rename to src/leveldb/raw.nim diff --git a/src/leveldbtool.nim b/src/leveldbtool.nim new file mode 100644 index 0000000..1a485cf --- /dev/null +++ b/src/leveldbtool.nim @@ -0,0 +1,129 @@ +import options, os, strutils +import leveldb + +proc tool() = + proc usage() = + echo "LevelDB client" + echo "" + echo "Usage:" + echo " leveldb [-d ] create" + echo " leveldb [-d ] get [-x | --hex]" + echo " leveldb [-d ] put [-x | --hex]" + echo " leveldb [-d ] list [-x | --hex]" + echo " leveldb [-d ] keys" + echo " leveldb [-d ] delete " + echo " leveldb [-d ] repair" + echo " leveldb -h | --help" + echo " leveldb -v | --version" + echo "" + echo "Options:" + echo " -d --database Database path" + echo " -x --hex binary value in uppercase hex" + echo " -h --help Show this screen" + echo " -v --version Show version" + quit() + + var args = commandLineParams() + + if "-h" in args or "--help" in args or len(args) == 0: + usage() + + if "-v" in args or "--version" in args: + echo "leveldb.nim ", version + let (major, minor) = getLibVersion() + echo "leveldb ", major, ".", minor + quit() + + proc findArg(s: seq[string], item: string): int = + result = find(s, item) + let stop = find(s, "--") + if stop >= 0 and stop <= result: + result = -1 + + var dbPath = "./" + var i = findArg(args, "-d") + var j = findArg(args, "--database") + if i >= 0 and j >= 0: + quit("Please specify database path one time only.") + i = max(i, j) + if i >= 0: + if (i + 1) < len(args): + dbPath = args[i+1] + args.delete(i+1) + args.delete(i) + else: + quit("Please specify database path.") + + var hex = false + i = findArg(args, "-x") + j = findArg(args, "--hex") + if i >= 0: + hex = true + args.delete(i) + if j >= 0: + hex = true + args.delete(j) + + # drop stop word + if "--" in args: + args.delete(args.find("--")) + + if len(args) == 0: + usage() + + proc checkCommand(args: seq[string], requires: int) = + if len(args) < requires + 1: + quit("Command " & args[0] & " requires at least " & $(requires) & " arguments.") + + + var db: LevelDb + var key, value: string + if args[0] == "create": + db = leveldb.open(dbPath) + db.close() + elif args[0] == "get": + checkCommand(args, 1) + db = leveldb.open(dbPath) + key = args[1] + let val = db.get(key) + if val.isNone(): + quit() + else: + if hex: + echo val.get().toHex() + else: + echo val.get() + db.close() + elif args[0] == "put": + checkCommand(args, 2) + db = leveldb.open(dbPath) + key = args[1] + value = args[2] + if hex: + value = parseHexStr(value) + db.put(key, value) + db.close() + elif args[0] == "list": + db = leveldb.open(dbPath) + for key, value in db.iter(): + if hex: + echo key, " ", value.toHex() + else: + echo key, " ", value + db.close() + elif args[0] == "keys": + db = leveldb.open(dbPath) + for key, value in db.iter(): + echo key + db.close() + elif args[0] == "delete": + checkCommand(args, 1) + db = leveldb.open(dbPath) + key = args[1] + db.delete(key) + db.close() + elif args[0] == "repair": + repairDb(dbPath) + +when isMainModule: + tool() diff --git a/tests/test.nim b/tests/test.nim index 15756bb..dad2a3a 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -1,5 +1,5 @@ import unittest, options, os, osproc, sequtils, strutils -import leveldb, leveldbpkg/raw +import leveldb, leveldb/raw const tmpDir = "/tmp/testleveldb" @@ -31,7 +31,7 @@ proc execTool(args: varargs[string]): tuple[output: string, exitCode: int] = var quotedArgs = @args quotedArgs.insert(tmpDbDir) quotedArgs.insert("--database") - quotedArgs.insert(tmpNimbleDir / "bin" / "leveldb") + quotedArgs.insert(tmpNimbleDir / "bin" / "leveldbtool") quotedArgs = quotedArgs.map(proc (x: string): string = "\"" & x & "\"") if not existsDir(tmpDbDir): @@ -203,6 +203,9 @@ suite "leveldb": suite "package": + setup: + removeDir(tmpDir) + test "import as package": let (output, exitCode) = execNimble("install") check exitCode == QuitSuccess @@ -220,6 +223,9 @@ suite "package": suite "tool": + setup: + removeDir(tmpDir) + test "leveldb tool": var (output, exitCode) = execNimble("install") check exitCode == QuitSuccess