Advertisement
Guest User

githubinstall

a guest
May 22nd, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.79 KB | None | 0 0
  1. --[[
  2. Downloads a repository from github.
  3. This script has no dependencies, so that it can
  4. be used to install things from scratch.
  5. ]]
  6. local args = { ... }
  7.  
  8. function urlForFile(repo, file)
  9.   return "https://raw.githubusercontent.com/dopscraft/"
  10.     .. repo .. "/master/" .. file
  11. end
  12.  
  13. function run()
  14.   if not args[2] then
  15.     print("githubinstall <repository> <destination>")
  16.     print("Downloads the contents of a github repository to the specified folder")
  17.     return
  18.   end
  19.   local repo = args[1]
  20.   local target = args[2]
  21.   if not fs.exists(target) then
  22.     fs.makeDir(target)
  23.   elseif not fs.isDir(target) then
  24.     print("Target location is not a directory")
  25.     return
  26.   end
  27.   local files = fs.list(target)
  28.   if next(files) then
  29.     print("Target location is not empty")
  30.     return
  31.   end
  32.   -- Get file list.
  33.   local url = urlForFile(repo, "filelist")
  34.   print("downloading filelist")
  35.   local fileList = http.get(url)
  36.   if not fileList or fileList.getResponseCode() ~= 200 then
  37.     print("Unable to download filelist from repository")
  38.     return
  39.   end
  40.   local files = {}
  41.   while true do
  42.     local line = fileList.readLine()
  43.     if not line then
  44.       break
  45.     end
  46.     table.insert(files, line)
  47.   end
  48.   -- Download each file to the specified folder.
  49.   for k,v in pairs(files) do
  50.     print(v)
  51.     url = urlForFile(repo, v)
  52.     local remoteFile = http.get(url)
  53.     if not remoteFile or remoteFile.getResponseCode() ~= 200 then
  54.       print("Error downloading " .. v)
  55.     else
  56.       local localFile = fs.open(fs.combine(target, v), "w")
  57.       if not localFile then
  58.         print("Error writing " .. v)
  59.       else
  60.         localFile.write(remoteFile.readAll())
  61.         localFile.close()
  62.       end
  63.       remoteFile.close()
  64.     end
  65.   end
  66.   print("Downloaded " .. repo)
  67. end
  68. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement