github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/data/completions/bash/kata-runtime (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2018 Intel Corporation
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  
     8  #---------------------------------------------------------------------
     9  # Description: Bash tab completion script.
    10  #---------------------------------------------------------------------
    11  
    12  _kataruntime='kata-runtime'
    13  
    14  # Return a list of sub-commands
    15  _kata_get_subcmds()
    16  {
    17      "$_kataruntime" --generate-bash-completion
    18  }
    19  
    20  # Return a list of options for the specified sub-command
    21  #
    22  # Limitation: Note that this only supports long-options.
    23  _kata_get_subcmd_options()
    24  {
    25      local subcmd="$1"
    26  
    27      "$_kataruntime" "$subcmd" --help |\
    28          egrep -- "^ *--[^ ]*[ ][^ ]*" |\
    29          awk '{print $1}' |\
    30          tr -d \, |\
    31          sort
    32  }
    33  
    34  # Return a list of global options
    35  _kata_get_global_options()
    36  {
    37      _kata_get_subcmd_options ""
    38  }
    39  
    40  # Return name of subcmd already seen, or ""
    41  _kata_subcmd_seen()
    42  {
    43      local subcmds=$(_kata_get_subcmds)
    44      local cmd
    45  
    46      for cmd in $subcmds; do
    47          local word
    48          for word in ${COMP_WORDS[@]}; do
    49              [ "$cmd" = "$word" ] && echo "$cmd"
    50          done
    51      done
    52  
    53      echo ""
    54  }
    55  
    56  # Return 0 if the specified sub-command requires the name of an
    57  # *existing* container, else 1.
    58  _kata_subcmd_needs_existing_container()
    59  {
    60      local subcmd="$1"
    61      local cmd
    62  
    63      for cmd in \
    64  	    'kata-check' \
    65  	    'kata-env' \
    66  	    'create' \
    67  	    'help' \
    68  	    'list' \
    69  	    'version'; do
    70          [ "$cmd" = "$subcmd" ] && return 1
    71      done
    72  
    73      return 0
    74  }
    75  
    76  # Returns a list of container names
    77  _kata_get_containers()
    78  {
    79      # Commands that manipulate containers need root privileges.
    80      # If the user isn't running as root, don't attempt to obtain a list
    81      # as it will result in an error.
    82      [ $(id -u) -eq 0 ] || return
    83  
    84      "$_kataruntime" list --quiet
    85  }
    86  
    87  _kata_bash_autocomplete() {
    88      COMPREPLY=()
    89  
    90      local opts opt
    91  
    92      local cur="${COMP_WORDS[COMP_CWORD]}"
    93  
    94      for opt in \
    95          '-h' '--help' 'help' \
    96          '-v' '--version' 'version';
    97      do
    98          # No further completions possible for these commands
    99          [ "$cur" = "$opt" ] && return 0
   100      done
   101  
   102      local subcmd_seen=$(_kata_subcmd_seen)
   103  
   104      if [ -n "$subcmd_seen" ]; then
   105  	    _kata_subcmd_needs_existing_container "$subcmd_seen"
   106  	    local container_cmd=$?
   107  
   108              if [ -n "$cur" ]; then
   109                      # Complete with local options and maybe container names
   110                      opts=$(_kata_get_subcmd_options "$subcmd_seen")
   111                      [ $container_cmd -eq 0 ] && opts="$opts $(_kata_get_containers)"
   112              elif [[ "${cur}" == -* ]]; then
   113                      # Complete with local options
   114                      opts=$(_kata_get_subcmd_options "$subcmd_seen")
   115              else
   116                      # Potentially complete with container names
   117                      [ $container_cmd -eq 0 ] && opts="$(_kata_get_containers)"
   118              fi
   119      else
   120              if [ -n "$cur" ]; then
   121                      # Complete with global options and subcmds
   122                      opts="$opts $(_kata_get_global_options)"
   123                      opts="$opts $(_kata_get_subcmds)"
   124              elif [[ "${cur}" == -* ]]; then
   125                      # Complete with global options
   126                      opts=$(_kata_get_global_options)
   127              else
   128                      # Complete with subcmds
   129                      opts=$(_kata_get_subcmds)
   130              fi
   131      fi
   132  
   133      [ -n "$opts" ] && COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
   134  
   135      return 0
   136  }
   137  
   138  complete -F _kata_bash_autocomplete "$_kataruntime"