Skip to content

Commit 0b5c4e3

Browse files
committed
Refactor shell completion scripts + add fish comletion
1 parent a9380dd commit 0b5c4e3

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

Makefile.am

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ EXTRA_DIST =\
2929
CHANGELOG.md \
3030
src/astylerc \
3131
src/test_filesystem.cpp \
32-
scripts/usbguard-zsh-completion \
3332
scripts/modeline.vim \
3433
scripts/astyle.sh \
3534
scripts/reformat-sources.sh \
@@ -446,7 +445,17 @@ usbguard_LDADD=\
446445

447446
if ENABLE_BASH_COMPLETION
448447
bashcompletiondir = $(BASH_COMPLETION_DIR)
449-
dist_bashcompletion_DATA = $(top_srcdir)/scripts/bash_completion/usbguard
448+
dist_bashcompletion_DATA = $(top_srcdir)/scripts/shell-completion/usbguard
449+
endif
450+
451+
if ENABLE_ZSH_COMPLETION
452+
zshcompletiondir = $(ZSH_COMPLETION_DIR)
453+
dist_zshcompletion_DATA = $(top_srcdir)/scripts/shell-completion/_usbguard
454+
endif
455+
456+
if ENABLE_FISH_COMPLETION
457+
fishcompletiondir = $(FISH_COMPLETION_DIR)
458+
dist_fishcompletion_DATA = $(top_srcdir)/scripts/shell-completion/usbguard.fish
450459
endif
451460

452461
usbguard_rule_parser_SOURCES=\

configure.ac

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,30 @@ else
746746
BASH_COMPLETION_DIR="$with_bash_completion_dir"
747747
fi
748748

749+
AC_ARG_WITH([zsh-completion-dir],
750+
AS_HELP_STRING([--with-zsh-completion-dir[=PATH]],
751+
[Enable zsh auto-completion. @<:@default=yes@:>@]),
752+
[], [with_zsh_completion_dir=yes])
753+
754+
if test "x$with_zsh_completion_dir" = "xyes"; then
755+
ZSH_COMPLETION_DIR="$datadir/zsh/site-functions"
756+
else
757+
ZSH_COMPLETION_DIR="$with_zsh_completion_dir"
758+
fi
759+
760+
AC_ARG_WITH([fish-completion-dir],
761+
AS_HELP_STRING([--with-fish-completion-dir[=PATH]],
762+
[Enable fish auto-completion. Uses pkgconfig if no path given. @<:@default=yes@:>@]),
763+
[], [with_fish_completion_dir=yes])
764+
765+
if test "x$with_fish_completion_dir" = "xyes"; then
766+
PKG_CHECK_MODULES([FISH], [fish >= 3.0.0],
767+
[FISH_COMPLETION_DIR=$($PKG_CONFIG --variable=completionsdir fish)],
768+
[FISH_COMPLETION_DIR="$datadir/fish/vendor_completions.d"])
769+
else
770+
FISH_COMPLETION_DIR="$with_fish_completion_dir"
771+
fi
772+
749773
if test "x$debug" = xyes; then
750774
CXXFLAGS="$CXXFLAGS $CXXFLAGS_DEBUG_ENABLED"
751775
CFLAGS="$CFLAGS $CFLAGS_DEBUG_ENABLED"
@@ -785,6 +809,10 @@ AC_SUBST([ANALYZE_CONFIGURE_ARGS], $ac_configure_args)
785809

786810
AC_SUBST([BASH_COMPLETION_DIR])
787811
AM_CONDITIONAL([ENABLE_BASH_COMPLETION], [test "x$with_bash_completion_dir" != "xno"])
812+
AC_SUBST([ZSH_COMPLETION_DIR])
813+
AM_CONDITIONAL([ENABLE_ZSH_COMPLETION], [test "x$with_zsh_completion_dir" != "xno"])
814+
AC_SUBST([FISH_COMPLETION_DIR])
815+
AM_CONDITIONAL([ENABLE_FISH_COMPLETION], [test "x$with_fish_completion_dir" != "xno"])
788816

