]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
shell-completion/*/insmod: add bash/fish/zsh completion
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Sun, 22 Sep 2024 21:11:57 +0000 (16:11 -0500)
A few inline todos and some odd fish behaviour as mentioned inline.
Otherwise things just work :-)

v2:
 - use e(x)clusive answers for fish, tweak force string

v3:
 - wire the completions to the autotools build

v4:
 - use SPDX style copyright statements

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Makefile.am
meson.build
shell-completion/bash/insmod [new file with mode: 0644]
shell-completion/fish/insmod.fish [new file with mode: 0644]
shell-completion/zsh/_insmod [new file with mode: 0644]

index 1c6faca6a75fc5916de6b0f29ab49aa4e8c51d96..213a1fe6220ab0dddf18dc4c4834b8bfb57137fd 100644 (file)
@@ -125,17 +125,20 @@ noarch_pkgconfig_DATA = tools/kmod.pc
 
 bashcompletiondir=@bashcompletiondir@
 dist_bashcompletion_DATA = \
+       shell-completion/bash/insmod \
        shell-completion/bash/kmod \
        shell-completion/bash/lsmod \
        shell-completion/bash/rmmod
 
 fishcompletiondir=@fishcompletiondir@
 dist_fishcompletion_DATA = \
+       shell-completion/fish/insmod.fish \
        shell-completion/fish/lsmod.fish \
        shell-completion/fish/rmmod.fish
 
 zshcompletiondir=@zshcompletiondir@
 dist_zshcompletion_DATA = \
+       shell-completion/zsh/_insmod \
        shell-completion/zsh/_lsmod \
        shell-completion/zsh/_rmmod
 
index c85d984675cea5759bef8d4f9d1aafe237f08031..1c87fe0ed02ad8b8c11198bab87a7614eeccb426 100644 (file)
@@ -215,6 +215,7 @@ foreach tuple : _completiondirs
   endif
 
   _completions = [
+    'insmod',
     'lsmod',
     'rmmod',
   ]
diff --git a/shell-completion/bash/insmod b/shell-completion/bash/insmod
new file mode 100644 (file)
index 0000000..5741f33
--- /dev/null
@@ -0,0 +1,35 @@
+# insmod(8) completion                                      -*- shell-script -*-
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# SPDX-FileCopyrightText: 2024 Emil Velikov <emil.l.velikov@gmail.com>
+#
+# Formatted using:
+# shfmt --language-dialect bash --indent 4 --func-next-line
+
+_insmod()
+{
+    # long/short opt pairs
+    local -A opts=(
+        ['force']='f'
+        ['syslog']='s'
+        ['verbose']='v'
+        ['version']='V'
+        ['help']='h'
+    )
+
+    local cur="${COMP_WORDS[COMP_CWORD]}"
+
+    if [[ $cur == --* ]]; then
+        COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
+    elif [[ $cur == -* ]]; then
+        if (( ${#cur} != 2 )); then
+            COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
+        fi
+        COMPREPLY+=($(compgen -P '-' -W "${opts[*]}" -- "${cur:1}"))
+    else
+        # TODO: match only one file
+        _filedir '@(ko?(.gz|.xz|.zst))'
+        # TODO: add module parameter completion, based of modinfo
+    fi
+} &&
+    complete -F _insmod insmod
diff --git a/shell-completion/fish/insmod.fish b/shell-completion/fish/insmod.fish
new file mode 100644 (file)
index 0000000..606ab5c
--- /dev/null
@@ -0,0 +1,21 @@
+# insmod(8) completion                                      -*- shell-script -*-
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# SPDX-FileCopyrightText: 2024 Emil Velikov <emil.l.velikov@gmail.com>
+
+# globally disable file completions
+complete -c insmod -f
+
+complete -c insmod -s f -l force   -d "DANGEROUS: forces a module load, may cause data corruption and crash your machine"
+complete -c insmod -s s -l syslog  -d 'print to syslog, not stderr'
+complete -c insmod -s v -l verbose -d 'enables more messages'
+complete -c insmod -s V -l version -d 'show version'
+complete -c insmod -s h -l help    -d 'show this help'
+
+# provide an exclusive (x) list of required (r) answers (a), keeping (k) the
+# matches at the top.
+# TODO: match only one file
+# BUG: fish lists everything, even when only the given suffix is requested. Plus
+# we need to explicitly keep them sorted.
+complete -c insmod -x -ra "(__fish_complete_suffix .ko .ko.gz .ko.xz .ko.zst)" -k
+# TODO: add module parameter completion, based of modinfo
diff --git a/shell-completion/zsh/_insmod b/shell-completion/zsh/_insmod
new file mode 100644 (file)
index 0000000..de2c7ff
--- /dev/null
@@ -0,0 +1,14 @@
+#compdef insmod
+
+# insmod(8) completion                                      -*- shell-script -*-
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# SPDX-FileCopyrightText: 2024 Emil Velikov <emil.l.velikov@gmail.com>
+
+_arguments \
+    {-f,--force}'[DANGEROUS: forces a module load, may cause data corruption and crash your machine]' \
+    {-s,--syslog}'[print to syslog, not stderr]' \
+    {-v,--verbose}'[enables more messages]' \
+    {-V,--version}'[show version]' \
+    {-h,--help}'[show this help]' \
+    '1::module:_files -g "*.ko(|.gz|.xz|.zst)"'