mc-computer-scripts/cc-tweaked/scripts/update.lua

56 lines
1.5 KiB
Lua

-- usage : update <url> <program>
--
-- downloads
local program = ...
local function new_config(name)
local path = "/etc/" .. name
local function read_key_in_line(line, key)
local prefix = key .. ":"
local prefix_len = string.len(prefix)
local actual = string.sub(line, 0, prefix_len)
if not (prefix == actual) then return nil end
local value = string.sub(line, prefix_len + 1)
return value
end
local function get(key, default)
local file = fs.open(path, "r")
if file == nil then return default end
while true do
local line = file.readLine()
if line == nil then return default end
local value = read_key_in_line(line, key)
if not (value == nil) then return value end
end
end
return { get = get }
end
local function main()
if program == nil then
print("Usage: update <program>")
return
end
local url = new_config("update").get("url", "http://cc.epitls.fr")
local url_ = url .. "/" .. program
local response = http.get(url_)
if (response == nil) or (type(response) == "string") then
print("HTTP Request to", url_, " failed :", response)
return
end
local content = response.readAll()
local path = "./" .. program
if fs.exists(path) then fs.delete(path) end
local file = fs.open(path, "w")
file.write(content)
print("Updated program", program)
end
main()