789817
AM_CONDITIONAL([SYSTEMD_SUPPORT_ENABLED], [test "x$systemd" = xyes ])
790818
AM_CONDITIONAL([DBUS_ENABLED], [test "x$with_dbus" = xyes ])
@@ -793,6 +821,7 @@ AM_CONDITIONAL([POLICYKIT_ENABLED], [test "x$with_polkit" = xyes])
793821
AM_CONDITIONAL([FULL_TEST_SUITE_ENABLED], [test "x$full_test_suite" = xyes])
794822
AM_CONDITIONAL([WITH_LDAP], [test "x$with_ldap" = xyes])
795823
AM_CONDITIONAL([BASH_COMPLETION_ENABLED], [test "x$bash_completion" != xno])
824+
AM_CONDITIONAL([FISH_ENABLED], [test "x$fish" != xno])
796825

797826
CXXFLAGS="$CXXFLAGS -fvisibility=hidden $COMMON_WARNING_FLAGS $WARNING_CXXFLAGS"
798827
CFLAGS="$CFLAGS -fvisibility=hidden $COMMON_WARNING_FLAGS $WARNING_CFLAGS"
@@ -845,6 +874,8 @@ echo " D-Bus Busconfig: $dbus_busconfig_dir"
845874
echo " PolicyKit Policies: $polkit_policy_dir"
846875
echo " systemd unit dir: $systemd_unit_dir"
847876
echo " Bash completion dir: $BASH_COMPLETION_DIR"
877+
echo " Zsh completion dir: $ZSH_COMPLETION_DIR"
878+
echo " Fish completion dir: $FISH_COMPLETION_DIR"
848879
echo
849880
echo "## Compilation Flags"
850881
echo
File renamed without changes.
File renamed without changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
function __fish_usbguard_complete_first_subcommand
2+
set -l cmd (commandline -opc)
3+
if test (count $cmd) -eq 1
4+
return 0
5+
end
6+
return 1
7+
end
8+
9+
set -l subcommands \
10+
"get-parameter\tGet the value of a runtime parameter" \
11+
"set-parameter\tSet the value of a runtime parameter" \
12+
"list-devices\tList all USB devices recognized by the USBGuard daemon" \
13+
"allow-device\tAuthorize a device to interact with the system" \
14+
"block-device\tDeauthorize a device" \
15+
"reject-device\tDeauthorize and remove a device" \
16+
"list-rules\tList the rule set (policy) used by the USBGuard daemon" \
17+
"append-rule\tAppend the rule to ther current rule set" \
18+
"remove-rule\tRemove a rule identified by the rule id from the rule set" \
19+
"generate-policy\tGenerate a rule set (policy) which authorizes currently connected USB devices" \
20+
"watch\tWatch the IPC interface events and print them to stdout" \
21+
"read-descriptor\tRead a USB descriptor from a file and print it in human-readable format" \
22+
"add-user\tCreate an IPC access control file allowing user/group to use the USBGuard IPC bus" \
23+
"remove-user\tRemove an IPC access control file associated with the user/group"
24+
25+
complete -c usbguard -x
26+
27+
for part in $subcommands
28+
set -l cmd (string split -f 1 "\t" "$part")
29+
30+
complete -c usbguard -x -n "__fish_usbguard_complete_first_subcommand" -a "$cmd" -d (string split -f 2 "\t" "$part")
31+
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "h" -l "help" -d "Show help"
32+
end
33+
34+
for cmd in get-parameter set-parameter
35+
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd; and not __fish_seen_subcommand_from InsertedDevicePolicy; and not __fish_seen_subcommand_from ImplicitPolicyTarget" -a "InsertedDevicePolicy ImplicitPolicyTarget"
36+
end
37+
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter" -s "v" -l "verbose" -d "Print the previous and new attribute value"
38+
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from InsertedDevicePolicy" -a "block" -d "Deauthorize every present device"
39+
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from InsertedDevicePolicy" -a "reject" -d "Remove every present device"
40+
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from InsertedDevicePolicy" -a "apply-policy" -d "Evaluate the ruleset for every present device"
41+
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from ImplicitPolicyTarget" -a "allow" -d "Authorize the device"
42+
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from ImplicitPolicyTarget" -a "block" -d "Block the device"
43+
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from ImplicitPolicyTarget" -a "reject" -d "Remove the device"
44+
45+
complete -c usbguard -x -n "__fish_seen_subcommand_from list-devices" -s "a" -l "allowed" -d "List allowed devices"
46+
complete -c usbguard -x -n "__fish_seen_subcommand_from list-devices" -s "b" -l "blocked" -d "List blocked devices"
47+
complete -c usbguard -x -n "__fish_seen_subcommand_from list-devices" -s "t" -l "tree" -d "List devices in a tree format"
48+
49+
for cmd in allow-device block-device reject-device
50+
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "p" -l "permanent" -d "Make the decision permanent"
51+
end
52+
53+
complete -c usbguard -x -n "__fish_seen_subcommand_from list-rules" -s "d" -l "show-devices" -d "Show all devices which are affected by the specific rule"
54+
complete -c usbguard -x -n "__fish_seen_subcommand_from list-rules" -s "l" -l "label" -d "Only show rules having a specific label"
55+
56+
complete -c usbguard -x -n "__fish_seen_subcommand_from append-rule" -s "a" -l "after" -d "Append the new rule after a rule with the specified rule id"
57+
complete -c usbguard -x -n "__fish_seen_subcommand_from append-rule" -s "t" -l "temporary" -d "Make the desicion temporary"
58+
59+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "p" -l "with-ports" -d "Generate port specific rules for all devices"
60+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "P" -l "no-ports-sn" -d "Don't generate port specific rules for devices without an iSerial value"
61+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "d" -l "devpath" -d "Only generate a rule for devices at the specified sub path of /sys"
62+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "t" -l "target" -d "Generate an explicit \"catch all\" rule with the specified target"
63+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "X" -l "no-hashes" -d "Don't generate a hash attribute for each device"
64+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "H" -l "hash-only" -d "Generate a hash-only policy"
65+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "L" -l "ldif" -d "Generate a ldif policy for LDAP"
66+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "b" -l "usbguardbase" -d "Generate a ldif policy for LDAP with specified base"
67+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "o" -l "objectclass" -d "Generate a ldif policy for LDAP with specified objectClass"
68+
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "n" -l "name-prefix" -d "Generate a ldif policy for LDAP with specified name prefix"
69+
70+
complete -c usbguard -x -n "__fish_seen_subcommand_from watch" -s "w" -l "wait" -d "Wait for IPC connection to become available"
71+
complete -c usbguard -x -n "__fish_seen_subcommand_from watch" -s "o" -l "once" -d "Wait only when starting, if needed"
72+
complete -c usbguard -F -r -n "__fish_seen_subcommand_from watch" -s "e" -l "exec" -d "Run an executable file located at specified path for every event"
73+
74+
for cmd in add-user remove-user
75+
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -a "(__fish_complete_users)"
76+
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -a "(__fish_complete_user_ids)"
77+
complete -c usbguard -n "__fish_seen_subcommand_from $cmd" -s "u" -l "user" -d "Specified name representing a username or UID (default)"
78+
79+
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "g" -l "group" -a "(__fish_complete_groups)"
80+
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "g" -l "group" -a "(__fish_complete_group_ids)"
81+
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "g" -l "group" -d "Specified name representing a group name or GID"
82+
end
83+
complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "p" -l "policy" -d "Policy related previleges"
84+
complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "d" -l "devices" -d "Device related privileges"
85+
complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "e" -l "exceptions" -d "Exceptions related privileges"
86+
complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "P" -l "parameters" -d "Run-time parameter related privileges"

0 commit comments

Comments
 (0)