Skip to content

Commit

Permalink
Merge pull request #27 from OSInside/completion
Browse files Browse the repository at this point in the history
Add bash completion
  • Loading branch information
schaefi authored Nov 13, 2023
2 parents cf7746b + 3ccc4b3 commit e02dcbb
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
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
165 changes: 165 additions & 0 deletions completion/flake-ctl
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/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
}

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() {
word_list=$*
readarray -t COMPREPLY < <(compgen -W "$word_list" -- "${cur}")
}

__flake_ctl_main() {
local cur cword
_init_completion || return
_get_comp_words_by_ref cur prev

if [ "${cword}" -gt 1 ];then
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
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

0 comments on commit e02dcbb

Please sign in to comment.