Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local defs = {{
- name = "username",
- message = "Github Username:",
- setting = "GITHUB_USERNAME"
- }, {
- name = "repo",
- message = "Github Repository:",
- setting = "GITHUB_REPO"
- }, {
- name = "branch",
- message = "Github Repository Branch:",
- setting = "GITHUB_BRANCH"
- }, {
- name = "token",
- message = "Github API Token(optional):",
- setting = "GITHUB_TOKEN"
- }}
- local config = {}
- term.clear()
- for i = 1, #defs do
- local def = defs[i]
- config[def.name] = tArgs[i] or settings.get(def.setting)
- if not config[def.name] then
- term.setCursorPos(1, 1)
- term.write(def.message)
- config[def.name] = read()
- settings.set(def.setting, config[def.name])
- term.clear()
- end
- end
- local apiurl = "https://api.github.com/repos/" .. config.username .. "/" .. config.repo .. "/branches/" .. config.branch
- local githubPath = "https://raw.githubusercontent.com/" .. config.username .. "/" .. config.repo .. "/" .. config.branch
- local headers = config.token ~= "" and {
- Authorization = "token " .. config.token
- } or nil
- local getJSON = function(api)
- local post, err = http.get(api, headers)
- if err then
- print(err)
- end
- local response = post.readAll()
- return textutils.unserializeJSON(response)
- end
- local WIDTH, HEIGHT = term.getSize()
- local base = getJSON(apiurl)
- if not base.commit then
- error(base.message)
- end
- settings.save()
- local baseTreeUrl = base.commit.commit.tree.url
- term.setTextColor(colors.white)
- local installText = "--[[ Building File List ]]--"
- term.setCursorPos(WIDTH / 2 - #installText / 2, 1)
- term.write(installText)
- local function buildFiles(url)
- local tree = getJSON(url).tree
- local tmpTable = {}
- for k, v in ipairs(tree) do
- if config.token == "" then
- sleep(1)
- end
- if (v.type == "tree") then
- tmpTable[v.path] = buildFiles(v.url)
- else
- term.setCursorPos(WIDTH / 2 - #v.path / 2, 2)
- term.clearLine()
- term.write(v.path)
- tmpTable[#tmpTable + 1] = v.path
- end
- end
- return tmpTable
- end
- local files = buildFiles(baseTreeUrl)
- local totalFiles = 0
- local downloaded = 0
- local function calculateFiles(_tbl)
- for k, v in pairs(_tbl) do
- if type(v) == "table" then
- calculateFiles(v)
- else
- totalFiles = totalFiles + 1
- end
- end
- end
- local fileColors = {colors.white, colors.lightGray, colors.gray}
- local fileNames = {}
- local function downloadFiles(_tbl, dir)
- local WIDTH, HEIGHT = term.getSize()
- for k, v in pairs(_tbl) do
- if type(v) == "table" then
- downloadFiles(v, dir .. "/" .. k)
- else
- local path = dir .. "/" .. v
- shell.run("wget", githubPath .. path, path)
- table.insert(fileNames, 1, path)
- downloaded = downloaded + 1
- term.clear()
- term.setTextColor(colors.white)
- local installText = "--[[ Installing BeastCraft ]]--"
- local progressText =
- downloaded .. " of " .. totalFiles .. "(" .. math.ceil(100 * (downloaded / totalFiles)) .. "%)"
- term.setCursorPos(WIDTH / 2 - #installText / 2, 2)
- term.write(installText)
- term.setCursorPos(WIDTH / 2 - #progressText / 2, 3)
- term.write(progressText)
- for i = 1, 3 do
- if fileNames[i] then
- term.setCursorPos(WIDTH / 2 - #fileNames[i] / 2, i + 3)
- term.setTextColor(fileColors[i])
- term.write(fileNames[i])
- end
- end
- end
- end
- term.setCursorPos(WIDTH / 2 - 3, 7)
- end
- calculateFiles(files)
- downloadFiles(files, "")
Add Comment
Please, Sign In to add comment