austinv11

github

Jan 12th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.47 KB | None | 0 0
  1. --This is a simple program that gives the ability for computercraft to download files off of GitHub, similar to the
  2. --default pastebin program (ONLY DOES GET)
  3.  
  4. local tArgs = {...}
  5.  
  6. local function errorColor(text)
  7.     local isColor = term.isColor()
  8.     if isColor == true then
  9.         term.setTextColor(colors.red)
  10.         print(text)
  11.         term.setTextColor(colors.white)
  12.     else
  13.         print(text)
  14.         end
  15.     end
  16.  
  17. local function gitGet(user, repo, branch, path, toPath)
  18.     local dl = http.get("https://raw.github.com/"..user.."/"..repo.."/"..branch.."/"..path)
  19.     term.write("Connecting to GitHub.......")
  20.     sleep(.125)
  21.     if dl then
  22.         print("Connected!")
  23.         local file = dl.readAll()
  24.         term.write("Downloading File.......")
  25.         dl.close()
  26.         sleep(.125)
  27.         local check = fs.exists(toPath)
  28.         if check == true then
  29.             errorColor("Error: File Already Exists!")
  30.         else
  31.             local w = fs.open(toPath,"w")
  32.             if w then
  33.                 w.write(file)
  34.                 w.close()
  35.                 print("Downloaded as "..toPath.."!")
  36.                 return true
  37.             else
  38.                 w.close()
  39.                 term.write("Download Failed!")
  40.                 end
  41.             end
  42.     else
  43.         errorColor("Connection Failed!")
  44.         end
  45.     end
  46.  
  47. if tArgs[5] == nil then
  48.     errorColor("Usages: github get <user> <repo> <branch> <path> <topath>")
  49. elseif tArgs[1] == "get" then
  50.     if tArgs[6] ~= nil then
  51.         gitGet(tArgs[2], tArgs[3], tArgs[4], tArgs[5], tArgs[6])
  52.     else
  53.         gitGet(tArgs[2], tArgs[3], tArgs[4], tArgs[5], tArgs[5])
  54.         end
  55. else
  56.     errorColor("Usages: github get <user> <repo> <branch> <path> <topath>")
  57.     end
Advertisement
Add Comment
Please, Sign In to add comment