Skip to content
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

feat(doas): new completion #766

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions completions/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bashcomp_DATA = 2to3 \
dmypy \
dnssec-keygen \
dnsspoof \
doas \
dot \
dpkg \
dpkg-source \
Expand Down
45 changes: 45 additions & 0 deletions completions/doas
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# doas(1) completion -*- shell-script -*-

_comp_cmd_doas()
{
local cur prev words cword split
_init_completion -s || return

local i

for ((i = 1; i <= cword; i++)); do
if [[ ${words[i]} != -* ]]; then
local PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
local root_command=${words[i]}
_command_offset $i
return
fi
[[ ${words[i]} == -@(!(-*)[uCLs]) ]] &&

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between what you wrote and my suggestion below? They both seem to work, but my suggestion seems simpler.

Suggested change
[[ ${words[i]} == -@(!(-*)[uCLs]) ]] &&
[[ ${words[i]} == -@([uCLs]) ]] &&

((i++))
done

case "$prev" in
-!(-*)u)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above; I simply use -u and -C in my personal doas completion file.

Copy link
Owner

@scop scop Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will fail to match the case where u or C is the last in a set of bundled short options, like doas -nu <TAB>. Similarly above.

COMPREPLY=($(compgen -u -- "$cur"))
return
;;
-!(-*)C)
_filedir
return
;;
-!(-*)[Ls])
return
;;
esac

$split && return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_usage "$1")' -- "$cur"))
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
return
fi
} &&
complete -F _comp_cmd_doas doas

# ex: filetype=sh
1 change: 1 addition & 0 deletions test/t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ EXTRA_DIST = \
test_dmypy.py \
test_dnssec_keygen.py \
test_dnsspoof.py \
test_doas.py \
test_dot.py \
test_dpkg.py \
test_dpkg_deb.py \
Expand Down
17 changes: 17 additions & 0 deletions test/t/test_doas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest


class TestDoas:
@pytest.mark.complete("doas -", require_cmd=True)
def test_1(self, completion):
assert completion

@pytest.mark.complete("doas cd foo", cwd="shared/default")
def test_2(self, completion):
assert completion == ".d/"
assert not completion.endswith(" ")

@pytest.mark.complete("doas sh share")
def test_3(self, completion):
assert completion == "d/"
assert not completion.endswith(" ")
1 change: 1 addition & 0 deletions test/test-cmd-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ display
dmesg
dmypy
dnsspoof
doas
dpkg
dpkg-deb
dpkg-query
Expand Down