From 2d63440f5cbe178c2699161f9f94978b086b9d92 Mon Sep 17 00:00:00 2001 From: LDDestroier Date: Mon, 1 Jun 2020 15:42:22 -0400 Subject: [PATCH] Update gitget.lua --- gitget.lua | 134 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 104 insertions(+), 30 deletions(-) diff --git a/gitget.lua b/gitget.lua index 67ae811..3854975 100644 --- a/gitget.lua +++ b/gitget.lua @@ -6,10 +6,63 @@ --]] local verbose = true -local tArg = {...} -local reponame = tArg[1] -local repopath = tArg[2] -local outpath = tArg[3] or "" +local function interpretArgs(tInput, tArgs) + local output = {} + local errors = {} + local usedEntries = {} + for aName, aType in pairs(tArgs) do + output[aName] = false + for i = 1, #tInput do + if not usedEntries[i] then + if tInput[i] == aName and not output[aName] then + if aType then + usedEntries[i] = true + if type(tInput[i+1]) == aType or type(tonumber(tInput[i+1])) == aType then + usedEntries[i+1] = true + if aType == "number" then + output[aName] = tonumber(tInput[i+1]) + else + output[aName] = tInput[i+1] + end + else + output[aName] = nil + errors[1] = errors[1] and (errors[1] + 1) or 1 + errors[aName] = "expected " .. aType .. ", got " .. type(tInput[i+1]) + end + else + usedEntries[i] = true + output[aName] = true + end + end + end + end + end + for i = 1, #tInput do + if not usedEntries[i] then + output[#output+1] = tInput[i] + end + end + return output, errors +end + +local argData = { + ["-b"] = "string", -- string, branch + ["-s"] = false, -- boolean, silent + ["-y"] = false, -- boolean, automatically accept to overwrite +} + +-- this token has only enough permissions to download files, so don't get any ideas +local token = "0f7e97e6524dcb03f79978ff88235a510f5ff4ae" + +local argList = interpretArgs({...}, argData) + +local reponame = argList[1] +local repopath = argList[2] +local outpath = argList[3] or "" + +local branch = argList["-b"] or "master" +local silent = argList["-s"] or false +local autoOverwrite = argList["-y"] if outpath:sub(1,1) ~= "/" then outpath = fs.combine(shell.dir(), outpath) @@ -179,15 +232,23 @@ local function decode(str) return t end -local writeToFile = function(filename,contents) +local writeToFile = function(filename, contents) local file = fs.open(filename,"w") file.write(contents) file.close() end +local sPrint = function(str) + if not silent then + return print(str) + end +end + local getFromGitHub -getFromGitHub = function(reponame,repopath,filepath,verbose) - local jason = http.get("https://api.github.com/repos/"..reponame.."/contents/"..(repopath or "")) +getFromGitHub = function(reponame, repopath, filepath, verbose) + local jason = http.get("https://api.github.com/repos/" .. reponame .. "/contents/" .. (repopath or ""), { + ["Authorization"] = token + }) if not jason then return false end local repo = decode(jason.readAll()) for k,v in pairs(repo) do @@ -195,12 +256,16 @@ getFromGitHub = function(reponame,repopath,filepath,verbose) return false else if v.type == "file" then - if verbose then print("'"..fs.combine(filepath,v.name).."'") end - writeToFile(fs.combine(filepath,v.name),http.get(v.download_url).readAll()) + if verbose then + sPrint("'" .. fs.combine(filepath, v.name) .. "'") + end + writeToFile(fs.combine(filepath, v.name), http.get(v.download_url).readAll()) elseif v.type == "dir" then - if verbose then print("'"..fs.combine(filepath,v.name).."'") end - fs.makeDir(fs.combine(filepath,v.name)) - getFromGitHub(reponame,fs.combine(repopath,v.name),fs.combine(filepath,v.name),verbose) + if verbose then + sPrint("'" .. fs.combine(filepath,v.name) .. "'") + end + fs.makeDir(fs.combine(filepath, v.name)) + getFromGitHub(reponame, fs.combine(repopath, v.name), fs.combine(filepath, v.name), verbose) end end end @@ -208,34 +273,43 @@ end local displayHelp = function() local progname = fs.getName(shell.getRunningProgram()) - print(progname.." [owner/repo] [repopath] [output dir]") + sPrint(progname .. " [owner/repo] [repopath] [output dir]") + sPrint(" -b [branch]") + sPrint(" -s | runs silent unless asking to overwrite") + sPrint(" -y | overwrites without asking") end if not (reponame and repopath and outpath) then return displayHelp() else if fs.exists(outpath) and not fs.isDir(outpath) then - write("'"..outpath.."' already exists!") - write("Overwrite?") - print(" (Y/N)") - local evt,key - while true do - evt,key = os.pullEvent("key") - if key == keys.y then - if (not fs.isDir(outpath)) then fs.delete(outpath) end - break - elseif key == keys.n then - print("Abort.") - coroutine.yield() - return + if autoOverwrite then + fs.delete(outpath) + sPrint("Overwritten '" .. outpath .. "'.") + else + print("'" .. outpath .. "' already exists!") + print("Overwrite? (Y/N)") + local evt,key + while true do + evt, key = os.pullEvent("key") + if key == keys.y then + if (not fs.isDir(outpath)) then + fs.delete(outpath) + end + break + elseif key == keys.n then + print("Abort.") + coroutine.yield() + return + end end end end - if repopath == "*" then repopath = "" end + repopath = (repopath == "*") and "" or repopath local oldtxt = (term.getTextColor and term.getTextColor()) or colors.white - print("Downloading...") + sPrint("Downloading...") term.setTextColor(term.isColor() and colors.green or colors.lightGray) - getFromGitHub(reponame,repopath,outpath,verbose) + getFromGitHub(reponame, repopath, outpath, verbose) term.setTextColor(oldtxt) - print("Downloaded to /"..fs.combine("",outpath)) + sPrint("Downloaded to /" .. fs.combine("", outpath)) end