Skip to content

Commit 66727b4

Browse files
authored
only use arguments on suda#executable if it's equal to "sudo" (#60)
* only use arguments on suda#executable if it's equal to "sudo" This ensures that the sudo options work correctly, and creates *rudimentary* support for other commands (such as `doas`, see #40) * refactor: s:get_command now takes the command to run This helps avoid the printf('%s %s') which is repetative and unclear. get_command now returns the *full* command to run.
1 parent b6363e8 commit 66727b4

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

autoload/suda.vim

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
1+
function! s:get_command(opts, cmd)
2+
" TODO: should we pass '--' between a:opts and a:cmd?
3+
" TODO: should we change this api to use lists? system() allows either
4+
" strings or lists. We don't need a intermediate shell for anything though.
5+
" TODO: Should we move shell escaping to the responsibility of
6+
" suda#system/s:get_command to avoid forgetting it at the call site?
7+
return g:suda#executable ==# "sudo" && len(a:opts) > 0
8+
\ ? printf('%s %s %s', g:suda#executable, a:opts, a:cmd)
9+
\ : printf('%s %s', g:suda#executable, a:cmd)
10+
endfunction
11+
112
function! suda#system(cmd, ...) abort
213
let cmd = has('win32') || g:suda#nopass
3-
\ ? printf('%s %s', g:suda#executable, a:cmd)
4-
\ : printf('%s -p '''' -n %s', g:suda#executable, a:cmd)
14+
\ ? s:get_command('', a:cmd)
15+
\ : s:get_command('-p '''' -n', a:cmd)
516
if &verbose
617
echomsg '[suda]' cmd
718
endif
819
let result = a:0 ? system(cmd, a:1) : system(cmd)
920
if v:shell_error == 0
1021
return result
1122
endif
23+
let ask_pass = 1
1224
" Let's try running a command non-interactively. If it works, we have a sudo
1325
" timestamp that has not timed out yet. In this case there is no need to ask
1426
" for a password.
1527
" This only works if the timestamp_type is set to 'global' in the sudo
1628
" configuation file. It does not work with 'ppid', 'kernel' or 'tty'.
17-
let cmd = printf('%s -n true', g:suda#executable)
18-
let result = system(cmd)
19-
if v:shell_error == 0
20-
let cmd = printf('%s %s', g:suda#executable, a:cmd)
21-
else
29+
" Note: for non-sudo commands, don't do this, instead *always* ask for the password
30+
if g:suda#executable ==# "sudo"
31+
let cmd = s:get_command("-n", "true")
32+
let result = system(cmd)
33+
if v:shell_error == 0
34+
let cmd = s:get_command('', a:cmd)
35+
let ask_pass = 0
36+
endif
37+
endif
38+
if ask_pass == 1
2239
try
2340
call inputsave()
2441
redraw | let password = inputsecret(g:suda#prompt)
2542
finally
2643
call inputrestore()
2744
endtry
28-
let cmd = printf('%s -p '''' -S %s', g:suda#executable, a:cmd)
45+
let cmd = s:get_command('-p '''' -S', a:cmd)
2946
endif
3047
return system(cmd, password . "\n" . (a:0 ? a:1 : ''))
3148
endfunction

0 commit comments

Comments
 (0)