# bash completion file for buildah command
#
# 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. ~/.buildah-completion.sh and add the line
#    below to your .bashrc after bash completion features are loaded
#    . ~/.buildah-completion.sh
#
# Configuration:
#

# __buildah_to_alternatives transforms a multiline list of strings into a single line
# string with the words separated by `|`.
# This is used to prepare arguments to __buildah_pos_first_nonflag().
__buildah_to_alternatives() {
    local parts=( $1 )
    local IFS='|'
    echo "${parts[*]}"
}

# __buildah_to_extglob transforms a multiline list of options into an extglob pattern
# suitable for use in case statements.
__buildah_to_extglob() {
    local extglob=$( __buildah_to_alternatives "$1" )
    echo "@($extglob)"
}

# __buildah_pos_first_nonflag finds the position of the first word that is neither
# option nor an option's argument. If there are options that require arguments,
# you should pass a glob describing those options, e.g. "--option1|-o|--option2"
# Use this function to restrict completions to exact positions after the argument list.
__buildah_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++ ))
      # eat "=" in case of --option=arg syntax
      [ "${words[$counter]}" = "=" ] && (( counter++ ))
    else
      case "${words[$counter]}" in
        -*)
          ;;
        *)
          break
          ;;
      esac
    fi

    # Bash splits words at "=", retaining "=" as a word, examples:
    # "--log-level=error" => 3 words, "--log-opt syslog-facility=daemon" => 4 words
    while [ "${words[$counter + 1]}" = "=" ] ; do
      counter=$(( counter + 2))
    done

    (( counter++ ))
  done

  echo $counter
}

# 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.

__buildah_previous_extglob_setting=$(shopt -p extglob)
shopt -s extglob

# __buildah_list_mounted
__buildah_list_mounted() {
   COMPREPLY=($(compgen -W "$(buildah mount | awk '{print $1}')" -- $cur))
}

__buildah_list_containers() {
   COMPREPLY=($(compgen -W "$(buildah containers --format '{{.ContainerName}} {{.ContainerID}}' )" -- $cur))
}
__buildah_list_images() {
   COMPREPLY=($(compgen -W "$(buildah images --format '{{.ID}} {{.Name}}' )" -- $cur))
}
__buildah_list_images_scratch() {
   COMPREPLY=($(compgen -W "$(buildah images --format '{{.ID}} {{.Name}}' ) scratch" -- $cur))
}
__buildah_list_containers_images() {
   COMPREPLY=($(compgen -W "$(buildah containers --format '{{.ContainerName}} {{.ContainerID}}') $(buildah images --format '{{.ID}} {{.Name}}')" -- $cur))
}

__buildah_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 __buildah_pos_first_nonflag().
__buildah_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.
__buildah_to_extglob() {
    local extglob=$(__buildah_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.
__buildah_subcommands() {
	  local subcommands="$1"

	  local counter=$(($command_pos + 1))
	  while [ $counter -lt $cword ]; do
		    case "${words[$counter]}" in
			      $(__buildah_to_extglob "$subcommands") )
subcommand_pos=$counter
local subcommand=${words[$counter]}
local completions_func=_buildah_${command}_${subcommand}
declare -F $completions_func >/dev/null && $completions_func
return 0
;;
esac
(( counter++ ))
done
return 1
}

 # suppress trailing whitespace
 __buildah_nospace() {
     # compopt is not available in ancient bash versions
     type compopt &>/dev/null && compopt -o nospace
 }


 # global options that may appear after the buildah command
 _buildah_buildah() {
     local boolean_options="
         --help -h
         --version -v
         "
     local options_with_args="
         --registries-conf
         --registries-conf-dir
         --root
         --runroot
         --storage-driver
         --storage-opt
         --userns-uid-map
         --userns-gid-map
         "

     case "$prev" in
         --root | --runroot)
             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"))
                     __buildah_nospace
                     ;;
                 *)
                     _filedir
                     __buildah_nospace
                     ;;
             esac
             return
             ;;
         --storage-driver)
             COMPREPLY=($(compgen -W 'devicemapper overlay2' -- "$cur"))
             return
             ;;
         $(__buildah_to_extglob "$options_with_args"))
 return
 ;;
 esac

 case "$cur" in
     -*)
         COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
         ;;
     *)
         local counter=$(__buildah_pos_first_nonflag $(__buildah_to_extglob "$options_with_args"))
         if [ $cword -eq $counter ]; then
             COMPREPLY=($(compgen -W "${commands[*]} help" -- "$cur"))
         fi
         ;;
 esac
}

 _buildah_rmi() {
     local boolean_options="
     --all
     -a
     --prune
     -p
     --force
     -f
     --help
     -h
  "

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
         *)
             __buildah_list_images
             ;;
     esac
 }

 _buildah_rm() {
     local boolean_options="
     --all
     -a
     --help
     -h
  "

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
         *)
             __buildah_list_containers
             ;;
     esac
 }

 _buildah_help() {
     local counter=$(__buildah_pos_first_nonflag)
     if [ $cword -eq $counter ]; then
         COMPREPLY=($(compgen -W "${commands[*]}" -- "$cur"))
     fi
 }

 _buildah_config() {
     local boolean_options="
     --add-history
     --help
     -h
  "

     local options_with_args="
       --annotation
       -a
       --arch
       --author
       --cmd
       --comment
       --created-by
       --domainname
       --entrypoint
       --env
       -e
       --healthcheck
       --healthcheck-interval
       --healthcheck-retries
       --healthcheck-start-period
       --healthcheck-timeout
       --history-comment
       --hostname
       --label
       -l
       --onbuild
       --os
       --port
       -p
       --shell
       --stop-signal
       --user
       -u
       --volume
       -v
       --workingdir
   "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
         *)
             __buildah_list_containers
             ;;
     esac
 }

 _buildah_commit() {
     local boolean_options="
          --help
          -h
          --disable-compression
          -D
          --manifest
          --quiet
          -q
          --rm
          --squash
          --tls-verify
          --omit-timestamp
  "

     local options_with_args="
          --authfile
          --cert-dir
          --creds
          --format
          -f
          --iidfile
          --sign-by
  "

     local all_options="$options_with_args $boolean_options"

     case "$prev" in
         --signature-policy)
             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"))
                     __buildah_nospace
                     ;;
                 *)
                     _filedir
                     __buildah_nospace
                     ;;
             esac
             return
             ;;

         $(__buildah_to_extglob "$options_with_args"))
 return
 ;;
 esac

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
         *)
             __buildah_list_containers
             ;;
     esac
 }

 _buildah_bud() {
     local boolean_options="
     --help
     -h
     --layers
     --no-cache
     --omit-timestamp
     --pull
     --pull-always
     --pull-never
     --quiet
     -q
     --squash
     --tls-verify
  "

     local options_with_args="
     --arch
     --add-host
     --annotation
     --authfile
     --build-arg
     --cap-add
     --cap-drop
     --cert-dir
     --cgroup-parent
     --cni-config-dir
     --cni-plugin-path
     --cpu-period
     --cpu-quota
     --cpu-shares
     --cpuset-cpus
     --cpuset-mems
     --creds
     --decryption-key
     --device
     --dns-search
     --dns
     --dns-option
     -f
     --file
     --format
     --http-proxy
     --ignorefile
     --iidfile
     --isolation
     --ipc
     --label
     --manifest
     -m
     --memory
     --memory-swap
     --net
     --network
     --no-pivot
     --os
     --pid
     --platform
     --runtime
     --runtime-flag
     --security-opt
     --shm-size
     --sign-by
     -t
     --tag
     --target
     --ulimit
     --userns
     --userns-uid-map
     --userns-gid-map
     --userns-uid-map-user
     --userns-gid-map-group
     --uts
     --volume
     -v
  "

     local all_options="$options_with_args $boolean_options"

     case "$prev" in
         --runtime)
             COMPREPLY=($(compgen -W 'runc runv' -- "$cur"))
             ;;
         $(__buildah_to_extglob "$options_with_args"))
 return
 ;;
 esac

 case "$cur" in
     -*)
         COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
         ;;
 esac
}

 _buildah_build_using_dockerfile() {
	 _buildah_bud "$@"
}

 _buildah_run() {
     local boolean_options="
     --add-history
     --help
     -t
     --terminal
     -h
  "

     local options_with_args="
     --cap-add
     --cap-drop
     --cni-config-dir
     --cni-plugin-path
     --hostname
     --ipc
     --isolation
     --mount
     --net
     --network
     --pid
     --runtime
     --runtime-flag
     --user
     --uts
     --volume
     -v
  "

     local all_options="$options_with_args $boolean_options"

     case "$prev" in
         --runtime)
             COMPREPLY=($(compgen -W 'runc runv' -- "$cur"))
             ;;
         $(__buildah_to_extglob "$options_with_args"))
 return
 ;;
 esac

 case "$cur" in
     -*)
         COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
	 ;;
      *)
         __buildah_list_containers
         ;;
 esac
}

 _buildah_copy() {
     local boolean_options="
     --add-history
     --help
     -h
     --quiet
     -q
     --tls-verify
     --remove-signatures
  "

     local options_with_args="
     --chown
     --chmod
     --contextdir
     --ignorefile
     --from
     --authfile
     --cert-dir
     --creds
     --decryption-key
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_add() {
     local boolean_options="
     --add-history
     --help
     -h
     --quiet
     -q
     --tls-verify
     --remove-signatures
    "

     local options_with_args="
     --chown
     --chmod
     --contextdir
     --ignorefile
     --from
     --authfile
     --cert-dir
     --creds
     --decryption-key
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_unmount() {
     _buildah_umount $@
 }

 _buildah_umount() {
     local boolean_options="
     --all
     -a
     --help
     -h
  "

     local options_with_args="
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
         *)
	     __buildah_list_mounted
             ;;
     esac
 }

 _buildah_pull() {
     local boolean_options="
     --all-tags
     -a
     --help
     -h
     --quiet
     -q
     --tls-verify
     --remove-signatures
  "

     local options_with_args="
     --authfile
     --cert-dir
     --creds
     --decryption-key
     --policy
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_push() {
     local boolean_options="
          --all
          --help
          -h
          --disable-compression
          -D
          --quiet
          -q
          --rm
          --tls-verify
          --remove-signatures
  "

     local options_with_args="
          --authfile
          --cert-dir
          --creds
          --encrypt-layer
          --encryption-key
          --format
          -f
          --sign-by
  "

     local all_options="$options_with_args $boolean_options"

     case "$prev" in
         --signature-policy)
             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"))
                     __buildah_nospace
                     ;;
                 *)
                     _filedir
                     __buildah_nospace
                     ;;
             esac
             return
             ;;

         $(__buildah_to_extglob "$options_with_args"))
 return
 ;;
 esac

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
         *)
             __buildah_list_images
             ;;
     esac
 }

 _buildah_logout() {
     local boolean_options="
     --help
     -h
     --all
     -a
  "

     local options_with_args="
     --authfile
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_login() {
     local boolean_options="
     --help
     -h
     --get-login
     --tls-verify
  "

     local options_with_args="
     --authfile
     --cert-dir
     --password string
     -p
     --password-stdin
     --username
     -u
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_manifest() {
     local boolean_options="
     --help
     -h
     --all
  "
     subcommands="
        add
        annotate
        create
        inspect
        push
        remove
        rm
     "
     __buildah_subcommands "$subcommands" && return

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options " -- "$cur"))
             ;;
         *)
             COMPREPLY=( $( compgen -W "$subcommands" -- "$cur" ) )
             ;;
     esac

}
 _buildah_manifest_add() {
     local boolean_options="
     --help
     -h
     --all
     --tls-verify
  "

     local options_with_args="
     --authfile
     --annotation
     --arch
     --cert-dir
     --creds
     --features
     --os
     --os-features
     --os-version
     --variant
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_manifest_annotate() {
     local boolean_options="
     --help
     -h
  "

     local options_with_args="
     --annotation
     --arch
     --features
     --os
     --os-features
     --os-version
     --variant
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_manifest_create() {
     local boolean_options="
     --help
     -h
     --all
  "

     local options_with_args="
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_manifest_inspect() {
     local boolean_options="
     --help
     -h
  "

     local options_with_args="
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_manifest_push() {
     local boolean_options="
     --help
     -h
     --all
     --remove-signatures
     --tls-verify
  "

     local options_with_args="
     --authfile
     --cert-dir
     --creds
     --digestfile
     --format
     -f
     --rm
     --sign-by
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_manifest_remove() {
     local boolean_options="
     --help
     -h
  "

     local options_with_args="
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_manifest_rm() {
     local boolean_options="
     --help
     -h
  "

     local options_with_args="
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_mount() {
     local boolean_options="
     --help
     -h
     --notruncate
  "

     local options_with_args="
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
         *)
             __buildah_list_containers
             ;;
     esac
 }

 _buildah_ps() {
   _buildah_containers
 }

_buildah_list() {
   _buildah_containers
 }

_buildah_ls() {
   _buildah_containers
 }

_buildah_containers() {
     local boolean_options="
     --help
     -h
     --json
     --quiet
     -q
     --noheading
     -n
     --notruncate
     -a
     --all
  "

     local options_with_args="
     --filter
     -f
     --format
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_images() {
     local boolean_options="
     --all
     -a
     --digests
     --help
     -h
     --history
     --json
     --quiet
     -q
     --noheading
     -n
     --no-trunc
     --notruncate
  "

     local options_with_args="
     --filter
     -f
     --format
  "

     local all_options="$options_with_args $boolean_options"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
     esac
 }

 _buildah_info() {
     local options_with_args="
       --log-level
       --D
       --format
     "

     local all_options="$options_with_args"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$options_with_args" -- "$cur"))
             ;;
     esac
}

 _buildah_inspect() {
     local options_with_args="
       --format
       -f
       --type
       -t
     "

     local all_options="$options_with_args"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$options_with_args" -- "$cur"))
             ;;
         *)
             __buildah_list_containers_images
             ;;

esac
 }

 _buildah_tag() {
     local options_with_args="
     "

     local all_options="$options_with_args"

     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$options_with_args" -- "$cur"))
             ;;
         *)
             __buildah_list_images
             ;;
     esac
 }

 _buildah_from() {
     local boolean_options="
     --help
     -h
     --pull
     --pull-always
     --pull-never
     --quiet
     -q
     --tls-verify
  "

     local options_with_args="
     --add-host
     --authfile
     --cap-add
     --cap-drop
     --cert-dir
     --cgroup-parent
     --cidfile
     --cni-config-dir
     --cni-plugin-path
     --cpu-period
     --cpu-quota
     --cpu-shares
     --cpuset-cpus
     --cpuset-mems
     --creds
     --device
     --http-proxy
     --ipc
     --isolation
     -m
     --memory
     --memory-swap
     --name
     --net
     --network
     --pid
     --security-opt
     --shm-size
     --ulimit
     --userns
     --userns-uid-map
     --userns-gid-map
     --userns-uid-map-user
     --userns-gid-map-group
     --uts
     --volume
  "


     case "$cur" in
         -*)
             COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
             ;;
         *)
             __buildah_list_images_scratch
             ;;
     esac
 }

 _buildah_unshare() {
     local boolean_options="
     --help
     -h
     "

     local options_with_args="
     --mount
     "
 }

 _buildah_rename() {
     local boolean_options="
     --help
     -h
     "

     local options_with_args="
     "
 }

 _buildah_version() {
     local boolean_options="
     --help
     -h
     "

     local options_with_args="
     "
 }

 _buildah() {
   local previous_extglob_setting=$(shopt -p extglob)
   shopt -s extglob

   local commands=(
       add
       bud
       build-using-dockerfile
       commit
       config
       containers
       copy
       delete
       from
       images
       info
       inspect
       list
       ls
       manifest
       mount
       pull
       push
       ps
       rename
       rm
       rmi
       run
       tag
       umount
       unmount
       unshare
       version
   )

   COMPREPLY=()
   local cur prev words cword
   _get_comp_words_by_ref -n : cur prev words cword

   local command='buildah' command_pos=0 subcommand_pos
   local counter=1
   while [ $counter -lt $cword ]; do
       case "${words[$counter]}" in
           $(__buildah_to_extglob "$global_options_with_args") )
 (( counter++ ))
 ;;
 -*)
 ;;
 =)
 (( counter++ ))
 ;;
 *)
     command="${words[$counter]}"
     command_pos=$counter
     break
     ;;
 esac
 (( counter++ ))
done

 local binary="${words[0]}"

 local completions_func=_buildah_${command/-/_}
 declare -F $completions_func >/dev/null && $completions_func

 eval "$previous_extglob_setting"
 return 0
 }

 eval "$__buildah_previous_extglob_setting"
 unset __buildah_previous_extglob_setting

 complete -F _buildah buildah
