Advertisement
apemanzilla

gitget

Mar 6th, 2014
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. --[[ /gitget
  2. GitHub downloading utility for CC.
  3. Developed by apemanzilla.
  4.  
  5. If you want to use this as an automated installer, please see lines 13 and 23.
  6.  
  7. This requires ElvishJerricco's JSON parsing API.
  8. Direct link: http://pastebin.com/raw.php?i=4nRg9CHU
  9. ]]--
  10.  
  11. local args = {...}
  12.  
  13. --[[
  14. --Remove the line above this and change the lines below to use automated mode.
  15. local automated = true                                                  -- Don't touch!
  16. local hide_progress = true                                              -- If true, will not list out files as they are downloaded
  17. args[1] = "apemanzilla"                                                 -- Github username
  18. args[2] = "ApeOs"                                                               -- Github repo name
  19. args[3] = nil                                                                   -- Branch - defaults to "master"
  20. args[4] = nil                                                                   -- Path - defaults to root ("/")
  21. local pre_dl = "print('Starting download...')"  -- Command(s) to run before download starts.
  22. local post_dl = "print('Download complete!')"   -- Command(s) to run after download completes.
  23. --Remove the line below this and change the lines below to use automated mode.
  24. ]]--
  25.  
  26. args[3] = args[3] or "master"
  27. args[4] = args[4] or ""
  28.  
  29. if not automated and #args < 2 then
  30.         print("Usage:\n"..shell.getRunningProgram().." <user> <repo> [branch=master] [path=/]") error()
  31. end
  32.  
  33. local function save(data,file)
  34.         local file = shell.resolve(file)
  35.         if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) then
  36.                 if fs.exists(string.sub(file,1,#file - #fs.getName(file))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
  37.                 fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
  38.         end
  39.         local f = fs.open(file,"w")
  40.         f.write(data)
  41.         f.close()
  42. end
  43.  
  44. local function download(url, file)
  45.         save(http.get(url).readAll(),file)
  46. end
  47.  
  48. if not json then
  49.         print("Downloading JSON api...\n(Credits to ElvishJerricco!)")
  50.         download("http://pastebin.com/raw.php?i=4nRg9CHU","json")
  51.         os.loadAPI("json")
  52. end
  53.  
  54. if pre_dl then loadstring(pre_dl)() else print("Downloading files from github....") end
  55. local data = json.decode(http.get("https://api.github.com/repos/"..args[1].."/"..args[2].."/git/trees/"..args[3].."?recursive=1").readAll())
  56. if data.message and data.message == "Not found" then error("Invalid repository",2) else
  57.     for k,v in pairs(data.tree) do
  58.         -- Make directories
  59.         if v.type == "tree" then
  60.             fs.makeDir(fs.combine(args[4],v.path))
  61.             if not hide_progress then
  62.                 print(v.path)
  63.             end
  64.         end
  65.     end
  66.     for k,v in pairs(data.tree) do
  67.         -- Download files
  68.         if v.type == "blob" then
  69.             download("https://raw.github.com/"..args[1].."/"..args[2].."/"..args[3].."/"..v.path,fs.combine(args[4],v.path))
  70.             if not hide_progress then
  71.                 print(v.path)
  72.             end
  73.         end
  74.     end
  75. end
  76. if post_dl then loadstring(post_dl)() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement