AlexCatze2005

gitinstaller

Feb 25th, 2021 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. local shell = require("shell")
  2. local comp = require("computer")
  3. local ser = require("serialization")
  4. local inet = require("component").internet
  5. local fs = require("filesystem")
  6. local ref = "master"
  7. local repo = ""
  8. local deploy_path = "/"
  9.  
  10. local function request(url, body, headers, timeout)
  11.   local handle, err = inet.request(url, body, headers)
  12.   if not handle then
  13.     return nil, ("request failed: %s"):format(err or "unknown error")
  14.   end
  15.   local start = comp.uptime()
  16.   while true do
  17.     local status, err = handle.finishConnect()
  18.     if status then
  19.       break
  20.     end
  21.     if status == nil then
  22.       return nil, ("request failed: %s"):format(err or "unknown error")
  23.     end
  24.     if comp.uptime() >= start + (timeout or 3 )then
  25.       handle.close()
  26.       return nil, "request failed: connection timed out"
  27.     end
  28.     os.sleep(0.05)
  29.   end
  30.   return handle
  31. end
  32.  
  33. function tablelength(T)
  34.   local count = 0
  35.   for _ in pairs(T) do count = count + 1 end
  36.   return count
  37. end
  38.  
  39.  
  40. function get_files(path)
  41.   local handle, err = request(path)
  42.   local body = {}
  43.   local read = 0
  44.   while true do
  45.     local chunk, err = handle.read()
  46.     if not chunk then
  47.       local _, _, responseHeaders = handle.response()
  48.       local length
  49.       for k, v in pairs(responseHeaders) do
  50.         if k:lower() == "content-length" then
  51.           length = tonumber(v)
  52.         end
  53.       end
  54.       if not length or read >= length then
  55.         break
  56.       end
  57.     end
  58.     read = read + #chunk
  59.     table.insert(body, chunk)
  60.   end
  61.   body = table.concat(body)
  62.   local types = {}
  63.   local paths = {}
  64.   local urls = {}
  65.   local durls = {}
  66.   for str in string.gmatch(body, "\"type\":\"[^\"]*\",") do
  67.     table.insert(types, str:sub(9,-3))
  68.   end
  69.   for str in string.gmatch(body, "\"path\":\"[^\"]*\",") do
  70.     table.insert(paths, str:sub(9,-3))
  71.   end
  72.   for str in string.gmatch(body, "\"url\":\"[^\"]*\",") do
  73.     table.insert(urls, str:sub(8,-3))
  74.   end
  75.   for str in string.gmatch(body, "\"download_url\":\"[^\"]*\",") do
  76.     table.insert(durls, str:sub(17,-3))
  77.   end
  78.   for i=1,tablelength(types) do
  79.     if types[i] == "file" then
  80.       if not fs.exists(deploy_path..paths[i]:match(".*/")) then fs.makeDirectory(deploy_path..paths[i]:match(".*/")) end
  81.       shell.execute("wget -f "..durls[i].." "..deploy_path..paths[i])
  82.     else
  83.       get_files(urls[i])
  84.     end
  85.   end
  86. end
  87.  
  88. get_files("https://api.github.com/repos/"..repo.."/contents/?ref="..ref)
  89.  
Add Comment
Please, Sign In to add comment