140 lines
3.0 KiB
Lua
140 lines
3.0 KiB
Lua
--[[
|
|
|
|
Usages:
|
|
> gitgud sync <repo path> <local path>
|
|
download file from repo
|
|
|
|
> gitgud delete <local path>
|
|
delete downloaded file
|
|
|
|
> gitgud
|
|
re-download all synced files
|
|
|
|
> gitgud run <repo path>
|
|
fetch and exec file from repo
|
|
|
|
]]
|
|
|
|
if not http then error("http is not enabled") end
|
|
|
|
local function get(url)
|
|
local response, err = http.get(url)
|
|
if not response then
|
|
return nil, err
|
|
end
|
|
|
|
local status, err = response.getResponseCode()
|
|
if status ~= 200 then
|
|
return nil, (status .. " " .. err)
|
|
end
|
|
|
|
local result = response.readAll()
|
|
response.close()
|
|
return result
|
|
end
|
|
|
|
function ensure_dotlua(path)
|
|
if string.match(path, "%.lua$") then
|
|
return path
|
|
else
|
|
return path .. ".lua"
|
|
end
|
|
end
|
|
|
|
local function gitea_url(path)
|
|
return "https://gitea.leafbla.de/dominic/turtles/raw/branch/master/".. path
|
|
end
|
|
|
|
local function download_file(repo_path, local_path)
|
|
write("Downloading " .. repo_path .. " as " .. local_path .. "... ")
|
|
local result, err = get(gitea_url(repo_path))
|
|
if not result then
|
|
print()
|
|
printError(err)
|
|
return
|
|
end
|
|
|
|
local f = fs.open(local_path, "w")
|
|
f.write(result)
|
|
f.close()
|
|
|
|
print("OK")
|
|
end
|
|
|
|
-- keep track of downloaded files
|
|
local storage_path = ".gitgud"
|
|
|
|
local storage = {}
|
|
if fs.exists(storage_path) then
|
|
local f = fs.open(storage_path, "r")
|
|
storage = textutils.unserialize(f.readAll())
|
|
f.close()
|
|
end
|
|
|
|
local function update_storage()
|
|
local f = fs.open(storage_path, "w")
|
|
f.write(textutils.serialize(storage))
|
|
f.close()
|
|
end
|
|
|
|
local args = {...}
|
|
|
|
if #args == 0 then
|
|
print("Syncing " .. #storage .. " files.")
|
|
|
|
for local_path, repo_path in pairs(storage) do
|
|
download_file(repo_path, local_path)
|
|
end
|
|
|
|
print("Done.")
|
|
|
|
elseif args[1] == "sync" then
|
|
local usage = "gitgud sync <repo path> <local path>"
|
|
local repo_path = args[2] or error(usage)
|
|
local local_path = args[3] or error(usage)
|
|
|
|
repo_path = ensure_dotlua(repo_path)
|
|
local_path = ensure_dotlua(local_path)
|
|
|
|
storage[local_path] = repo_path
|
|
update_storage()
|
|
|
|
download_file(repo_path, local_path)
|
|
|
|
elseif args[1] == "delete" then
|
|
local usage = "gitgud delete <local path>"
|
|
local local_path = args[2] or error(usage)
|
|
|
|
local_path = ensure_dotlua(local_path)
|
|
|
|
storage[local_path] = nil
|
|
update_storage()
|
|
|
|
fs.delete(local_path)
|
|
|
|
elseif args[1] == "run" then
|
|
local usage = "gitgud run <repo path> [args...]"
|
|
local repo_path = args[2] or error(usage)
|
|
|
|
repo_path = ensure_dotlua(repo_path)
|
|
|
|
write("Downloading " .. repo_path .. "... ")
|
|
local result, err = get(gitea_url(repo_path))
|
|
if not result then
|
|
print()
|
|
printError(err)
|
|
else
|
|
print("OK")
|
|
end
|
|
|
|
local func, err = load(result, repo_path, "t", _ENV)
|
|
if not func then
|
|
printError(err)
|
|
return
|
|
end
|
|
local success, err = pcall(func, select(3, ...))
|
|
if not success then
|
|
printError(err)
|
|
end
|
|
|
|
end |