-
Notifications
You must be signed in to change notification settings - Fork 12
Custom Super User Command #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
This seems entirely reasonable. I would probably use |
|
Thanks @bobthecow, that's a good idea. Done this in 47e6946. |
functions/sudope.fish
Outdated
|
|
||
| # 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe set -l for the fallback here? does anything outside this function need it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I understand. Thanks for your explanation and your patience.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 18d9fd2.
functions/sudope.fish
Outdated
|
|
||
| # 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably regex escape $sudope_command (string escape --style=regex -- "$sudope_command" and store it somewhere?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, I'm with you :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 11b1fca.
functions/sudope.fish
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set -l here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 18d9fd2.
|
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.