mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-27 15:57:09 +00:00
subsystem: cli
Add kata command line bash completion Fixes #110 Signed-off-by: Ricardo Aravena <raravena@branch.io>
This commit is contained in:
parent
ca9f7abba9
commit
706904524a
8
Makefile
8
Makefile
@ -78,6 +78,9 @@ GENERATED_FILES += $(COLLECT_SCRIPT)
|
||||
SCRIPTS += $(COLLECT_SCRIPT)
|
||||
SCRIPTS_DIR := $(BINDIR)
|
||||
|
||||
BASH_COMPLETIONS := data/completions/bash/kata-runtime
|
||||
BASH_COMPLETIONSDIR := $(SHAREDIR)/bash-completion/completions
|
||||
|
||||
PKGDATADIR := $(SHAREDIR)/$(PROJECT_DIR)
|
||||
PKGLIBDIR := $(LOCALSTATEDIR)/lib/$(PROJECT_DIR)
|
||||
PKGRUNDIR := $(LOCALSTATEDIR)/run/$(PROJECT_DIR)
|
||||
@ -389,13 +392,16 @@ check-go-static:
|
||||
coverage:
|
||||
$(QUIET_TEST).ci/go-test.sh html-coverage
|
||||
|
||||
install: default runtime install-scripts
|
||||
install: default runtime install-scripts install-completions
|
||||
$(QUIET_INST)install -D $(TARGET) $(DESTTARGET)
|
||||
$(QUIET_INST)install -D $(CONFIG) $(DESTCONFIG)
|
||||
|
||||
install-scripts: $(SCRIPTS)
|
||||
$(foreach f,$(SCRIPTS),$(call INSTALL_EXEC,$f,$(SCRIPTS_DIR)))
|
||||
|
||||
install-completions:
|
||||
$(QUIET_INST)install --mode 0644 -D $(BASH_COMPLETIONS) $(BASH_COMPLETIONSDIR)
|
||||
|
||||
clean:
|
||||
$(QUIET_CLEAN)rm -f $(TARGET) $(CONFIG) $(GENERATED_GO_FILES) $(GENERATED_FILES) $(COLLECT_SCRIPT)
|
||||
|
||||
|
138
data/completions/bash/kata-runtime
Normal file
138
data/completions/bash/kata-runtime
Normal file
@ -0,0 +1,138 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright (c) 2018 Intel Corporation
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
#---------------------------------------------------------------------
|
||||
# Description: Bash tab completion script.
|
||||
#---------------------------------------------------------------------
|
||||
|
||||
_kataruntime='kata-runtime'
|
||||
|
||||
# Return a list of sub-commands
|
||||
_kata_get_subcmds()
|
||||
{
|
||||
"$_kataruntime" --generate-bash-completion
|
||||
}
|
||||
|
||||
# Return a list of options for the specified sub-command
|
||||
#
|
||||
# Limitation: Note that this only supports long-options.
|
||||
_kata_get_subcmd_options()
|
||||
{
|
||||
local subcmd="$1"
|
||||
|
||||
"$_kataruntime" "$subcmd" --help |\
|
||||
egrep -- "^ *--[^ ]*[ ][^ ]*" |\
|
||||
awk '{print $1}' |\
|
||||
tr -d \, |\
|
||||
sort
|
||||
}
|
||||
|
||||
# Return a list of global options
|
||||
_kata_get_global_options()
|
||||
{
|
||||
_kata_get_subcmd_options ""
|
||||
}
|
||||
|
||||
# Return name of subcmd already seen, or ""
|
||||
_kata_subcmd_seen()
|
||||
{
|
||||
local subcmds=$(_kata_get_subcmds)
|
||||
local cmd
|
||||
|
||||
for cmd in $subcmds; do
|
||||
local word
|
||||
for word in ${COMP_WORDS[@]}; do
|
||||
[ "$cmd" = "$word" ] && echo "$cmd"
|
||||
done
|
||||
done
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Return 0 if the specified sub-command requires the name of an
|
||||
# *existing* container, else 1.
|
||||
_kata_subcmd_needs_existing_container()
|
||||
{
|
||||
local subcmd="$1"
|
||||
local cmd
|
||||
|
||||
for cmd in \
|
||||
'kata-check' \
|
||||
'kata-env' \
|
||||
'create' \
|
||||
'help' \
|
||||
'list' \
|
||||
'version'; do
|
||||
[ "$cmd" = "$subcmd" ] && return 1
|
||||
done
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Returns a list of container names
|
||||
_kata_get_containers()
|
||||
{
|
||||
# Commands that manipulate containers need root privileges.
|
||||
# If the user isn't running as root, don't attempt to obtain a list
|
||||
# as it will result in an error.
|
||||
[ $(id -u) -eq 0 ] || return
|
||||
|
||||
"$_kataruntime" list --quiet
|
||||
}
|
||||
|
||||
_kata_bash_autocomplete() {
|
||||
COMPREPLY=()
|
||||
|
||||
local opts opt
|
||||
|
||||
local cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
|
||||
for opt in \
|
||||
'-h' '--help' 'help' \
|
||||
'-v' '--version' 'version';
|
||||
do
|
||||
# No further completions possible for these commands
|
||||
[ "$cur" = "$opt" ] && return 0
|
||||
done
|
||||
|
||||
local subcmd_seen=$(_kata_subcmd_seen)
|
||||
|
||||
if [ -n "$subcmd_seen" ]; then
|
||||
_kata_subcmd_needs_existing_container "$subcmd_seen"
|
||||
local container_cmd=$?
|
||||
|
||||
if [ -n "$cur" ]; then
|
||||
# Complete with local options and maybe container names
|
||||
opts=$(_kata_get_subcmd_options "$subcmd_seen")
|
||||
[ $container_cmd -eq 0 ] && opts="$opts $(_kata_get_containers)"
|
||||
elif [[ "${cur}" == -* ]]; then
|
||||
# Complete with local options
|
||||
opts=$(_kata_get_subcmd_options "$subcmd_seen")
|
||||
else
|
||||
# Potentially complete with container names
|
||||
[ $container_cmd -eq 0 ] && opts="$(_kata_get_containers)"
|
||||
fi
|
||||
else
|
||||
if [ -n "$cur" ]; then
|
||||
# Complete with global options and subcmds
|
||||
opts="$opts $(_kata_get_global_options)"
|
||||
opts="$opts $(_kata_get_subcmds)"
|
||||
elif [[ "${cur}" == -* ]]; then
|
||||
# Complete with global options
|
||||
opts=$(_kata_get_global_options)
|
||||
else
|
||||
# Complete with subcmds
|
||||
opts=$(_kata_get_subcmds)
|
||||
fi
|
||||
fi
|
||||
|
||||
[ -n "$opts" ] && COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
complete -F _kata_bash_autocomplete "$_kataruntime"
|
Loading…
Reference in New Issue
Block a user