clarify
This commit is contained in:
parent
4a5bb816e4
commit
1394c595ac
114
cli.lua
114
cli.lua
@ -24,17 +24,25 @@ end
|
|||||||
local db = assert(flatdb("./db"))
|
local db = assert(flatdb("./db"))
|
||||||
local page = "0"
|
local page = "0"
|
||||||
|
|
||||||
local function handle(input)
|
local COMMANDS = {
|
||||||
local c = split(input, " ", 2)
|
SAVE = function(key)
|
||||||
local cmd, key, value = string.upper(c[1]), c[2], c[3]
|
|
||||||
if cmd == "SAVE" then
|
|
||||||
print(db:save(key) and "OK" or "ERROR")
|
print(db:save(key) and "OK" or "ERROR")
|
||||||
elseif cmd == "TOUCH" then
|
end,
|
||||||
|
TOUCH = function(key)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: TOUCH page")
|
||||||
|
return
|
||||||
|
end
|
||||||
if not db[key] then
|
if not db[key] then
|
||||||
db[key] = {}
|
db[key] = {}
|
||||||
end
|
end
|
||||||
print("OK")
|
print("OK")
|
||||||
elseif cmd == "SELECT" then
|
end,
|
||||||
|
SELECT = function(key)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: SELECT page")
|
||||||
|
return
|
||||||
|
end
|
||||||
if tonumber(key) and tonumber(key) < 16 then
|
if tonumber(key) and tonumber(key) < 16 then
|
||||||
if not db[key] then
|
if not db[key] then
|
||||||
db[key] = {}
|
db[key] = {}
|
||||||
@ -44,51 +52,103 @@ local function handle(input)
|
|||||||
page = key
|
page = key
|
||||||
print("OK")
|
print("OK")
|
||||||
else
|
else
|
||||||
print("ERROR")
|
print("ERROR: database not found, try using 'TOUCH' command first")
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
GET = function(key)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: GET key")
|
||||||
|
return
|
||||||
end
|
end
|
||||||
elseif cmd == "GET" then
|
|
||||||
print(pp.format(db[page][key]))
|
print(pp.format(db[page][key]))
|
||||||
elseif cmd == "SET" then
|
end,
|
||||||
|
SET = function(key, value)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: SET key value")
|
||||||
|
return
|
||||||
|
end
|
||||||
local ok, tmp = pcall(load("return "..tostring(value), "=(load)", "t", db))
|
local ok, tmp = pcall(load("return "..tostring(value), "=(load)", "t", db))
|
||||||
db[page][key] = ok and tmp or value
|
db[page][key] = ok and tmp or value
|
||||||
print("OK")
|
print("OK")
|
||||||
elseif cmd == "INCR" then
|
end,
|
||||||
|
INCR = function(key)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: INCR key")
|
||||||
|
return
|
||||||
|
end
|
||||||
if not db[page][key] then
|
if not db[page][key] then
|
||||||
db[page][key] = 0
|
db[page][key] = 0
|
||||||
end
|
end
|
||||||
db[page][key] = db[page][key] + 1
|
db[page][key] = db[page][key] + 1
|
||||||
print("OK")
|
print("OK")
|
||||||
elseif cmd == "DECR" then
|
end,
|
||||||
|
DECR = function(key)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: DECR key")
|
||||||
|
return
|
||||||
|
end
|
||||||
if not db[page][key] then
|
if not db[page][key] then
|
||||||
db[page][key] = 0
|
db[page][key] = 0
|
||||||
end
|
end
|
||||||
db[page][key] = db[page][key] - 1
|
db[page][key] = db[page][key] - 1
|
||||||
print("OK")
|
print("OK")
|
||||||
elseif cmd == "RPUSH" then
|
end,
|
||||||
|
RPUSH = function(key, value)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: RPUSH list value")
|
||||||
|
return
|
||||||
|
end
|
||||||
if not db[page][key] then
|
if not db[page][key] then
|
||||||
db[page][key] = {}
|
db[page][key] = {}
|
||||||
end
|
end
|
||||||
table.insert(db[page][key], value)
|
table.insert(db[page][key], value)
|
||||||
print(#db[page][key])
|
print(#db[page][key])
|
||||||
elseif cmd == "LPUSH" then
|
end,
|
||||||
|
LPUSH = function(key, value)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: LPUSH list value")
|
||||||
|
return
|
||||||
|
end
|
||||||
if not db[page][key] then
|
if not db[page][key] then
|
||||||
db[page][key] = {}
|
db[page][key] = {}
|
||||||
end
|
end
|
||||||
table.insert(db[page][key], 1, value)
|
table.insert(db[page][key], 1, value)
|
||||||
print(#db[page][key])
|
print(#db[page][key])
|
||||||
elseif cmd == "RPOP" then
|
end,
|
||||||
if not db[page][key] then
|
RPOP = function(key)
|
||||||
db[page][key] = {}
|
if not key then
|
||||||
|
print("USAGE: RPOP list")
|
||||||
|
return
|
||||||
end
|
end
|
||||||
print(table.remove(db[page][key], value))
|
if db[page][key] then
|
||||||
elseif cmd == "LPOP" then
|
print(table.remove(db[page][key]))
|
||||||
if not db[page][key] then
|
else
|
||||||
db[page][key] = {}
|
print("nil")
|
||||||
end
|
end
|
||||||
|
end,
|
||||||
|
LPOP = function(key)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: LPOP list")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if db[page][key] then
|
||||||
print(table.remove(db[page][key], 1))
|
print(table.remove(db[page][key], 1))
|
||||||
elseif cmd == "LLEN" then
|
else
|
||||||
|
print("nil")
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
LLEN = function(key)
|
||||||
|
if not key then
|
||||||
|
print("USAGE: LLEN list")
|
||||||
|
return
|
||||||
|
end
|
||||||
print(db[page][key] and #db[page][key] or 0)
|
print(db[page][key] and #db[page][key] or 0)
|
||||||
elseif cmd == "LINDEX" then
|
end,
|
||||||
|
LINDEX = function(key, value)
|
||||||
|
if not key and not value then
|
||||||
|
print("USAGE: LINDEX list index")
|
||||||
|
return
|
||||||
|
end
|
||||||
if db[page][key] then
|
if db[page][key] then
|
||||||
local i = tonumber(value)
|
local i = tonumber(value)
|
||||||
if i < 0 then i = i + #db[page][key] end
|
if i < 0 then i = i + #db[page][key] end
|
||||||
@ -98,6 +158,16 @@ local function handle(input)
|
|||||||
print("nil")
|
print("nil")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
local function handle(input)
|
||||||
|
local c = split(input, " ", 2)
|
||||||
|
local cmd, key, value = string.upper(c[1]), c[2], c[3]
|
||||||
|
if COMMANDS[cmd] then
|
||||||
|
COMMANDS[cmd](key, value)
|
||||||
|
else
|
||||||
|
print("ERROR: command not found")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function main()
|
local function main()
|
||||||
|
Loading…
Reference in New Issue
Block a user