From 2637521788fb264b76057a8f2ba438a1b0658c22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20Sch=C3=A4fer?= Date: Fri, 10 Nov 2023 16:40:40 +0100 Subject: [PATCH 1/2] Add bash completion Add completion for flake-ctl. This Fixes #18 --- Makefile | 4 + completion/flake-ctl | 177 +++++++++++++++++++++++++++++++++++++++ package/flake-pilot.spec | 1 + 3 files changed, 182 insertions(+) create mode 100755 completion/flake-ctl diff --git a/Makefile b/Makefile index 970fa69..e3104a4 100644 --- a/Makefile +++ b/Makefile @@ -118,6 +118,10 @@ install: $(DESTDIR)$(TEMPLATEDIR)/firecracker.json install -m 644 doc/*.8 ${DESTDIR}/usr/share/man/man8 install -m 755 utils/* $(DESTDIR)$(SBINDIR) + # completion + install -d -m 755 ${buildroot}usr/share/bash-completion/completions + install -m 755 completion/flake-ctl \ + ${buildroot}usr/share/bash-completion/completions/flake-ctl uninstall: rm -f $(DESTDIR)$(BINDIR)/flake-ctl diff --git a/completion/flake-ctl b/completion/flake-ctl new file mode 100755 index 0000000..ddf6934 --- /dev/null +++ b/completion/flake-ctl @@ -0,0 +1,177 @@ +#!/bin/bash +# shellcheck shell=bash + +__flake_ctl_firecracker_pull() { + __comp_reply_unused " + --force + --initrd + --kernel + --kis-image + --name + --rootfs + --help + " +} + +__flake_ctl_firecracker_help() { + : +} + +__flake_ctl_firecracker_register() { + __comp_reply_unused " + --app + --include-tar + --include-path + --no-net + --overlay-size + --resume + --run-as + --target + --vm + --help + " +} + +__flake_ctl_firecracker_remove() { + __comp_reply_unused "--app --vm --help" +} + +__flake_ctl_help() { + __comp_reply "" +} + +__flake_ctl_list() { + __comp_reply_unused "--help" +} + +__flake_ctl_podman_pull() { + __comp_reply_unused "--uri --help" +} + +__flake_ctl_podman_help() { + __comp_reply "" +} + +__flake_ctl_podman_register() { + __comp_reply_unused " + --app + --attach + --base + --container + --include-tar + --include-path + --info + --layer + --opt + --resume + --run-as + --target + --help + " +} + +__flake_ctl_podman_remove() { + __comp_reply_unused "--app --container --help" +} + +__flake_ctl_podman_load() { + __comp_reply_unused "--oci --help" +} + +__flake_ctl_complete_command() { + local command="$1" + local completion_func="__flake_ctl_${command//-/_}" + $completion_func + return 0 +} + +__comp_option() { + for item in ${COMP_LINE}; do + if [[ $item =~ ^-.* ]];then + echo "${item}" + return + fi + done +} + +function __comp_reply_unused { + local available + local used + for option in $1;do + used=0 + for item in ${COMP_LINE}; do + if [ "${item}" = "${option}" ];then + used=1; break + fi + done + if [ "${used}" = 0 ];then + available="${available} ${option}" + fi + done + __comp_reply "${available}" +} + +__comp_reply() { + local cur word_list + word_list=$* + cur=${COMP_WORDS[COMP_CWORD]} + COMPREPLY=( $(compgen -W "$word_list" -- "$cur") ) +} + +__flake_ctl_main() { + local cur prev cword cmd + _init_completion || return + _get_comp_words_by_ref cur prev + + if [ "${cword}" -gt 1 ];then + cmd=$(__comp_option) + for comp in ${COMP_WORDS[1]}_${COMP_WORDS[2]};do + case "$comp" in + podman_load| \ + podman_help| \ + podman_pull| \ + podman_register| \ + podman_remove| \ + firecracker_remove| \ + firecracker_help| \ + firecracker_pull| \ + firecracker_register) + __flake_ctl_complete_command $comp && return 0 + ;; + firecracker_*) + command="firecracker" && __comp_reply " + help pull register remove + " && return 0 + ;; + podman_*) + command="podman" && __comp_reply " + help load pull register remove + " && return 0 + ;; + help_*) + command="help" && __comp_reply "" && return 0 + ;; + list_*) + command="list" && __comp_reply "" && return 0 + ;; + esac + done + fi + + if (( $COMP_CWORD == 1 )); then + __comp_reply " + --help + --version + firecracker + help + list + podman + " + return 0 + fi + + # Back to default file completion + COMPREPLY=() +} + +complete -o bashdefault -o default -F __flake_ctl_main flake-ctl diff --git a/package/flake-pilot.spec b/package/flake-pilot.spec index 5dd5141..8c67f39 100644 --- a/package/flake-pilot.spec +++ b/package/flake-pilot.spec @@ -181,6 +181,7 @@ install -m 644 flakes.yml %{buildroot}/etc/flakes.yml %dir /etc/flakes %config /etc/flakes.yml /usr/bin/flake-ctl +%{_usr}/share/bash-completion/completions/flake-ctl %doc /usr/share/man/man8/flake-ctl.8.gz %doc /usr/share/man/man8/flake-ctl-list.8.gz From 3ccc4b357a691b8841097051d5aa471b52caf9a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20Sch=C3=A4fer?= Date: Fri, 10 Nov 2023 19:51:19 +0100 Subject: [PATCH 2/2] make shellcheck happy --- completion/flake-ctl | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/completion/flake-ctl b/completion/flake-ctl index ddf6934..c6fed29 100755 --- a/completion/flake-ctl +++ b/completion/flake-ctl @@ -85,15 +85,6 @@ __flake_ctl_complete_command() { return 0 } -__comp_option() { - for item in ${COMP_LINE}; do - if [[ $item =~ ^-.* ]];then - echo "${item}" - return - fi - done -} - function __comp_reply_unused { local available local used @@ -112,19 +103,16 @@ function __comp_reply_unused { } __comp_reply() { - local cur word_list word_list=$* - cur=${COMP_WORDS[COMP_CWORD]} - COMPREPLY=( $(compgen -W "$word_list" -- "$cur") ) + readarray -t COMPREPLY < <(compgen -W "$word_list" -- "${cur}") } __flake_ctl_main() { - local cur prev cword cmd + local cur cword _init_completion || return _get_comp_words_by_ref cur prev if [ "${cword}" -gt 1 ];then - cmd=$(__comp_option) for comp in ${COMP_WORDS[1]}_${COMP_WORDS[2]};do case "$comp" in podman_load| \ @@ -136,7 +124,7 @@ __flake_ctl_main() { firecracker_help| \ firecracker_pull| \ firecracker_register) - __flake_ctl_complete_command $comp && return 0 + __flake_ctl_complete_command "${comp}" && return 0 ;; firecracker_*) command="firecracker" && __comp_reply " @@ -158,7 +146,7 @@ __flake_ctl_main() { done fi - if (( $COMP_CWORD == 1 )); then + if (( COMP_CWORD == 1 )); then __comp_reply " --help --version