add program updating script
This commit is contained in:
parent
7007a2c994
commit
8e18e26e47
1 changed files with 56 additions and 0 deletions
56
cc-tweaked/scripts/update.lua
Normal file
56
cc-tweaked/scripts/update.lua
Normal file
|
@ -0,0 +1,56 @@
|
|||
-- 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()
|
Loading…
Add table
Add a link
Reference in a new issue