#!/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() { cut -d: -f 1 /etc/group } # 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 arm64 mips mips64 mips64le mipsle ppc64 ppc64le s390x " -- "$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 parisc parisc64 " -- "$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=" --help -h --host-specific --version -v " local all_options="$options_with_args $boolean_options" case "$prev" in --log-level) __oci-runtime-tool_complete_log_level return ;; esac case "$cur" in -*) COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) ) ;; *) local counter=$( __oci-runtime-tool_pos_first_nonflag $(__oci-runtime-tool_to_extglob "$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 -h" -- "$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=" --apparmor --arch --args --bind --cap-add --cap-drop --cgroups-path --cwd --device-add --device-remove --env --env-file --gid --gidmappings --groups --hostname --label --linux-cpu-shares --linux-cpu-period --linux-cpu-quota --linux-cpus --linux-mem-kernel-limit --linux-mem-kernel-tcp --linux-mem-limit --linux-mem-reservation --linux-mem-swap --linux-mem-swappiness --linux-mems --linux-namespace-add --linux-namespace-remove --linux-network-classid --linux-network-priorities --linux-pids-limit --linux-realtime-period --linux-realtime-runtime --masked-paths --mount-cgroups --mount-label --oom-score-adj --os --output --poststart --poststop --prestart --readonly-paths --rootfs-path --rootfs-propagation --rlimits-add --rlimits-remove --seccomp-allow --seccomp-arch --seccomp-default --seccomp-default-force --seccomp-errno --seccomp-kill --seccomp-remove --seccomp-trace --seccomp-trap --selinux-label --sysctl --template --tmpfs --uid --uidmappings " local boolean_options=" --device-remove-all --disable-oom-kill --help -h --linux-namespace-remove-all --no-new-privileges --privileged --rlimits-remove-all --rootfs-readonly --seccomp-only --seccomp-remove-all --tty " local all_options="$options_with_args $boolean_options" case "$prev" in --arch) __oci-runtime-tool_complete_arches return ;; --cap-add|--cap-drop) __oci-runtime-tool_complete_capabilities return ;; --env|-e) COMPREPLY=( $( compgen -e -- "$cur" ) ) __oci-runtime-tool_nospace return ;; --env-file) _filedir __oci-runtime-tool_nospace return ;; --gid) _gids return ;; --groups) COMPREPLY=( $( compgen -W "$( __oci-runtime-tool_groups )" -- "$cur" ) ) __oci-runtime-tool_nospace return ;; --mount-cgroups) COMPREPLY=( $( compgen -W "no ro rw" -- "$cur" ) ) return ;; --os) COMPREPLY=( $( compgen -W "linux windows" -- "$cur" ) ) return ;; --poststart|--poststop|--prestart) COMPREPLY=( $( compgen -W "$( __oci-runtime-tool_hooks )" -- "$cur" ) ) __oci-runtime-tool_nospace return ;; --root-propagation) __oci-runtime-tool_complete_propagations 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 ;; --seccomp-arch) __oci-runtime-tool_complete_seccomp_arches return ;; --seccomp-default) __oci-runtime-tool_complete_seccomp_actions return ;; --uid) _uids 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 ) 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