]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
completions: apply suggestions from shellcheck
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 21 Jul 2024 09:21:49 +0000 (11:21 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 22 Jul 2024 09:16:45 +0000 (11:16 +0200)
readarray is used to create arrays. The one clear advantage is that we don't need to
override $IFS. Together with the change to not assign an unused variable, this
removes shellcheck warnings.

Nevertheless, shellcheck would still warn about the file because it doesn't
know about the variables that are in the part that is generated dynamically.

Also, move more content to the static resource file. The order of declarations
doesn't matter, so it's fine if the variables are defined below the functions.

Also, adjust the formatting in the bash resource to follow the usual style
with 'if something; then' on one line.

mkosi/completion.py
mkosi/resources/completion.bash
mkosi/resources/completion.zsh

index dec09c6909df205eb83ae2b912308dc5c9c15460..ed1df98c494f71499de70a8afda86a8486d15eac 100644 (file)
@@ -115,7 +115,8 @@ def finalize_completion_bash(options: list[CompletionItem], resources: Path) ->
     options_by_key = {o.short: o for o in options if o.short} | {o.long: o for o in options if o.long}
 
     with io.StringIO() as c:
-        c.write("# SPDX-License-Identifier: LGPL-2.1-or-later\n\n")
+        c.write(completion.read_text())
+
         c.write(to_bash_array("_mkosi_options", options_by_key.keys()))
         c.write("\n\n")
 
@@ -139,8 +140,6 @@ def finalize_completion_bash(options: list[CompletionItem], resources: Path) ->
         c.write(to_bash_array("_mkosi_verbs", [str(v) for v in config.Verb]))
         c.write("\n\n\n")
 
-        c.write(completion.read_text())
-
         return c.getvalue()
 
 
@@ -184,15 +183,12 @@ def finalize_completion_zsh(options: list[CompletionItem], resources: Path) -> s
     completion = resources / "completion.zsh"
 
     with io.StringIO() as c:
-        c.write("#compdef mkosi\n")
-        c.write("# SPDX-License-Identifier: LGPL-2.1-or-later\n\n")
+        c.write(completion.read_text())
+        c.write("\n")
 
         c.write(to_zsh_array("_mkosi_verbs", [str(v) for v in config.Verb]))
         c.write("\n\n")
 
-        c.write(completion.read_text())
-        c.write("\n")
-
         c.write("_arguments -s \\\n")
         c.write("    '(- *)'{-h,--help}'[Show this help]' \\\n")
         c.write("    '(- *)--version[Show package version]' \\\n")
index c665752a6fb13179461987378f80f6105f463b9f..82026d96292c2758c41ac37dbb282a527727702a 100644 (file)
@@ -1,3 +1,6 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# shellcheck shell=bash
+
 _mkosi_compgen_files() {
     compgen -f -- "$1"
 }
@@ -7,13 +10,13 @@ _mkosi_compgen_dirs() {
 }
 
 _mkosi_completion() {
-    local completing_program="$1"
+    # completing_program="$1"
     local completing_word="$2"
     local completing_word_preceding="$3"
 
     if [[ "$completing_word" =~ ^- ]]  # completing an option
     then
-        COMPREPLY=( $(compgen -W "${_mkosi_options[*]}" -- "${completing_word}") )
+        readarray -t COMPREPLY < <(compgen -W "${_mkosi_options[*]}" -- "${completing_word}")
 
     elif [[ "$completing_word_preceding" =~ ^- ]]  # the previous word was an option
     then
@@ -24,20 +27,20 @@ _mkosi_completion() {
 
         if [[ -n "${current_option_compgen}" ]]
         then
-            local IFS=$'\n'
-            COMPREPLY=( $("${current_option_compgen}" "${completing_word}") )
-            unset IFS
+            readarray -t COMPREPLY < <("${current_option_compgen}" "${completing_word}")
         fi
-        COMPREPLY+=( $(compgen -W "${current_option_choices}" -- "${completing_word}") )
+        readarray -t COMPREPLY -O "${#COMPREPLY[@]}" \
+                  < <(compgen -W "${current_option_choices}" -- "${completing_word}")
 
         if [[ "${current_option_nargs}" == "?" ]]
         then
-            COMPREPLY+=( $(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}") )
+            readarray -t COMPREPLY -O "${#COMPREPLY[@]}" \
+                      < <(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}")
         fi
     else
         # the preceding word wasn't an option, so we are doing position
         # arguments now and all of them are verbs
-        COMPREPLY=( $(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}") )
+        readarray -t COMPREPLY < <(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}")
     fi
 }
 
index e9cd3b58e5f74cf4718929b364d9ef2310b3f0f0..b1687d5aed22fe4942f607d1a2a014b836f59ddf 100644 (file)
@@ -1,3 +1,7 @@
+#compdef mkosi
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# shellcheck shell=zsh
+
 _mkosi_verb(){
     if (( CURRENT == 1 )); then
         _describe -t commands 'mkosi verb' _mkosi_verbs