Custom Super User Command - #15
Conversation
|
This seems entirely reasonable. I would probably use |
|
Thanks @bobthecow, that's a good idea. Done this in 47e6946. |
|
|
||
| # If no custom super user command defaults to sudo | ||
| if test -z "$sudope_command" | ||
| set sudope_command 'sudo' |
There was a problem hiding this comment.
maybe set -l for the fallback here? does anything outside this function need it?
There was a problem hiding this comment.
Sorry, I'm new to fish, but I can read in help set:
If a variable is not explicitly set to be either universal, global or
local and has never before been defined, the variable will be local to
the currently executing function. Note that this is different from
using the -l or --local flag. If one of those flags is used, the
variable will be local to the most inner currently executing block,
while without these the variable will be local to the function. If no
function is executing, the variable will be global.
So I think that all these -l flag in this file are not useful. Correct me if I'm wrong, please.
There was a problem hiding this comment.
using unspecified variable scope introduces opportunities to pollute global state, e.g.:
set -gx sudope_command
echo $sudope_command # nothing
function thing
if test -z "$sudope_command"
set sudope_command sudo
end
end
thing
echo $sudope_command # "sudo"if you used set -q rather than test -z to check for a user-defined value, it would have only set the fallback locally. but it also would have failed if $sudope_command were set to empty, as above.
i tend to use a pattern that always sets locally, to avoid any chance of this sort of thing. in this case, we could either introduce a brand new variable name, or we can use set -l outside a block:
set -gx sudope_command
echo $sudope_command # nothing
function thing
test -z "$sudope_command"
or set -l sudope_command sudo
end
thing
echo $sudope_command # nothingThere was a problem hiding this comment.
Now I understand. Thanks for your explanation and your patience.
|
|
||
| # Parse the command line (first line only). | ||
| set -l command_parts (string match -ir '^(\s*)(sudo(\s+|$))?(.*)' $command_buffer[1]) | ||
| set -l command_parts (string match -ir "^(\s*)($sudope_command(\s+|\$))?(.*)" $command_buffer[1]) |
There was a problem hiding this comment.
we should probably regex escape $sudope_command (string escape --style=regex -- "$sudope_command" and store it somewhere?)
There was a problem hiding this comment.
I didn't know the string builtin. Cool stuff, thanks! Can you explain to me its role here? $sudope_command it's supposed to be a shell command (eg. sudo, doas, et cetera). Are you thinking about something like mkfs.ext2, that in fact it's a problem here?
There was a problem hiding this comment.
yeah, . was the thing i was most interested in. but mostly it's defensive programming. interpolating user-provided values into a regex introduces opportunities for things to go wrong, and it's unnecessary when we have a way to escape them right here :)
| if test $cursor_position -ge (math $lead_length+$sudo_length) | ||
| # The cursor was after "sudo". | ||
| set cursor_position (math $cursor_position-$sudo_length) | ||
| set sudope_command_length (string length -- "$command_parts[3]") |
There was a problem hiding this comment.
Again from help set:
If a variable is not explicitly set to be either universal, global or
local, but has been previously defined, the previous variable scope is
used.
There was a problem hiding this comment.
right. i thought we wanted this one to be block scoped? do we want it to persist after this case block?
There was a problem hiding this comment.
In the contest of your reply above (#15 (comment)), it makes no sense to make it persistent. I'll fix all these issues in my next commits. Thanks.
|
e1492c6 fixes a bug introduced by previous changes in my branch: when the super user command was added to the command line, the following white space was not considered in pushing the cursor ahead. |
This PR adds and documents the possibility to set a custom super user command to prepend to the command line, such as
doasinstead ofsudofor *BSD users.You can set the custom su command in the variable
su_command.