1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-13 19:50:31 +00:00

Add wget run (#218)

Equivalent to `pastebin run`, but allows running arbitrary URLs
instead.

Is this a little questionable? Yes - people shouldn't be downloading
and running code from the internet. But hey, people do that already,
so we might as well make it convenient.
This commit is contained in:
JakobDev 2019-06-08 15:49:42 +02:00 committed by SquidDev
parent a0e7c4a74c
commit 309cbdb8be
5 changed files with 104 additions and 19 deletions

View File

@ -5,3 +5,4 @@ ex:
"wget http://pastebin.com/raw/CxaWmPrX test" will download the file from the URL http://pastebin.com/raw/CxaWmPrX, and save it as "test". "wget http://pastebin.com/raw/CxaWmPrX test" will download the file from the URL http://pastebin.com/raw/CxaWmPrX, and save it as "test".
"wget http://example.org/test.lua/?foo=bar#qzu" will download the file from the URL http://example.org/test.lua/?foo=bar#qzu and save it as "test.lua" "wget http://example.org/test.lua/?foo=bar#qzu" will download the file from the URL http://example.org/test.lua/?foo=bar#qzu and save it as "test.lua"
"wget http://example.org/" will download the file from the URL http://example.org and save it as "example.org" "wget http://example.org/" will download the file from the URL http://example.org and save it as "example.org"
"wget run http://pastebin.com/raw/CxaWmPrX" will download the file from the URL http://pastebin.com/raw/CxaWmPrX and run it immediately.

View File

@ -8,6 +8,8 @@ Run "list" or "ls" to see all files in a directory.
You can delete files and directories with "delete" or "rm". You can delete files and directories with "delete" or "rm".
Use "pastebin put" to upload a program to pastebin. Use "pastebin put" to upload a program to pastebin.
Use "pastebin get" to download a program from pastebin. Use "pastebin get" to download a program from pastebin.
Use "pastebin run" to run a program from pastebin without saving it. Use "pastebin run" to run a program from pastebin.
Use the "edit" program to create and edit your programs. Use the "edit" program to create and edit your programs.
You can copy files with "copy" or "cp". You can copy files with "copy" or "cp".
You can use "wget run <url>" to run a program from the internet.
You can use "wget" to download a file from the internet.

View File

@ -2,14 +2,24 @@
local function printUsage() local function printUsage()
print( "Usage:" ) print( "Usage:" )
print( "wget <url> [filename]" ) print( "wget <url> [filename]" )
print( "wget run <url>" )
end end
local tArgs = { ... } local tArgs = { ... }
local run = false
if tArgs[1] == "run" then
table.remove( tArgs, 1 )
run = true
end
if #tArgs < 1 then if #tArgs < 1 then
printUsage() printUsage()
return return
end end
local url = table.remove( tArgs, 1 )
if not http then if not http then
printError( "wget requires http API" ) printError( "wget requires http API" )
printError( "Set http_enable to true in ComputerCraft.cfg" ) printError( "Set http_enable to true in ComputerCraft.cfg" )
@ -22,6 +32,13 @@ local function getFilename( sUrl )
end end
local function get( sUrl ) local function get( sUrl )
-- Check if the URL is valid
local ok, err = http.checkURL( url )
if not ok then
printError( err or "Invalid URL." )
return
end
write( "Connecting to " .. sUrl .. "... " ) write( "Connecting to " .. sUrl .. "... " )
local response = http.get( sUrl , nil , true ) local response = http.get( sUrl , nil , true )
@ -37,29 +54,34 @@ local function get( sUrl )
return sResponse return sResponse
end end
-- Determine file to download if run then
local sUrl = tArgs[1] local res = get(url)
if not res then return end
--Check if the URL is valid local func, err = load(res, getFilename(url), "t", _ENV)
local ok, err = http.checkURL( sUrl ) if not func then
if not ok then printError(err)
printError( err or "Invalid URL." )
return return
end end
local sFile = tArgs[2] or getFilename( sUrl ) local ok, err = pcall(func, table.unpack(tArgs))
local sPath = shell.resolve( sFile ) if not ok then
if fs.exists( sPath ) then printError( err )
end
else
local sFile = tArgs[1] or getFilename( url )
local sPath = shell.resolve( sFile )
if fs.exists( sPath ) then
print( "File already exists" ) print( "File already exists" )
return return
end end
local res = get(url)
if not res then return end
-- Do the get
local res = get( sUrl )
if res then
local file = fs.open( sPath, "wb" ) local file = fs.open( sPath, "wb" )
file.write( res ) file.write( res )
file.close() file.close()
print( "Downloaded as "..sFile ) print( "Downloaded as " .. sFile )
end end

View File

@ -183,6 +183,12 @@ local function completeExec( shell, nIndex, sText, tPreviousText )
return completeMultipleChoice( sText, tCommands, true ) return completeMultipleChoice( sText, tCommands, true )
end end
end end
local tWgetOptions = { "run" }
local function completeWget( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completeMultipleChoice( sText, tWgetOptions, true )
end
end
shell.setCompletionFunction( "rom/programs/alias.lua", completeAlias ) shell.setCompletionFunction( "rom/programs/alias.lua", completeAlias )
shell.setCompletionFunction( "rom/programs/cd.lua", completeDir ) shell.setCompletionFunction( "rom/programs/cd.lua", completeDir )
shell.setCompletionFunction( "rom/programs/copy.lua", completeEitherEither ) shell.setCompletionFunction( "rom/programs/copy.lua", completeEitherEither )
@ -210,6 +216,7 @@ shell.setCompletionFunction( "rom/programs/fun/advanced/paint.lua", completeFile
shell.setCompletionFunction( "rom/programs/http/pastebin.lua", completePastebin ) shell.setCompletionFunction( "rom/programs/http/pastebin.lua", completePastebin )
shell.setCompletionFunction( "rom/programs/rednet/chat.lua", completeChat ) shell.setCompletionFunction( "rom/programs/rednet/chat.lua", completeChat )
shell.setCompletionFunction( "rom/programs/command/exec.lua", completeExec ) shell.setCompletionFunction( "rom/programs/command/exec.lua", completeExec )
shell.setCompletionFunction( "rom/programs/http/wget.lua", completeWget )
if turtle then if turtle then
local tGoOptions = { "left", "right", "forward", "back", "down", "up" } local tGoOptions = { "left", "right", "forward", "back", "down", "up" }

View File

@ -0,0 +1,53 @@
local capture = require "test_helpers".capture_program
describe("The wget program", function()
local function setup_request()
stub(_G, "http", {
checkURL = function()
return true
end,
get = function()
return {
readAll = function()
return [[print("Hello", ...)]]
end,
close = function()
end,
}
end
})
end
it("downloads one file", function()
setup_request()
capture(stub, "wget", "https://example.com")
expect(fs.exists("/example.com")):eq(true)
end)
it("downloads one file with given filename", function()
setup_request()
capture(stub, "wget", "https://example.com /test-files/download")
expect(fs.exists("/test-files/download")):eq(true)
end)
it("runs a program from the internet", function()
setup_request()
expect(capture(stub, "wget", "run", "http://test.com", "a", "b", "c"))
:matches { ok = true, output = "Connecting to http://test.com... Success.\nHello a b c\n", error = "" }
end)
it("displays its usage when given no arguments", function()
setup_request()
expect(capture(stub, "wget"))
:matches { ok = true, output = "Usage:\nwget <url> [filename]\nwget run <url>\n", error = "" }
end)
it("can be completed", function()
local complete = shell.getCompletionInfo()["rom/programs/http/wget.lua"].fnComplete
expect(complete(shell, 1, "", {})):same { "run " }
end)
end)