2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-07-01 16:52:03 +00:00

feat: warn when using rm with wildcard (*)

This commit is contained in:
sammyette 2025-06-10 19:06:18 -04:00
parent 1bb433dc64
commit 17324e28b8
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 77 additions and 6 deletions

View File

@ -361,6 +361,8 @@ function Greenhouse:initUi()
self = nil
bait.release('signal.sigint', sigint)
bait.release('signal.resize', resize)
ansikit.clear()
end
return Greenhouse

View File

@ -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'

View File

@ -40,6 +40,7 @@ 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 then
if processed.history ~= nil then history = processed.history end
if processed.command then command = processed.command end
if not processed.continue then
@ -48,6 +49,7 @@ function hilbish.processors.execute(command, opts)
end
end
end
end
return {
command = command,

View 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
}