Create wget
This commit is contained in:
parent
b79b405a3e
commit
b8add8a632
1 changed files with 64 additions and 0 deletions
64
src/bin/wget
Normal file
64
src/bin/wget
Normal file
|
@ -0,0 +1,64 @@
|
|||
--[[
|
||||
wget made by dan200
|
||||
]]
|
||||
|
||||
local function printUsage()
|
||||
print( "Usage:" )
|
||||
print( "wget <url> <filename>" )
|
||||
end
|
||||
|
||||
local tArgs = { ... }
|
||||
if #tArgs < 2 then
|
||||
printUsage()
|
||||
return
|
||||
end
|
||||
|
||||
if not http then
|
||||
printError( "wget requires http API" )
|
||||
printError( "Set http_enable to true in ComputerCraft.cfg" )
|
||||
return
|
||||
end
|
||||
|
||||
local function get( sUrl )
|
||||
write( "Connecting to " .. sUrl .. "... " )
|
||||
|
||||
local ok, err = http.checkURL( sUrl )
|
||||
if not ok then
|
||||
print( "Failed." )
|
||||
if err then
|
||||
printError( err )
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local response = http.get( sUrl )
|
||||
if not response then
|
||||
print( "Failed." )
|
||||
return nil
|
||||
end
|
||||
|
||||
print( "Success." )
|
||||
|
||||
local sResponse = response.readAll()
|
||||
response.close()
|
||||
return sResponse
|
||||
end
|
||||
|
||||
-- Determine file to download
|
||||
local sUrl = tArgs[1]
|
||||
local sFile = tArgs[2]
|
||||
local sPath = shell.resolve( sFile )
|
||||
if fs.exists( sPath ) then
|
||||
print( "File already exists" )
|
||||
return
|
||||
end
|
||||
|
||||
-- Do the get
|
||||
local res = get( sUrl )
|
||||
if res then
|
||||
local file = fs.open( sPath, "w" )
|
||||
file.write( res )
|
||||
file.close()
|
||||
|
||||
print( "Downloaded as "..sFile )
|
||||
end
|
Loading…
Reference in a new issue