Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bash completion #27

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
177 changes: 177 additions & 0 deletions completion/flake-ctl
Original file line number Diff line number Diff line change
@@ -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") )
Fixed Show fixed Hide fixed
}

__flake_ctl_main() {
local cur prev cword cmd
Fixed Show fixed Hide fixed
_init_completion || return
_get_comp_words_by_ref cur prev

if [ "${cword}" -gt 1 ];then
cmd=$(__comp_option)
Fixed Show fixed Hide fixed
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
Fixed Show fixed Hide fixed
;;
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
Fixed Show fixed Hide fixed
__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
1 change: 1 addition & 0 deletions package/flake-pilot.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading