mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-13 03:30:29 +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:
parent
a0e7c4a74c
commit
309cbdb8be
@ -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://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 run http://pastebin.com/raw/CxaWmPrX" will download the file from the URL http://pastebin.com/raw/CxaWmPrX and run it immediately.
|
||||
|
@ -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".
|
||||
Use "pastebin put" to upload a program to 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.
|
||||
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.
|
||||
|
@ -2,14 +2,24 @@
|
||||
local function printUsage()
|
||||
print( "Usage:" )
|
||||
print( "wget <url> [filename]" )
|
||||
print( "wget run <url>" )
|
||||
end
|
||||
|
||||
local tArgs = { ... }
|
||||
|
||||
local run = false
|
||||
if tArgs[1] == "run" then
|
||||
table.remove( tArgs, 1 )
|
||||
run = true
|
||||
end
|
||||
|
||||
if #tArgs < 1 then
|
||||
printUsage()
|
||||
return
|
||||
end
|
||||
|
||||
local url = table.remove( tArgs, 1 )
|
||||
|
||||
if not http then
|
||||
printError( "wget requires http API" )
|
||||
printError( "Set http_enable to true in ComputerCraft.cfg" )
|
||||
@ -22,6 +32,13 @@ local function getFilename( sUrl )
|
||||
end
|
||||
|
||||
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 .. "... " )
|
||||
|
||||
local response = http.get( sUrl , nil , true )
|
||||
@ -37,29 +54,34 @@ local function get( sUrl )
|
||||
return sResponse
|
||||
end
|
||||
|
||||
-- Determine file to download
|
||||
local sUrl = tArgs[1]
|
||||
if run then
|
||||
local res = get(url)
|
||||
if not res then return end
|
||||
|
||||
--Check if the URL is valid
|
||||
local ok, err = http.checkURL( sUrl )
|
||||
if not ok then
|
||||
printError( err or "Invalid URL." )
|
||||
return
|
||||
end
|
||||
local func, err = load(res, getFilename(url), "t", _ENV)
|
||||
if not func then
|
||||
printError(err)
|
||||
return
|
||||
end
|
||||
|
||||
local sFile = tArgs[2] or getFilename( sUrl )
|
||||
local sPath = shell.resolve( sFile )
|
||||
if fs.exists( sPath ) then
|
||||
print( "File already exists" )
|
||||
return
|
||||
end
|
||||
local ok, err = pcall(func, table.unpack(tArgs))
|
||||
if not ok 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" )
|
||||
return
|
||||
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" )
|
||||
file.write( res )
|
||||
file.close()
|
||||
|
||||
print( "Downloaded as "..sFile )
|
||||
print( "Downloaded as " .. sFile )
|
||||
end
|
||||
|
@ -183,6 +183,12 @@ local function completeExec( shell, nIndex, sText, tPreviousText )
|
||||
return completeMultipleChoice( sText, tCommands, true )
|
||||
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/cd.lua", completeDir )
|
||||
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/rednet/chat.lua", completeChat )
|
||||
shell.setCompletionFunction( "rom/programs/command/exec.lua", completeExec )
|
||||
shell.setCompletionFunction( "rom/programs/http/wget.lua", completeWget )
|
||||
|
||||
if turtle then
|
||||
local tGoOptions = { "left", "right", "forward", "back", "down", "up" }
|
||||
|
53
src/test/resources/test-rom/spec/http/wget_spec.lua
Normal file
53
src/test/resources/test-rom/spec/http/wget_spec.lua
Normal 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)
|
Loading…
Reference in New Issue
Block a user