mirror of
https://github.com/Hilbis/Hilbish
synced 2025-07-01 08:42:04 +00:00
feat: warn when using rm with wildcard (*
)
This commit is contained in:
parent
1bb433dc64
commit
17324e28b8
@ -361,6 +361,8 @@ function Greenhouse:initUi()
|
||||
self = nil
|
||||
bait.release('signal.sigint', sigint)
|
||||
bait.release('signal.resize', resize)
|
||||
|
||||
ansikit.clear()
|
||||
end
|
||||
|
||||
return Greenhouse
|
||||
|
@ -19,7 +19,9 @@ table.insert(package.searchers, function(module)
|
||||
end)
|
||||
|
||||
require 'nature.hilbish'
|
||||
|
||||
require 'nature.processors'
|
||||
require 'nature.processors.wildcardWarn'
|
||||
|
||||
require 'nature.commands'
|
||||
require 'nature.completions'
|
||||
|
@ -40,11 +40,13 @@ function hilbish.processors.execute(command, opts)
|
||||
for _, processor in ipairs(hilbish.processors.list) do
|
||||
if not contains(opts.skip, processor.name) then
|
||||
local processed = processor.func(command)
|
||||
if processed.history ~= nil then history = processed.history end
|
||||
if processed.command then command = processed.command end
|
||||
if not processed.continue then
|
||||
continue = false
|
||||
break
|
||||
if processed then
|
||||
if processed.history ~= nil then history = processed.history end
|
||||
if processed.command then command = processed.command end
|
||||
if not processed.continue then
|
||||
continue = false
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -54,4 +56,4 @@ function hilbish.processors.execute(command, opts)
|
||||
continue = continue,
|
||||
history = history
|
||||
}
|
||||
end
|
||||
end
|
65
nature/processors/wildcardWarn.lua
Normal file
65
nature/processors/wildcardWarn.lua
Normal file
@ -0,0 +1,65 @@
|
||||
local hilbish = require 'hilbish'
|
||||
local Greenhouse = require 'nature.greenhouse'
|
||||
local Page = require 'nature.greenhouse.page'
|
||||
|
||||
print 'wildcard warn loaded'
|
||||
|
||||
local function contains(search, needle)
|
||||
for _, p in ipairs(search) do
|
||||
if p:match(needle) then
|
||||
return p
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
local stdoutSink = {}
|
||||
function stdoutSink:write(t)
|
||||
io.write(t)
|
||||
end
|
||||
|
||||
function stdoutSink:writeln(t)
|
||||
print(t)
|
||||
end
|
||||
|
||||
hilbish.processors.add {
|
||||
name = 'wildcardWarn',
|
||||
func = function(commandLine)
|
||||
local args = string.split(commandLine, ' ')
|
||||
if args[1] == 'rm' then -- command check
|
||||
local match1 = contains(args, '%*')
|
||||
local match2 = contains(args, '/%*')
|
||||
if match1 or match2 then
|
||||
local matches = fs.glob(match1 or match2)
|
||||
if #matches == 0 then
|
||||
return {continue = true}
|
||||
end
|
||||
|
||||
::askWithInfo::
|
||||
print 'Detected wildcard with potentially dangerous command.'
|
||||
print 'Are you sure you want to run this command?'
|
||||
|
||||
::ask::
|
||||
local ans = hilbish.read '(Y/N, L to list files matched with wildcard) '
|
||||
ans = ans:lower()
|
||||
if ans == 'l' then
|
||||
local gh = Greenhouse(stdoutSink)
|
||||
local page = Page('Wildcard File List', '')
|
||||
page.lines = matches
|
||||
|
||||
gh:addPage(page)
|
||||
gh:initUi()
|
||||
goto askWithInfo
|
||||
elseif ans == 'y' then
|
||||
return {continue = true}
|
||||
elseif ans == 'n' then
|
||||
return {continue = false}
|
||||
else
|
||||
print 'Invalid answer..'
|
||||
goto ask
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user