#compdef packer

_packer () {
  local -a sub_commands && sub_commands=(
    'build:Build image(s) from template'
    'console:Creates a console for testing variable interpolation'
    'fix:Fixes templates from old versions of packer'
    'fmt:Rewrites HCL2 config files to canonical format'
    'hcl2_upgrade:Transform a JSON template into an HCL2 configuration'
    'init:Install missing plugins or upgrade plugins'
    'inspect:See components of a template'
    'plugins:Interact with Packer plugins and catalog'
    'validate:Check that a template is valid'
    'version:Prints the Packer version'
  )

  local -a build_arguments && build_arguments=(
    '-debug[Debug mode enabled for builds.]'
    '-force[Force a build to continue if artifacts exist, deletes existing artifacts.]'
    '-machine-readable[Produce machine-readable output.]'
    '-color=[(false) Disable color output. (Default: color)]'
    '-except=[(foo,bar,baz) Run all builds and post-processors other than these.]'
    '-on-error=[(cleanup,abort,ask) If the build fails do: clean up (default), abort, or ask.]'
    '-only=[(foo,bar,baz) Only build the given builds by name.]'
    '-parallel=[(false) Disable parallelization. (Default: false)]'
    '-parallel-builds=[(0) Number of builds to run in parallel. (Defaults to infinite: 0)]'
    '-timestamp-ui[Enable prefixing of each ui output with an RFC3339 timestamp]'
    '-var[("key=value") Variable for templates, can be used multiple times.]'
    '-var-file=[(path) JSON or HCL2 file containing user variables.]'
    '-warn-on-undeclared-var[Display warnings for user variable files containing undeclared variables.]'
    '(-)*:files:_files -g "*pkr.{hcl,json}"'
  )

  local -a console_arguments && console_arguments=(
    '-var[("key=value") Variable for templates, can be used multiple times.]'
    '-var-file=[(path) JSON or HCL2 file containing user variables.]'
    '(-)*:files:_files -g "*pkr.{hcl,json}"'
  )

  local -a fix_arguments && fix_arguments=(
    '(-)*:files:_files -g "*.json"'
  )

  local -a fmt_arguments && fmt_arguments=(
    '-check[Check if the input is formatted. Exit status 0 = properly formatted, non-zero otherwise.]'
    '-diff[Display diffs of formatting change]'
    '-write=[(false) Don not write to source files (always disabled if using -check)]'
    '-recursive[Also process files in subdirectories. By default cwd only]'
    '(-)*:files:_files -g "*pkr.{hcl,json}"'
  )

  local -a init_arguments && init_arguments=(
    '-upgrade[on top also update plugins to latest]'
    '(-)*:files:_files -g "*pkr.{hcl,json}"'
  )

  local -a inspect_arguments && inspect_arguments=(
    '-machine-readable[Machine-readable output]'
    '(-)*:files:_files -g "*pkr.{hcl,json}"'
  )

  local -a validate_arguments && validate_arguments=(
    '-syntax-only[Only check syntax. Do not verify config of the template.]'
    '-except=[(foo,bar,baz) Validate all builds other than these.]'
    '-only=[(foo,bar,baz) Validate only these builds.]'
    '-var[("key=value") Variable for templates, can be used multiple times.]'
    '-var-file=[(path) JSON or HCL2 file containing user variables.]'
    '-no-warn-undeclared-var[Disable warnings for user variable files containing undeclared variables.]'
    '-evaluate-datasources[(false) Evaluate data sources during validation (HCL2 only, may incur costs).]'
    '(-)*:files:_files -g "*pkr.{hcl,json}"'
  )

  _arguments -C \
    ':command:->command' \
    '*::options:->options'

  case $state in
      command)
        _describe -t commands 'command' sub_commands ;;
      options)
        case $line[1] in
          build)
            _arguments -s -S : $build_arguments ;;
          console)
            _arguments -s -S : $console_arguments ;;
          fix)
            _arguments -s -S : $fix_arguments ;;
          fmt)
            _arguments -s -S : $fmt_arguments ;;
          init)
            _arguments -s -S : $init_arguments ;;
          inspect)
            _arguments -s -S : $inspect_arguments ;;
          validate)
            _arguments -s -S : $validate_arguments ;;
        esac
      ;;
  esac
}
_packer "$@"
