Posted 23 April 2015 - 07:44 AM
I have an issue that is popping up, or in my case not popping up, in an updater script for my personal ComputerCraft OS called BMOS. (currently a work in progress) All of the source code can be found on a GitHub repository located at https://github.com/Tankobot/BMOS. The script will accept two arguments, an optional version/tag/branch and an optional installation path. Version defaults to master branch, and path defaults to the root directory. Depending on what version you give it, will control what index it downloads from GitHub. This index is used as a guide in order to download all the rest of the required files from GitHub. As it stands now, the script will complete with no interpreter errors, except it will only download and write just the index file. I've tried to compete a small amount of debugging myself, but the I'm kind of lost on how to use the http api. Thank you for any feedback.
update.lua
index.lua
update.lua
--[[
Title: BMOS Updater
Version: v0.1
--]]
local _NAME = "Tankobot/BMOS/"
local _PATH = "https://raw.githubusercontent.com/".._NAME
local download
local arg = {...}
local place
local names = {}
arg[1] = arg[1] or "master"
print("Retrieving file index...")
local indexHttp = http.get(_PATH..arg[1].."/index.lua")
assert(indexHttp, "Failed to retrieve index for "..arg[1]..".")
print("Retrieved index for "..arg[1]..".")
local index = io.open(shell.dir().."index.lua", "w")
index:write(indexHttp:readAll())
index:close()
indexHttp:close()
assert(http, "The updater requires the 'http' api in order to download necessary files.")
local function download(dirTable, currentPath)
dirTable = dirTable or {}
for i, v in pairs(dirTable) do
if type(v) == "table" then
local status, message = pcall(download, v, currentPath..i.."/")
if not status then
error(message, 0)
end
elseif type(v) == "string" then
if v == "end" then
os.queueEvent("end")
return true
else
local place = _PATH..arg[1]..currentPath..v
http.request(place)
names[place] = currentPath..v
end
else
error("Index formatted incorrectly, expected 'table' or 'string'. Got "..type(v), 0)
end
end
end
local function http2drive(path, names)
while true do
local event = {os.pullEvent()}
if event[1] == "http_success" then
local file = io.open(names[event[2]], "w")
file:write(event[3]:readAll())
file:close()
event[3]:close()
elseif event[1] == "http_failure" then
print("Failed to download file at '"..names[event[2]].."'.")
elseif event[1] == "end" then
return
end
end
end
local BMOS = loadfile("index.lua")()
arg[2] = arg[2] or shell.dir()
download(BMOS, "")
http2drive(arg[2], names)
print("BMOS finished downloading.")
print("Check above for errors. If no errors exist, BMOS should have installed correctly.")
index.lua
--File index
local BMOS = {
data = {
startup = {
"main.lua",
},
},
lib = {
os = {
"argSorter.lua",
"codec.lua",
"gui.lua",
"net.lua",
"task.lua",
},
},
tools = {
"CCIedit.lua",
"login.lua",
"startup",
"sudo.lua",
"taskManager",
"trampoline.lua",
},
"README.md",
"end",
}
return BMOS