mirror of https://github.com/LDDestroier/CC/
Update gitget.lua
This commit is contained in:
parent
1f4d5a2cc6
commit
2d63440f5c
134
gitget.lua
134
gitget.lua
|
@ -6,10 +6,63 @@
|
||||||
--]]
|
--]]
|
||||||
local verbose = true
|
local verbose = true
|
||||||
|
|
||||||
local tArg = {...}
|
local function interpretArgs(tInput, tArgs)
|
||||||
local reponame = tArg[1]
|
local output = {}
|
||||||
local repopath = tArg[2]
|
local errors = {}
|
||||||
local outpath = tArg[3] or ""
|
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
|
if outpath:sub(1,1) ~= "/" then
|
||||||
outpath = fs.combine(shell.dir(), outpath)
|
outpath = fs.combine(shell.dir(), outpath)
|
||||||
|
@ -179,15 +232,23 @@ local function decode(str)
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local writeToFile = function(filename,contents)
|
local writeToFile = function(filename, contents)
|
||||||
local file = fs.open(filename,"w")
|
local file = fs.open(filename,"w")
|
||||||
file.write(contents)
|
file.write(contents)
|
||||||
file.close()
|
file.close()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local sPrint = function(str)
|
||||||
|
if not silent then
|
||||||
|
return print(str)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local getFromGitHub
|
local getFromGitHub
|
||||||
getFromGitHub = function(reponame,repopath,filepath,verbose)
|
getFromGitHub = function(reponame, repopath, filepath, verbose)
|
||||||
local jason = http.get("https://api.github.com/repos/"..reponame.."/contents/"..(repopath or ""))
|
local jason = http.get("https://api.github.com/repos/" .. reponame .. "/contents/" .. (repopath or ""), {
|
||||||
|
["Authorization"] = token
|
||||||
|
})
|
||||||
if not jason then return false end
|
if not jason then return false end
|
||||||
local repo = decode(jason.readAll())
|
local repo = decode(jason.readAll())
|
||||||
for k,v in pairs(repo) do
|
for k,v in pairs(repo) do
|
||||||
|
@ -195,12 +256,16 @@ getFromGitHub = function(reponame,repopath,filepath,verbose)
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
if v.type == "file" then
|
if v.type == "file" then
|
||||||
if verbose then print("'"..fs.combine(filepath,v.name).."'") end
|
if verbose then
|
||||||
writeToFile(fs.combine(filepath,v.name),http.get(v.download_url).readAll())
|
sPrint("'" .. fs.combine(filepath, v.name) .. "'")
|
||||||
|
end
|
||||||
|
writeToFile(fs.combine(filepath, v.name), http.get(v.download_url).readAll())
|
||||||
elseif v.type == "dir" then
|
elseif v.type == "dir" then
|
||||||
if verbose then print("'"..fs.combine(filepath,v.name).."'") end
|
if verbose then
|
||||||
fs.makeDir(fs.combine(filepath,v.name))
|
sPrint("'" .. fs.combine(filepath,v.name) .. "'")
|
||||||
getFromGitHub(reponame,fs.combine(repopath,v.name),fs.combine(filepath,v.name),verbose)
|
end
|
||||||
|
fs.makeDir(fs.combine(filepath, v.name))
|
||||||
|
getFromGitHub(reponame, fs.combine(repopath, v.name), fs.combine(filepath, v.name), verbose)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -208,34 +273,43 @@ end
|
||||||
|
|
||||||
local displayHelp = function()
|
local displayHelp = function()
|
||||||
local progname = fs.getName(shell.getRunningProgram())
|
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
|
end
|
||||||
|
|
||||||
if not (reponame and repopath and outpath) then
|
if not (reponame and repopath and outpath) then
|
||||||
return displayHelp()
|
return displayHelp()
|
||||||
else
|
else
|
||||||
if fs.exists(outpath) and not fs.isDir(outpath) then
|
if fs.exists(outpath) and not fs.isDir(outpath) then
|
||||||
write("'"..outpath.."' already exists!")
|
if autoOverwrite then
|
||||||
write("Overwrite?")
|
fs.delete(outpath)
|
||||||
print(" (Y/N)")
|
sPrint("Overwritten '" .. outpath .. "'.")
|
||||||
local evt,key
|
else
|
||||||
while true do
|
print("'" .. outpath .. "' already exists!")
|
||||||
evt,key = os.pullEvent("key")
|
print("Overwrite? (Y/N)")
|
||||||
if key == keys.y then
|
local evt,key
|
||||||
if (not fs.isDir(outpath)) then fs.delete(outpath) end
|
while true do
|
||||||
break
|
evt, key = os.pullEvent("key")
|
||||||
elseif key == keys.n then
|
if key == keys.y then
|
||||||
print("Abort.")
|
if (not fs.isDir(outpath)) then
|
||||||
coroutine.yield()
|
fs.delete(outpath)
|
||||||
return
|
end
|
||||||
|
break
|
||||||
|
elseif key == keys.n then
|
||||||
|
print("Abort.")
|
||||||
|
coroutine.yield()
|
||||||
|
return
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if repopath == "*" then repopath = "" end
|
repopath = (repopath == "*") and "" or repopath
|
||||||
local oldtxt = (term.getTextColor and term.getTextColor()) or colors.white
|
local oldtxt = (term.getTextColor and term.getTextColor()) or colors.white
|
||||||
print("Downloading...")
|
sPrint("Downloading...")
|
||||||
term.setTextColor(term.isColor() and colors.green or colors.lightGray)
|
term.setTextColor(term.isColor() and colors.green or colors.lightGray)
|
||||||
getFromGitHub(reponame,repopath,outpath,verbose)
|
getFromGitHub(reponame, repopath, outpath, verbose)
|
||||||
term.setTextColor(oldtxt)
|
term.setTextColor(oldtxt)
|
||||||
print("Downloaded to /"..fs.combine("",outpath))
|
sPrint("Downloaded to /" .. fs.combine("", outpath))
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue