From b8add8a6320134badb937d5ca9b06a057f2351b6 Mon Sep 17 00:00:00 2001 From: Ale32bit Date: Mon, 16 May 2016 19:29:25 +0200 Subject: [PATCH] Create wget --- src/bin/wget | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/bin/wget diff --git a/src/bin/wget b/src/bin/wget new file mode 100644 index 0000000..af1908d --- /dev/null +++ b/src/bin/wget @@ -0,0 +1,64 @@ +--[[ + wget made by dan200 +]] + +local function printUsage() + print( "Usage:" ) + print( "wget " ) +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