8e5b17cf13
Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
508 lines
10 KiB
Bash
508 lines
10 KiB
Bash
#!/bin/bash
|
|
#
|
|
# bash completion file for core oci-runtime-tool commands
|
|
#
|
|
# This script provides completion of:
|
|
# - commands and their options
|
|
# - filepaths
|
|
#
|
|
# To enable the completions either:
|
|
# - place this file in /usr/share/bash-completion/completions
|
|
# or
|
|
# - copy this file to e.g. ~/.oci-runtime-tool-completion.sh and add the line
|
|
# below to your .bashrc after bash completion features are loaded
|
|
# . ~/.oci-runtime-tool-completion.sh
|
|
#
|
|
# Configuration:
|
|
#
|
|
|
|
|
|
# Note for developers:
|
|
# Please arrange options sorted alphabetically by long name with the short
|
|
# options immediately following their corresponding long form.
|
|
# This order should be applied to lists, alternatives and code blocks.
|
|
|
|
__oci-runtime-tool_previous_extglob_setting=$(shopt -p extglob)
|
|
shopt -s extglob
|
|
|
|
__oci-runtime-tool_pos_first_nonflag() {
|
|
local argument_flags=$1
|
|
|
|
local counter=$((${subcommand_pos:-${command_pos}} + 1))
|
|
while [ $counter -le $cword ]; do
|
|
if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
|
|
(( counter++ ))
|
|
else
|
|
case "${words[$counter]}" in
|
|
-*)
|
|
;;
|
|
*)
|
|
break
|
|
;;
|
|
esac
|
|
fi
|
|
(( counter++ ))
|
|
done
|
|
|
|
echo $counter
|
|
}
|
|
|
|
# Transforms a multiline list of strings into a single line string
|
|
# with the words separated by "|".
|
|
# This is used to prepare arguments to __oci-runtime-tool_pos_first_nonflag().
|
|
__oci-runtime-tool_to_alternatives() {
|
|
local parts=( $1 )
|
|
local IFS='|'
|
|
echo "${parts[*]}"
|
|
}
|
|
|
|
# Transforms a multiline list of options into an extglob pattern
|
|
# suitable for use in case statements.
|
|
__oci-runtime-tool_to_extglob() {
|
|
local extglob=$( __oci-runtime-tool_to_alternatives "$1" )
|
|
echo "@($extglob)"
|
|
}
|
|
|
|
# Subcommand processing.
|
|
# Locates the first occurrence of any of the subcommands contained in the
|
|
# first argument. In case of a match, calls the corresponding completion
|
|
# function and returns 0.
|
|
# If no match is found, 1 is returned. The calling function can then
|
|
# continue processing its completion.
|
|
#
|
|
# TODO if the preceding command has options that accept arguments and an
|
|
# argument is equal ot one of the subcommands, this is falsely detected as
|
|
# a match.
|
|
__oci-runtime-tool_subcommands() {
|
|
local subcommands="$1"
|
|
|
|
local counter=$(($command_pos + 1))
|
|
while [ $counter -lt $cword ]; do
|
|
case "${words[$counter]}" in
|
|
$(__oci-runtime-tool_to_extglob "$subcommands") )
|
|
subcommand_pos=$counter
|
|
local subcommand=${words[$counter]}
|
|
local completions_func=_oci-runtime-tool_${command}_${subcommand}
|
|
declare -F $completions_func >/dev/null && $completions_func
|
|
return 0
|
|
;;
|
|
esac
|
|
(( counter++ ))
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# List groups
|
|
__oci-runtime-tool_groups() {
|
|
cat /etc/group | cut -d: -f 1
|
|
}
|
|
|
|
# List installed hooks
|
|
__oci-runtime-tool_hooks() {
|
|
ls /usr/libexec/oci/hooks.d/*
|
|
}
|
|
|
|
# suppress trailing whitespace
|
|
__oci-runtime-tool_nospace() {
|
|
# compopt is not available in ancient bash versions
|
|
type compopt &>/dev/null && compopt -o nospace
|
|
}
|
|
|
|
__oci-runtime-tool_complete_log_level() {
|
|
COMPREPLY=( $( compgen -W "
|
|
debug
|
|
error
|
|
fatal
|
|
info
|
|
panic
|
|
warn
|
|
" -- "$cur" ) )
|
|
}
|
|
|
|
__oci-runtime-tool_complete_propagations() {
|
|
COMPREPLY=( $( compgen -W "
|
|
private
|
|
rprivate
|
|
rshared
|
|
rslave
|
|
shared
|
|
slave
|
|
" -- "$cur" ) )
|
|
}
|
|
|
|
# a selection of the available arches that is most likely of interest in the
|
|
# context of oci-runtime-tool containers.
|
|
__oci-runtime-tool_complete_arches() {
|
|
COMPREPLY=( $( compgen -W "
|
|
386
|
|
amd64
|
|
arm
|
|
" -- "$cur" ) )
|
|
}
|
|
|
|
# a selection of the available arches that is most likely of interest in the
|
|
# context of oci-runtime-tool containers.
|
|
__oci-runtime-tool_complete_seccomp_arches() {
|
|
COMPREPLY=( $( compgen -W "
|
|
x86
|
|
amd64
|
|
x32
|
|
arm
|
|
arm64
|
|
mips
|
|
mips64
|
|
mips64n32
|
|
mipsel
|
|
mipsel64
|
|
mipsel64n32
|
|
ppc
|
|
ppc64
|
|
ppc64le
|
|
s390
|
|
s390x
|
|
" -- "$cur" ) )
|
|
}
|
|
|
|
# a selection of the available actions that is most likely of interest in the
|
|
# context of oci-runtime-tool containers.
|
|
__oci-runtime-tool_complete_seccomp_actions() {
|
|
COMPREPLY=( $( compgen -W "
|
|
SCMP_ACT_ALLOW
|
|
SCMP_ACT_ERRNO
|
|
SCMP_ACT_KILL
|
|
SCMP_ACT_TRACE
|
|
SCMP_ACT_TRAP
|
|
" -- "$cur" ) )
|
|
}
|
|
__oci-runtime-tool_complete_capabilities() {
|
|
# The list of capabilities is defined in types.go, ALL was added manually.
|
|
COMPREPLY=( $( compgen -W "
|
|
ALL
|
|
AUDIT_CONTROL
|
|
AUDIT_WRITE
|
|
AUDIT_READ
|
|
BLOCK_SUSPEND
|
|
CHOWN
|
|
DAC_OVERRIDE
|
|
DAC_READ_SEARCH
|
|
FOWNER
|
|
FSETID
|
|
IPC_LOCK
|
|
IPC_OWNER
|
|
KILL
|
|
LEASE
|
|
LINUX_IMMUTABLE
|
|
MAC_ADMIN
|
|
MAC_OVERRIDE
|
|
MKNOD
|
|
NET_ADMIN
|
|
NET_BIND_SERVICE
|
|
NET_BROADCAST
|
|
NET_RAW
|
|
SETFCAP
|
|
SETGID
|
|
SETPCAP
|
|
SETUID
|
|
SYS_ADMIN
|
|
SYS_BOOT
|
|
SYS_CHROOT
|
|
SYSLOG
|
|
SYS_MODULE
|
|
SYS_NICE
|
|
SYS_PACCT
|
|
SYS_PTRACE
|
|
SYS_RAWIO
|
|
SYS_RESOURCE
|
|
SYS_TIME
|
|
SYS_TTY_CONFIG
|
|
WAKE_ALARM
|
|
" -- "$cur" ) )
|
|
}
|
|
|
|
|
|
# global options that may appear after the oci-runtime-tool command
|
|
_oci-runtime-tool_oci-runtime-tool() {
|
|
local options_with_args="
|
|
--log-level
|
|
"
|
|
|
|
local boolean_options="
|
|
$global_boolean_options
|
|
--help
|
|
--host-specific
|
|
--version -v
|
|
"
|
|
|
|
local all_options="$options_with_args $boolean_options"
|
|
|
|
case "$prev" in
|
|
$(__oci-runtime-tool_to_extglob "$global_options_with_args") )
|
|
return
|
|
;;
|
|
|
|
--log-level)
|
|
__oci-runtime-tool_complete_log_level
|
|
return
|
|
;;
|
|
|
|
esac
|
|
|
|
case "$cur" in
|
|
-*)
|
|
COMPREPLY=( $( compgen -W "$all_options $global_options_with_args" -- "$cur" ) )
|
|
;;
|
|
*)
|
|
local counter=$( __oci-runtime-tool_pos_first_nonflag $(__oci-runtime-tool_to_extglob "$global_options_with_args") )
|
|
if [ $cword -eq $counter ]; then
|
|
COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
|
|
fi
|
|
;;
|
|
esac
|
|
}
|
|
|
|
_oci-runtime-tool_validate() {
|
|
case "$prev" in
|
|
--path)
|
|
case "$cur" in
|
|
*:*)
|
|
# TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
|
|
;;
|
|
'')
|
|
COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
|
|
__oci-runtime-tool_nospace
|
|
;;
|
|
/*)
|
|
_filedir
|
|
__oci-runtime-tool_nospace
|
|
;;
|
|
esac
|
|
return
|
|
;;
|
|
esac
|
|
|
|
case "$cur" in
|
|
-*)
|
|
COMPREPLY=( $( compgen -W "--path --help" -- "$cur" ) )
|
|
;;
|
|
esac
|
|
|
|
}
|
|
|
|
_oci-runtime-tool_help() {
|
|
local counter=$(__oci-runtime-tool_pos_first_nonflag)
|
|
if [ $cword -eq $counter ]; then
|
|
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
|
|
fi
|
|
}
|
|
|
|
_oci-runtime-tool_generate() {
|
|
local options_with_args="
|
|
--arch
|
|
--apparmor
|
|
--args
|
|
--bind
|
|
--cap-add
|
|
--cap-drop
|
|
--cgroup
|
|
--cgroup-path
|
|
--cwd
|
|
--disable-oom-kill
|
|
--env
|
|
--env-file
|
|
--gid
|
|
--gidmappings
|
|
--groups
|
|
--hostname
|
|
--help
|
|
--ipc
|
|
--label
|
|
--linux-network-classid
|
|
--linux-network-priorities
|
|
--linux-pids-limit
|
|
--masked-paths
|
|
--mount
|
|
--mount-cgroups
|
|
--mount-label
|
|
--network
|
|
--os
|
|
--output
|
|
--pid
|
|
--poststart
|
|
--poststop
|
|
--prestart
|
|
--readonly-paths
|
|
--rootfs-path
|
|
--rootfs-propagation
|
|
--rlimits-add
|
|
--rlimits-remove
|
|
--rlimits-remove-all
|
|
--seccomp-allow
|
|
--seccomp-arch
|
|
--seccomp-default
|
|
--seccomp-default-force
|
|
--seccomp-errno
|
|
--seccomp-kill
|
|
--seccomp-only
|
|
--seccomp-remove
|
|
--seccomp-remove-all
|
|
--seccomp-trace
|
|
--seccomp-trap
|
|
--seccomp-syscalls
|
|
--selinux-label
|
|
--sysctl
|
|
--tmplate
|
|
--tmpfs
|
|
--uid
|
|
--uidmappings
|
|
--user
|
|
--uts
|
|
"
|
|
|
|
local boolean_options="
|
|
--no-new-privileges
|
|
--privileged
|
|
--rootfs-readonly
|
|
--tty
|
|
"
|
|
|
|
local all_options="$options_with_args $boolean_options"
|
|
|
|
case "$prev" in
|
|
--seccomp-arch)
|
|
__oci-runtime-tool_complete_seccomp_arches
|
|
return
|
|
;;
|
|
|
|
--arch)
|
|
__oci-runtime-tool_complete_arches
|
|
return
|
|
;;
|
|
|
|
--seccomp-default)
|
|
__oci-runtime-tool_complete_seccomp_actions
|
|
return
|
|
;;
|
|
|
|
--root-propagation)
|
|
__oci-runtime-tool_complete_propagations
|
|
return
|
|
;;
|
|
|
|
--cap-add|--cap-drop)
|
|
__oci-runtime-tool_complete_capabilities
|
|
return
|
|
;;
|
|
|
|
--mount-cgroups)
|
|
COMPREPLY=( $( compgen -W "ro" -- "$cur" ) )
|
|
return
|
|
;;
|
|
|
|
--os)
|
|
COMPREPLY=( $( compgen -W "linux windows" -- "$cur" ) )
|
|
return
|
|
;;
|
|
|
|
--groups)
|
|
COMPREPLY=( $( compgen -W "$( __oci-runtime-tool_groups )" -- "$cur" ) )
|
|
__oci-runtime-tool_nospace
|
|
return
|
|
;;
|
|
|
|
--poststart|--poststop|--prestart)
|
|
COMPREPLY=( $( compgen -W "$( __oci-runtime-tool_hooks )" -- "$cur" ) )
|
|
__oci-runtime-tool_nospace
|
|
return
|
|
;;
|
|
|
|
--rootfs|--tmpfs|--bind|--cwd)
|
|
case "$cur" in
|
|
*:*)
|
|
# TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
|
|
;;
|
|
'')
|
|
COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
|
|
__oci-runtime-tool_nospace
|
|
;;
|
|
*)
|
|
_filedir
|
|
__oci-runtime-tool_nospace
|
|
;;
|
|
esac
|
|
return
|
|
;;
|
|
--env|-e)
|
|
COMPREPLY=( $( compgen -e -- "$cur" ) )
|
|
__oci-runtime-tool_nospace
|
|
return
|
|
;;
|
|
--env-file)
|
|
_filedir
|
|
__oci-runtime-tool_nospace
|
|
return
|
|
;;
|
|
$(__oci-runtime-tool_to_extglob "$options_with_args") )
|
|
return
|
|
;;
|
|
esac
|
|
|
|
case "$cur" in
|
|
-*)
|
|
COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
|
|
;;
|
|
esac
|
|
}
|
|
|
|
_oci-runtime-tool() {
|
|
local previous_extglob_setting=$(shopt -p extglob)
|
|
shopt -s extglob
|
|
|
|
local commands=(
|
|
validate
|
|
generate
|
|
)
|
|
|
|
# These options are valid as global options for all client commands
|
|
# and valid as command options for `oci-runtime-tool daemon`
|
|
local global_boolean_options="
|
|
--help -h
|
|
--version -v
|
|
"
|
|
|
|
local global_options_with_args="
|
|
--help
|
|
"
|
|
|
|
COMPREPLY=()
|
|
local cur prev words cword
|
|
_get_comp_words_by_ref -n : cur prev words cword
|
|
|
|
local command='oci-runtime-tool' command_pos=0 subcommand_pos
|
|
local counter=1
|
|
while [ $counter -lt $cword ]; do
|
|
case "${words[$counter]}" in
|
|
-*)
|
|
;;
|
|
=)
|
|
(( counter++ ))
|
|
;;
|
|
*)
|
|
command="${words[$counter]}"
|
|
command_pos=$counter
|
|
break
|
|
;;
|
|
esac
|
|
(( counter++ ))
|
|
done
|
|
|
|
local completions_func=_oci-runtime-tool_${command}
|
|
declare -F $completions_func >/dev/null && $completions_func
|
|
|
|
eval "$previous_extglob_setting"
|
|
return 0
|
|
}
|
|
|
|
eval "$__oci-runtime-tool_previous_extglob_setting"
|
|
unset __oci-runtime-tool_previous_extglob_setting
|
|
|
|
complete -F _oci-runtime-tool oci-runtime-tool
|