|
| 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 | + |
1 | 12 | function! suda#system(cmd, ...) abort |
2 | 13 | 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) |
5 | 16 | if &verbose |
6 | 17 | echomsg '[suda]' cmd |
7 | 18 | endif |
8 | 19 | let result = a:0 ? system(cmd, a:1) : system(cmd) |
9 | 20 | if v:shell_error == 0 |
10 | 21 | return result |
11 | 22 | endif |
| 23 | + let ask_pass = 1 |
12 | 24 | " Let's try running a command non-interactively. If it works, we have a sudo |
13 | 25 | " timestamp that has not timed out yet. In this case there is no need to ask |
14 | 26 | " for a password. |
15 | 27 | " This only works if the timestamp_type is set to 'global' in the sudo |
16 | 28 | " 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 |
22 | 39 | try |
23 | 40 | call inputsave() |
24 | 41 | redraw | let password = inputsecret(g:suda#prompt) |
25 | 42 | finally |
26 | 43 | call inputrestore() |
27 | 44 | endtry |
28 | | - let cmd = printf('%s -p '''' -S %s', g:suda#executable, a:cmd) |
| 45 | + let cmd = s:get_command('-p '''' -S', a:cmd) |
29 | 46 | endif |
30 | 47 | return system(cmd, password . "\n" . (a:0 ? a:1 : '')) |
31 | 48 | endfunction |
|
0 commit comments