From 0b5c4e347b0f68a37af10065391daefced27771f Mon Sep 17 00:00:00 2001 From: FollieHiyuki Date: Sun, 18 Sep 2022 22:46:49 +0700 Subject: [PATCH] Refactor shell completion scripts + add fish comletion --- Makefile.am | 13 ++- configure.ac | 31 +++++++ .../_usbguard} | 0 .../usbguard | 0 scripts/shell-completion/usbguard.fish | 86 +++++++++++++++++++ 5 files changed, 128 insertions(+), 2 deletions(-) rename scripts/{usbguard-zsh-completion => shell-completion/_usbguard} (100%) rename scripts/{bash_completion => shell-completion}/usbguard (100%) create mode 100644 scripts/shell-completion/usbguard.fish diff --git a/Makefile.am b/Makefile.am index f4ce03d8..3132f07a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -29,7 +29,6 @@ EXTRA_DIST =\ CHANGELOG.md \ src/astylerc \ src/test_filesystem.cpp \ - scripts/usbguard-zsh-completion \ scripts/modeline.vim \ scripts/astyle.sh \ scripts/reformat-sources.sh \ @@ -446,7 +445,17 @@ usbguard_LDADD=\ if ENABLE_BASH_COMPLETION bashcompletiondir = $(BASH_COMPLETION_DIR) -dist_bashcompletion_DATA = $(top_srcdir)/scripts/bash_completion/usbguard +dist_bashcompletion_DATA = $(top_srcdir)/scripts/shell-completion/usbguard +endif + +if ENABLE_ZSH_COMPLETION +zshcompletiondir = $(ZSH_COMPLETION_DIR) +dist_zshcompletion_DATA = $(top_srcdir)/scripts/shell-completion/_usbguard +endif + +if ENABLE_FISH_COMPLETION +fishcompletiondir = $(FISH_COMPLETION_DIR) +dist_fishcompletion_DATA = $(top_srcdir)/scripts/shell-completion/usbguard.fish endif usbguard_rule_parser_SOURCES=\ diff --git a/configure.ac b/configure.ac index 16b0c96e..0b9b034f 100644 --- a/configure.ac +++ b/configure.ac @@ -746,6 +746,30 @@ else BASH_COMPLETION_DIR="$with_bash_completion_dir" fi +AC_ARG_WITH([zsh-completion-dir], + AS_HELP_STRING([--with-zsh-completion-dir[=PATH]], + [Enable zsh auto-completion. @<:@default=yes@:>@]), + [], [with_zsh_completion_dir=yes]) + +if test "x$with_zsh_completion_dir" = "xyes"; then + ZSH_COMPLETION_DIR="$datadir/zsh/site-functions" +else + ZSH_COMPLETION_DIR="$with_zsh_completion_dir" +fi + +AC_ARG_WITH([fish-completion-dir], + AS_HELP_STRING([--with-fish-completion-dir[=PATH]], + [Enable fish auto-completion. Uses pkgconfig if no path given. @<:@default=yes@:>@]), + [], [with_fish_completion_dir=yes]) + +if test "x$with_fish_completion_dir" = "xyes"; then + PKG_CHECK_MODULES([FISH], [fish >= 3.0.0], + [FISH_COMPLETION_DIR=$($PKG_CONFIG --variable=completionsdir fish)], + [FISH_COMPLETION_DIR="$datadir/fish/vendor_completions.d"]) +else + FISH_COMPLETION_DIR="$with_fish_completion_dir" +fi + if test "x$debug" = xyes; then CXXFLAGS="$CXXFLAGS $CXXFLAGS_DEBUG_ENABLED" CFLAGS="$CFLAGS $CFLAGS_DEBUG_ENABLED" @@ -785,6 +809,10 @@ AC_SUBST([ANALYZE_CONFIGURE_ARGS], $ac_configure_args) AC_SUBST([BASH_COMPLETION_DIR]) AM_CONDITIONAL([ENABLE_BASH_COMPLETION], [test "x$with_bash_completion_dir" != "xno"]) +AC_SUBST([ZSH_COMPLETION_DIR]) +AM_CONDITIONAL([ENABLE_ZSH_COMPLETION], [test "x$with_zsh_completion_dir" != "xno"]) +AC_SUBST([FISH_COMPLETION_DIR]) +AM_CONDITIONAL([ENABLE_FISH_COMPLETION], [test "x$with_fish_completion_dir" != "xno"]) AM_CONDITIONAL([SYSTEMD_SUPPORT_ENABLED], [test "x$systemd" = xyes ]) AM_CONDITIONAL([DBUS_ENABLED], [test "x$with_dbus" = xyes ]) @@ -793,6 +821,7 @@ AM_CONDITIONAL([POLICYKIT_ENABLED], [test "x$with_polkit" = xyes]) AM_CONDITIONAL([FULL_TEST_SUITE_ENABLED], [test "x$full_test_suite" = xyes]) AM_CONDITIONAL([WITH_LDAP], [test "x$with_ldap" = xyes]) AM_CONDITIONAL([BASH_COMPLETION_ENABLED], [test "x$bash_completion" != xno]) +AM_CONDITIONAL([FISH_ENABLED], [test "x$fish" != xno]) CXXFLAGS="$CXXFLAGS -fvisibility=hidden $COMMON_WARNING_FLAGS $WARNING_CXXFLAGS" CFLAGS="$CFLAGS -fvisibility=hidden $COMMON_WARNING_FLAGS $WARNING_CFLAGS" @@ -845,6 +874,8 @@ echo " D-Bus Busconfig: $dbus_busconfig_dir" echo " PolicyKit Policies: $polkit_policy_dir" echo " systemd unit dir: $systemd_unit_dir" echo " Bash completion dir: $BASH_COMPLETION_DIR" +echo " Zsh completion dir: $ZSH_COMPLETION_DIR" +echo " Fish completion dir: $FISH_COMPLETION_DIR" echo echo "## Compilation Flags" echo diff --git a/scripts/usbguard-zsh-completion b/scripts/shell-completion/_usbguard similarity index 100% rename from scripts/usbguard-zsh-completion rename to scripts/shell-completion/_usbguard diff --git a/scripts/bash_completion/usbguard b/scripts/shell-completion/usbguard similarity index 100% rename from scripts/bash_completion/usbguard rename to scripts/shell-completion/usbguard diff --git a/scripts/shell-completion/usbguard.fish b/scripts/shell-completion/usbguard.fish new file mode 100644 index 00000000..02554923 --- /dev/null +++ b/scripts/shell-completion/usbguard.fish @@ -0,0 +1,86 @@ +function __fish_usbguard_complete_first_subcommand + set -l cmd (commandline -opc) + if test (count $cmd) -eq 1 + return 0 + end + return 1 +end + +set -l subcommands \ + "get-parameter\tGet the value of a runtime parameter" \ + "set-parameter\tSet the value of a runtime parameter" \ + "list-devices\tList all USB devices recognized by the USBGuard daemon" \ + "allow-device\tAuthorize a device to interact with the system" \ + "block-device\tDeauthorize a device" \ + "reject-device\tDeauthorize and remove a device" \ + "list-rules\tList the rule set (policy) used by the USBGuard daemon" \ + "append-rule\tAppend the rule to ther current rule set" \ + "remove-rule\tRemove a rule identified by the rule id from the rule set" \ + "generate-policy\tGenerate a rule set (policy) which authorizes currently connected USB devices" \ + "watch\tWatch the IPC interface events and print them to stdout" \ + "read-descriptor\tRead a USB descriptor from a file and print it in human-readable format" \ + "add-user\tCreate an IPC access control file allowing user/group to use the USBGuard IPC bus" \ + "remove-user\tRemove an IPC access control file associated with the user/group" + +complete -c usbguard -x + +for part in $subcommands + set -l cmd (string split -f 1 "\t" "$part") + + complete -c usbguard -x -n "__fish_usbguard_complete_first_subcommand" -a "$cmd" -d (string split -f 2 "\t" "$part") + complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "h" -l "help" -d "Show help" +end + +for cmd in get-parameter set-parameter + 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" +end +complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter" -s "v" -l "verbose" -d "Print the previous and new attribute value" +complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from InsertedDevicePolicy" -a "block" -d "Deauthorize every present device" +complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from InsertedDevicePolicy" -a "reject" -d "Remove every present device" +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" +complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from ImplicitPolicyTarget" -a "allow" -d "Authorize the device" +complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from ImplicitPolicyTarget" -a "block" -d "Block the device" +complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from ImplicitPolicyTarget" -a "reject" -d "Remove the device" + +complete -c usbguard -x -n "__fish_seen_subcommand_from list-devices" -s "a" -l "allowed" -d "List allowed devices" +complete -c usbguard -x -n "__fish_seen_subcommand_from list-devices" -s "b" -l "blocked" -d "List blocked devices" +complete -c usbguard -x -n "__fish_seen_subcommand_from list-devices" -s "t" -l "tree" -d "List devices in a tree format" + +for cmd in allow-device block-device reject-device + complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "p" -l "permanent" -d "Make the decision permanent" +end + +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" +complete -c usbguard -x -n "__fish_seen_subcommand_from list-rules" -s "l" -l "label" -d "Only show rules having a specific label" + +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" +complete -c usbguard -x -n "__fish_seen_subcommand_from append-rule" -s "t" -l "temporary" -d "Make the desicion temporary" + +complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "p" -l "with-ports" -d "Generate port specific rules for all devices" +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" +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" +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" +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" +complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "H" -l "hash-only" -d "Generate a hash-only policy" +complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "L" -l "ldif" -d "Generate a ldif policy for LDAP" +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" +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" +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" + +complete -c usbguard -x -n "__fish_seen_subcommand_from watch" -s "w" -l "wait" -d "Wait for IPC connection to become available" +complete -c usbguard -x -n "__fish_seen_subcommand_from watch" -s "o" -l "once" -d "Wait only when starting, if needed" +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" + +for cmd in add-user remove-user + complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -a "(__fish_complete_users)" + complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -a "(__fish_complete_user_ids)" + complete -c usbguard -n "__fish_seen_subcommand_from $cmd" -s "u" -l "user" -d "Specified name representing a username or UID (default)" + + complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "g" -l "group" -a "(__fish_complete_groups)" + complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "g" -l "group" -a "(__fish_complete_group_ids)" + complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "g" -l "group" -d "Specified name representing a group name or GID" +end +complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "p" -l "policy" -d "Policy related previleges" +complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "d" -l "devices" -d "Device related privileges" +complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "e" -l "exceptions" -d "Exceptions related privileges" +complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "P" -l "parameters" -d "Run-time parameter related privileges"