turtle5204

Kitten OS SDK

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