codingbutter

gitloader

Jan 28th, 2022 (edited)
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. local tArgs = {...}
  2. local defs = {{
  3.     name = "username",
  4.     message = "Github Username:",
  5.     setting = "GITHUB_USERNAME"
  6. }, {
  7.     name = "repo",
  8.     message = "Github Repository:",
  9.     setting = "GITHUB_REPO"
  10. }, {
  11.     name = "branch",
  12.     message = "Github Repository Branch:",
  13.     setting = "GITHUB_BRANCH"
  14. }, {
  15.     name = "token",
  16.     message = "Github API Token(optional):",
  17.     setting = "GITHUB_TOKEN"
  18. }}
  19. local config = {}
  20. term.clear()
  21. for i = 1, #defs do
  22.     local def = defs[i]
  23.     config[def.name] = tArgs[i] or settings.get(def.setting)
  24.     if not config[def.name] then
  25.         term.setCursorPos(1, 1)
  26.         term.write(def.message)
  27.         config[def.name] = read()
  28.         settings.set(def.setting, config[def.name])
  29.         term.clear()
  30.     end
  31. end
  32. local apiurl = "https://api.github.com/repos/" .. config.username .. "/" .. config.repo .. "/branches/" .. config.branch
  33. local githubPath = "https://raw.githubusercontent.com/" .. config.username .. "/" .. config.repo .. "/" .. config.branch
  34. local headers = config.token ~= "" and {
  35.     Authorization = "token " .. config.token
  36. } or nil
  37. local getJSON = function(api)
  38.     local post, err = http.get(api, headers)
  39.     if err then
  40.         print(err)
  41.     end
  42.     local response = post.readAll()
  43.     return textutils.unserializeJSON(response)
  44. end
  45. local WIDTH, HEIGHT = term.getSize()
  46. local base = getJSON(apiurl)
  47. if not base.commit then
  48.     error(base.message)
  49. end
  50. settings.save()
  51. local baseTreeUrl = base.commit.commit.tree.url
  52. term.setTextColor(colors.white)
  53. local installText = "--[[ Building File List ]]--"
  54. term.setCursorPos(WIDTH / 2 - #installText / 2, 1)
  55. term.write(installText)
  56. local function buildFiles(url)
  57.  
  58.     local tree = getJSON(url).tree
  59.     local tmpTable = {}
  60.     for k, v in ipairs(tree) do
  61.         if config.token == "" then
  62.             sleep(1)
  63.         end
  64.         if (v.type == "tree") then
  65.             tmpTable[v.path] = buildFiles(v.url)
  66.         else
  67.             term.setCursorPos(WIDTH / 2 - #v.path / 2, 2)
  68.             term.clearLine()
  69.             term.write(v.path)
  70.             tmpTable[#tmpTable + 1] = v.path
  71.         end
  72.     end
  73.     return tmpTable
  74. end
  75. local files = buildFiles(baseTreeUrl)
  76. local totalFiles = 0
  77. local downloaded = 0
  78. local function calculateFiles(_tbl)
  79.     for k, v in pairs(_tbl) do
  80.         if type(v) == "table" then
  81.             calculateFiles(v)
  82.         else
  83.             totalFiles = totalFiles + 1
  84.         end
  85.     end
  86. end
  87.  
  88. local fileColors = {colors.white, colors.lightGray, colors.gray}
  89. local fileNames = {}
  90. local function downloadFiles(_tbl, dir)
  91.     local WIDTH, HEIGHT = term.getSize()
  92.     for k, v in pairs(_tbl) do
  93.         if type(v) == "table" then
  94.             downloadFiles(v, dir .. "/" .. k)
  95.         else
  96.             local path = dir .. "/" .. v
  97.             shell.run("wget", githubPath .. path, path)
  98.             table.insert(fileNames, 1, path)
  99.             downloaded = downloaded + 1
  100.             term.clear()
  101.  
  102.             term.setTextColor(colors.white)
  103.             local installText = "--[[ Installing BeastCraft ]]--"
  104.             local progressText =
  105.                 downloaded .. " of " .. totalFiles .. "(" .. math.ceil(100 * (downloaded / totalFiles)) .. "%)"
  106.             term.setCursorPos(WIDTH / 2 - #installText / 2, 2)
  107.             term.write(installText)
  108.             term.setCursorPos(WIDTH / 2 - #progressText / 2, 3)
  109.             term.write(progressText)
  110.             for i = 1, 3 do
  111.                 if fileNames[i] then
  112.                     term.setCursorPos(WIDTH / 2 - #fileNames[i] / 2, i + 3)
  113.                     term.setTextColor(fileColors[i])
  114.                     term.write(fileNames[i])
  115.                 end
  116.             end
  117.         end
  118.     end
  119.     term.setCursorPos(WIDTH / 2 - 3, 7)
  120. end
  121.  
  122. calculateFiles(files)
  123. downloadFiles(files, "")
  124.  
Add Comment
Please, Sign In to add comment