JackMacWindows

gist_client.lua

Jul 7th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1. -- gist.lua - Gist client for ComputerCraft
  2. -- Made by JackMacWindows for CraftOS-PC
  3.  
  4. if not http then
  5.     printError("Gist requires http API")
  6.     if _G.config ~= nil then printError("Set http_enable to true in the CraftOS-PC configuration")
  7.     else printError("Set http_enable to true in ComputerCraft's configuration") end
  8.     return 2
  9. end
  10.  
  11. local gist = require "gist"
  12.  
  13. local args = { ... }
  14.  
  15. local function readFile(filename, files, isEditing)
  16.     if fs.isDir(shell.resolve(filename)) then
  17.         for _, v in ipairs(fs.list(shell.resolve(filename))) do if readFile(fs.combine(filename, v), files, isEditing) then return true end end
  18.     else
  19.         if files[fs.getName(filename)] then print("Cannot upload files with duplicate names.") return true end
  20.         local file = fs.open(shell.resolve(filename), "rb")
  21.         if file == nil then
  22.             if not isEditing then print("Could not read " .. filename .. ".") return true
  23.             else files[fs.getName(filename)] = textutils.json_null end
  24.         else
  25.             files[fs.getName(filename)] = file.readAll()
  26.             file.close()
  27.         end
  28.     end
  29. end
  30.  
  31. local function getFiles(isEditing)
  32.     local files = {}
  33.     local i = isEditing and 3 or 2
  34.     while args[i] ~= nil and args[i] ~= "--" do
  35.         if readFile(args[i], files, isEditing) then return nil end
  36.         i = i + 1
  37.     end
  38.     if args[i] == "--" then return files, table.concat({ table.unpack(args, i + 1) }, " ") end
  39.     return files
  40. end
  41.  
  42. local function setTextColor(c) if term.isColor() then term.setTextColor(c) elseif c == colors.white or c == colors.yellow then term.setTextColor(colors.white) else term.setTextColor(colors.lightGray) end end
  43.  
  44. local helpstr = "Usages:\ngist put <files...> [-- description...]\ngist edit <id> <files...> [-- description]\ngist delete <id>\ngist get <id> <filename>\ngist run <id> [arguments...]\ngist info <id>"
  45.  
  46. if #args < 2 then
  47.     print(helpstr)
  48.     return 1
  49. end
  50.  
  51. if args[1] == "get" then
  52.     if #args < 3 then print(helpstr) return 1 end
  53.     if args[3]:sub(#args[3]) == "/" or fs.isDir(shell.resolve(args[3])) then
  54.         fs.makeDir(shell.resolve(args[3]))
  55.         local files, err = gist.getAll(args[2], write)
  56.         if files == nil then printError(err) return 3 end
  57.         for k, v in pairs(files) do
  58.             local file = fs.open(shell.resolve(fs.combine(args[3], k)), "wb")
  59.             file.write(v)
  60.             file.close()
  61.         end
  62.         print("Downloaded all files to " .. shell.resolve(args[3]))
  63.     else
  64.         local data, err = gist.get(args[2], write)
  65.         if data == nil then printError(err) return 3 end
  66.         local file = fs.open(shell.resolve(args[3]), "wb")
  67.         file.write(data)
  68.         file.close()
  69.         print("Downloaded as " .. shell.resolve(args[3]))
  70.     end
  71. elseif args[1] == "run" then
  72.     return gist.run(args[2], write, table.unpack(args, 3))
  73. elseif args[1] == "put" then
  74.     local files, description = getFiles(false)
  75.     if files == nil then return end
  76.     local id, html_url = gist.put(files, description, nil, true)
  77.     if id ~= nil then print("Uploaded as " .. html_url .. "\nRun 'gist get " .. id .. "' to download anywhere")
  78.     else printError(html_url) return 3 end
  79. elseif args[1] == "info" then
  80.     local tab, err = gist.info(args[2], write)
  81.     if tab == nil then printError(err) return 3 end
  82.     setTextColor(colors.yellow)
  83.     write("Description: ")
  84.     setTextColor(colors.white)
  85.     print(tab.description)
  86.     setTextColor(colors.yellow)
  87.     write("Author: ")
  88.     setTextColor(colors.white)
  89.     print(tab.author)
  90.     setTextColor(colors.yellow)
  91.     write("Revisions: ")
  92.     setTextColor(colors.white)
  93.     print(tab.revisionCount)
  94.     setTextColor(colors.yellow)
  95.     print("Files in this Gist:")
  96.     setTextColor(colors.white)
  97.     textutils.tabulate(tab.files)
  98. elseif args[1] == "edit" then
  99.     if #args < 3 then print(helpstr) return 1 end
  100.     local files, description = getFiles(true)
  101.     if files == nil then return 2 end
  102.     if not description then description = gist.info(args[2], write).description end
  103.     local id, html_url = gist.put(files, description, args[2], true)
  104.     if id then print("Uploaded as " .. html_url .. "\nRun 'gist get " .. args[2] .. "' to download anywhere")
  105.     else printError(html_url) return 3 end
  106. elseif args[1] == "delete" then
  107.     local ok, err = gist.delete(args[2], true)
  108.     if ok then print("The requested Gist has been deleted.") else printError(err) return 3 end
  109. else print(helpstr) return 1 end
Add Comment
Please, Sign In to add comment