Grab v2.1-beta

This commit is contained in:
Kirigaya Kazuto 2018-11-24 00:49:01 +08:00
parent eb55fc867c
commit 862da36309

119
grab.lua
View File

@ -1,4 +1,4 @@
-- Grab : OpenComputerScripts Installer -- Grab : Official OpenComputerScripts Installer
-- Created By Kiritow -- Created By Kiritow
local computer=require('computer') local computer=require('computer')
local component=require('component') local component=require('component')
@ -6,27 +6,63 @@ local shell=require('shell')
local filesystem=require('filesystem') local filesystem=require('filesystem')
local serialization=require('serialization') local serialization=require('serialization')
local args,options=shell.parse(...) local args,options=shell.parse(...)
local grab_version="Grab v2.1-beta"
local valid_options={
["cn"]=true, ["help"]=true, ["version"]=true, ["proxy"]=true
}
local valid_command={
["install"]=true,["update"]=true,["list"]=true,["download"]=true
}
local nOptions=0 local nOptions=0
for k,v in pairs(options) do nOptions=nOptions+1 end for k,v in pairs(options) do
if( (#args<1 and nOptions<1) or options["help"]) then if(not valid_options[k]) then
print("Grab - Official OpenComputerScripts Installer") if(string.len(k)>1) then print("Unknown option: --" .. k)
print("Usage:\n\tgrab [<options>] <command> [<projects>]") else print("Unknown option: -" .. k) end
print("Options:" return
.. "\n\t--cn Use mirror site in China. By default grab will download from Github." end
.. "\n\t--help Display this help page." nOptions=nOptions+1
.. "\n\t--version Display version and exit."
.. "\n\t--proxy=<Proxy File> Given a proxy file which will be loaded and returns a proxy function like: "
.. "function(RepoName: string, Branch: string ,FileAddress: string): string"
)
print("Command:"
.. "\n\tinstall, list, update"
)
return
end end
if(options["version"]) then
print("Grab v2.0-beta-rv3") local function show_usage()
print([===[Grab - Official OpenComputerScripts Installer
Usage:
grab [<options>] <command> ...
Options:
--cn Use mirror site in China. By default grab will download from Github.
--help Display this help page."
--version Display version and exit."
--proxy=<Proxy File> Given a proxy file which will be loaded and returns a proxy function like: "
function(RepoName: string, Branch: string ,FileAddress: string): string"
Command:
install <Project> ...: Install projects. Dependency will be downloaded automatically.
update: Update program info.
list: List available projects.
download <Filename> ...: Directly download files. (Just like the old `update`!)
]===])
end
local function check_internet()
if(component.internet==nil) then
print("Error: An internet card is required to run this program.")
return false
else
return true
end
end
if( (#args<1 and nOptions<1) or options["help"]) then
show_usage()
return return
end end
if(options["version"]) then
print(grab_version)
return
end
local function download(url) local function download(url)
if(component.internet==nil) then if(component.internet==nil) then
error("This program requires an Internet card.") error("This program requires an Internet card.")
@ -125,6 +161,8 @@ local function CreateDB(tb,is_raw)
end end
if(args[1]=="update") then if(args[1]=="update") then
if(not check_internet()) then return end
print("Updating programs info....") print("Updating programs info....")
io.write("Downloading... ") io.write("Downloading... ")
local ok,result,code=download(UrlGenerator("Kiritow/OpenComputerScripts","master","programs.info")) local ok,result,code=download(UrlGenerator("Kiritow/OpenComputerScripts","master","programs.info"))
@ -162,6 +200,8 @@ if(args[1]=="install") then
print("Nothing to install.") print("Nothing to install.")
return return
else else
if(not check_internet()) then return end
print("Checking programs info...") print("Checking programs info...")
end end
@ -282,5 +322,46 @@ if(args[1]=="list") then
return return
end end
if(args[1]=="download") then
if(#args<2) then
print("Nothing to download.")
return
else
if(not check_internet()) then return end
print("Collecting files...")
end
local files={}
for i=2,#args,1 do
table.insert(files,args[i])
end
for i=1,#files,1 do
io.write("[" .. i .. "/" .. #files .. "] Downloading " .. files[i] .. "...")
local ok,result,code=download(UrlGenerator("Kiritow/OpenComputerScripts","master",files[i]))
if(not ok) then
print("[Download Failed] " .. result)
return
elseif(code~=200) then
print("[Download Failed] response code " .. code .. " is not 200.")
return
else
local f,ferr=io.open(files[i],"w")
if(not f) then
print("[Write Failed] Unable to write. Error:" .. ferr)
else
f:write(result)
f:close()
print("[OK]")
end
end
end
end
-- reach here? -- reach here?
print("Invalid argument 1: " .. args[1]) if(#args<1) then
show_usage()
else
print("Unknown command: " .. args[1])
end