Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local function usage()
- print("Usage:")
- print(" - lookup <name>")
- print(" - lookup list")
- print(" - lookup create/add <name> <location>")
- print(" - lookup delete <name>")
- end
- if #args < 1 or #args > 3 then -- Just to speed things up a tad bit
- return usage()
- end
- local lookupDir, database = "/l/database.lua", {}
- if fs.exists(lookupDir) then
- local f = fs.open(lookupDir, "r")
- if not f then return printError("Failed to load database.") end
- database = textutils.unserialize(f.readAll()) f.close()
- end
- local function lookup(address)
- for i=1, #database do
- if database[i].address == address then return i end
- end
- return false
- end
- if args[1] == "create" or args[1] == "add" then
- if #args ~= 3 then return usage() end
- local tmp = lookup(args[2]) -- To avoid running this twice
- if tmp then return printError("Address already exists") end
- table.insert(database, {address=args[2],location=args[3]}) print("Added '"..args[2].."' to database.")
- elseif args[1] == "delete" or args[1] == "del" then
- if #args ~= 2 then return usage() end
- local tmp = lookup(args[2]) -- To avoid running this twice
- if not tmp then return printError("Address does not exist") end
- local tmp2 = database[tmp].address -- For display purposes
- table.remove(database, tmp) print("Deleted '"..tmp2.."' from database.")
- elseif args[1] == "list" or args[2] == "ls" then
- print("Database consist of "..#database.." Addresses:")
- for i=1, #database do
- sleep(.1)
- print("Name: "..database[i].address.." | Location: "..database[i].location)
- end
- return database
- else
- if #args ~= 1 then return usage() end
- local tmp = lookup(args[1]) -- To avoid running this twice
- if tmp then
- print("Address Exists!")
- print("Name: "..database[tmp].address)
- print("Location: "..database[tmp].location)
- return true
- else
- print("Address '"..args[1].."' does not exists.")
- return false
- end
- end
- local f = fs.open(lookupDir, "w")
- if not f then return printError("Failed to save database to '"..lookupDir.."'") end
- f.write(textutils.serialize(database)) f.close()
- print("Database saved.")
- -- End of Program
Advertisement
Add Comment
Please, Sign In to add comment