mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-12 03:00:30 +00:00
Allow multiple arguments for mkdir (#216)
This commit is contained in:
parent
99bdff0f92
commit
a81db2cda6
@ -2,3 +2,4 @@ View the source code at https://github.com/SquidDev-CC/CC-Tweaked
|
||||
View the documentation at https://wiki.computercraft.cc
|
||||
Visit the forum at https://forums.computercraft.cc
|
||||
You can disable these messages by running "set motd.enable false"
|
||||
You can create a directory with "mkdir".
|
||||
|
@ -1,15 +1,17 @@
|
||||
local tArgs = { ... }
|
||||
|
||||
if #tArgs < 1 then
|
||||
print( "Usage: mkdir <path>" )
|
||||
print( "Usage: mkdir <paths>" )
|
||||
return
|
||||
end
|
||||
|
||||
local sNewDir = shell.resolve( tArgs[1] )
|
||||
|
||||
if fs.exists( sNewDir ) and not fs.isDir(sNewDir) then
|
||||
printError( "Destination exists" )
|
||||
return
|
||||
for _, v in ipairs( tArgs ) do
|
||||
local sNewDir = shell.resolve( v )
|
||||
if fs.exists( sNewDir ) and not fs.isDir( sNewDir ) then
|
||||
printError( v..": Destination exists" )
|
||||
elseif fs.isReadOnly( sNewDir ) then
|
||||
printError( v..": Access denied" )
|
||||
else
|
||||
fs.makeDir( sNewDir )
|
||||
end
|
||||
end
|
||||
|
||||
fs.makeDir( sNewDir )
|
||||
|
||||
|
@ -66,6 +66,9 @@ local function completeFile( shell, nIndex, sText, tPreviousText )
|
||||
return fs.complete( sText, shell.dir(), true, false )
|
||||
end
|
||||
end
|
||||
local function completeFileMany( shell, nIndex, sText, tPreviousText )
|
||||
return fs.complete( sText, shell.dir(), true, false )
|
||||
end
|
||||
local function completeDir( shell, nIndex, sText, tPreviousText )
|
||||
if nIndex == 1 then
|
||||
return fs.complete( sText, shell.dir(), false, true )
|
||||
@ -192,7 +195,7 @@ shell.setCompletionFunction( "rom/programs/help.lua", completeHelp )
|
||||
shell.setCompletionFunction( "rom/programs/id.lua", completePeripheral )
|
||||
shell.setCompletionFunction( "rom/programs/label.lua", completeLabel )
|
||||
shell.setCompletionFunction( "rom/programs/list.lua", completeDir )
|
||||
shell.setCompletionFunction( "rom/programs/mkdir.lua", completeFile )
|
||||
shell.setCompletionFunction( "rom/programs/mkdir.lua", completeFileMany )
|
||||
shell.setCompletionFunction( "rom/programs/monitor.lua", completeMonitor )
|
||||
shell.setCompletionFunction( "rom/programs/move.lua", completeEitherEither )
|
||||
shell.setCompletionFunction( "rom/programs/redstone.lua", completeRedstone )
|
||||
|
@ -47,22 +47,23 @@ local function try(fn)
|
||||
end
|
||||
|
||||
local ok, err = xpcall(fn, function(err)
|
||||
return { message = err, trace = debug.traceback() }
|
||||
return { message = err, trace = debug.traceback(nil, 2) }
|
||||
end)
|
||||
|
||||
-- Restore a whole bunch of state
|
||||
io.input(io.stdin)
|
||||
io.output(io.stdout)
|
||||
|
||||
-- If we're an existing error, or we succeded then propagate it.
|
||||
-- If we succeeded, propagate it
|
||||
if ok then return ok, err end
|
||||
|
||||
-- Error handling failed for some reason - just return a simpler error
|
||||
if type(err) ~= "table" then
|
||||
return setmetatable({ message = tostring(err) }, error_mt)
|
||||
return ok, setmetatable({ message = tostring(err) }, error_mt)
|
||||
end
|
||||
|
||||
if getmetatable(err.message) == error_mt then return ok, err.message end
|
||||
|
||||
-- Find the common substring between the two traces. Yes, this is horrible.
|
||||
-- Find the common substring the errors' trace and the current one. Then
|
||||
-- eliminate it.
|
||||
local trace = debug.traceback()
|
||||
for i = 1, #trace do
|
||||
if trace:sub(-i) ~= err.trace:sub(-i) then
|
||||
@ -71,6 +72,12 @@ local function try(fn)
|
||||
end
|
||||
end
|
||||
|
||||
-- If we've received a rethrown error, copy
|
||||
if getmetatable(err.message) == error_mt then
|
||||
for k, v in pairs(err.message) do err[k] = v end
|
||||
return ok, err
|
||||
end
|
||||
|
||||
return ok, setmetatable(err, error_mt)
|
||||
end
|
||||
|
||||
|
29
src/test/resources/test-rom/spec/programs/mkdir_spec.lua
Normal file
29
src/test/resources/test-rom/spec/programs/mkdir_spec.lua
Normal file
@ -0,0 +1,29 @@
|
||||
describe("The mkdir program", function()
|
||||
it("creates a directory", function()
|
||||
fs.delete("/test-files")
|
||||
|
||||
shell.run("mkdir /test-files/a")
|
||||
|
||||
expect(fs.isDir("/test-files/a")):eq(true)
|
||||
end)
|
||||
|
||||
it("creates many directories", function()
|
||||
fs.delete("/test-files")
|
||||
|
||||
shell.run("mkdir /test-files/a /test-files/b")
|
||||
|
||||
expect(fs.isDir("/test-files/a")):eq(true)
|
||||
expect(fs.isDir("/test-files/b")):eq(true)
|
||||
end)
|
||||
|
||||
it("can be completed", function()
|
||||
fs.delete("/test-files")
|
||||
fs.makeDir("/test-files/a")
|
||||
fs.makeDir("/test-files/b")
|
||||
io.open("/test-files.a.txt", "w"):close()
|
||||
|
||||
local complete = shell.getCompletionInfo()["rom/programs/mkdir.lua"].fnComplete
|
||||
expect(complete(shell, 1, "/test-files/", {})):same { "a/", "b/" }
|
||||
expect(complete(shell, 2, "/test-files/", { "/" })):same { "a/", "b/" }
|
||||
end)
|
||||
end)
|
Loading…
Reference in New Issue
Block a user