#compdef cargo-pgrx

# cargo-pgrx is a cargo subcommand: the binary's first argument is always the
# literal `pgrx` (so `cargo pgrx <command>` dispatches here). This file completes
# the `cargo-pgrx` binary directly; `cargo pgrx …` is handled by _cargo.

_cargo-pgrx() {
  local curcontext="$curcontext" state line
  typeset -A opt_args

  # Postgres versions accepted as positionals / by the --pgNN init flags.
  local -a _pg_versions=(pg13 pg14 pg15 pg16 pg17 pg18 all)

  # Option groups shared across subcommands. Declared local; zsh's dynamic
  # scope makes them visible to _cargo_pgrx_cmd below.
  local -a _pgrx_global _pgrx_pkg _pgrx_features _pgrx_pgconfig

  _pgrx_global=(
    '(-v --verbose)*'{-v,--verbose}'[Enable info logs, -vv debug, -vvv trace]'
    '(-h --help)'{-h,--help}'[Print help]'
    '(-V --version)'{-V,--version}'[Print version]'
  )

  _pgrx_pkg=(
    '(-p --package)'{-p+,--package=}'[Package to build (see cargo help pkgid)]:package:'
    '--manifest-path=[Path to Cargo.toml]:Cargo.toml:_files -g "*.toml"'
  )

  _pgrx_features=(
    '--all-features[Activate all available features]'
    '--no-default-features[Do not activate the default feature]'
    '(-F --features)'{-F+,--features=}'[Space-separated list of features]:features:'
    '--target=[Build target triple]:triple:'
  )

  _pgrx_pgconfig=(
    '(-c --pg-config)'{-c+,--pg-config=}'[The pg_config path (default: first in $PATH)]:pg_config:_files'
  )

  _arguments -C \
    '1:pgrx subcommand:(pgrx)' \
    '*::args:->args'

  case $state in
    args)
      case $line[1] in
        pgrx) _cargo_pgrx_cmd ;;
      esac
      ;;
  esac
}

_cargo_pgrx_cmd() {
  local curcontext="$curcontext" state line
  typeset -A opt_args

  _arguments -C \
    "${_pgrx_global[@]}" \
    '1:command:->command' \
    '*::args:->cmdargs'

  case $state in
    command)
      local -a commands=(
        'bench:Run in-process benchmarks using #[pg_bench] functions'
        'init:Initialize pgrx development environment for the first time'
        'info:Provides information about pgrx-managed development environment'
        'start:Start a pgrx-managed Postgres instance'
        'stop:Stop a pgrx-managed Postgres instance'
        'status:Is a pgrx-managed Postgres instance running?'
        'new:Create a new extension crate'
        'install:Install the crate as an extension into the Postgres specified by pg_config'
        'package:Create an installation package directory'
        'schema:Generate extension schema files'
        'run:Compile/install extension to a pgrx-managed Postgres and start psql'
        'connect:Connect, via psql, to a Postgres instance'
        'test:Run the test suite for this crate'
        'get:Get a property from the extension control file'
        'cross:Commands for cross-compilation (experimental)'
        'upgrade:Upgrade pgrx crate versions in Cargo.toml'
        'regress:Run the regression test suite for this crate'
        'help:Print this message or the help of the given subcommand(s)'
      )
      _describe -t commands 'cargo pgrx command' commands
      ;;
    cmdargs)
      case $line[1] in
        bench)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" "${_pgrx_features[@]}" \
            '--dbname=[Use this database name instead of $extname_benches]:dbname:' \
            '--group-name=[Unique name for this benchmark group]:name:' \
            '--compare-group=[Named benchmark group to compare against]:group:' \
            '--resetdb[Recreate the benchmark database before running]' \
            '--cascade[Use CASCADE when dropping the extension during refresh]' \
            '--list[List discovered benchmark wrappers and exit]' \
            '--report[Render a read-only history report]' \
            '--json[Emit the final summary as JSON]' \
            '--wait=[Sleep this many seconds before starting benchmarks]:seconds:' \
            '--debug[Compile for debug mode instead of release]' \
            '--profile=[Specific profile to use]:profile:' \
            '--postgresql-conf=[Custom postgresql.conf settings (key=value)]:setting:' \
            "*:args:($_pg_versions)"
          ;;
        init)
          _arguments "${_pgrx_global[@]}" \
            '--pg13=[Path to PG13 pg_config, or "download"]:pg_config:_files' \
            '--pg14=[Path to PG14 pg_config, or "download"]:pg_config:_files' \
            '--pg15=[Path to PG15 pg_config, or "download"]:pg_config:_files' \
            '--pg16=[Path to PG16 pg_config, or "download"]:pg_config:_files' \
            '--pg17=[Path to PG17 pg_config, or "download"]:pg_config:_files' \
            '--pg18=[Path to PG18 pg_config, or "download"]:pg_config:_files' \
            '--base-port=[Base port number]:port:' \
            '--base-testing-port=[Base testing port number]:port:' \
            '--configure-flag=[Additional flag to pass to configure]:flag:' \
            '--no-run[Do not run compiled postgresql binaries (cross compiling)]' \
            '--valgrind[Compile PostgreSQL with Valgrind memory-error flags]' \
            '(-j --jobs)'{-j+,--jobs=}'[Allow N make jobs at once]:jobs:'
          ;;
        info)
          _arguments "${_pgrx_global[@]}" \
            '1:info command:(( path\:"Print path to a base version of Postgres build" pg-config\:"Print path to pg_config for a base version" version\:"Print specific version for a base Postgres version" help\:"Print help" ))'
          ;;
        start|stop|status)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" \
            '--postgresql-conf=[Custom postgresql.conf settings]:setting:' \
            '--valgrind[Run under valgrind]' \
            "*:pg version:($_pg_versions)"
          ;;
        new)
          _arguments "${_pgrx_global[@]}" \
            '(-b --bgworker)'{-b,--bgworker}'[Create a background worker template]' \
            '1:extension name:'
          ;;
        install)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" "${_pgrx_features[@]}" "${_pgrx_pgconfig[@]}" \
            '(-r --release)'{-r,--release}'[Compile for release mode]' \
            '--profile=[Specific profile to use]:profile:' \
            '--test[Build in test mode (for cargo pgrx test)]' \
            '(-s --sudo)'{-s,--sudo}'[Use sudo to install the extension artifacts]'
          ;;
        package)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" "${_pgrx_features[@]}" "${_pgrx_pgconfig[@]}" \
            '(-d --debug)'{-d,--debug}'[Compile for debug mode]' \
            '--profile=[Specific profile to use]:profile:' \
            '--test[Build in test mode (for cargo pgrx test)]' \
            '--out-dir=[Directory to output the package]:dir:_directories'
          ;;
        schema)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" "${_pgrx_features[@]}" "${_pgrx_pgconfig[@]}" \
            '--test[Build in test mode (for cargo pgrx test)]' \
            '(-r --release)'{-r,--release}'[Compile for release mode]' \
            '--profile=[Specific profile to use]:profile:' \
            '(-o --out)'{-o+,--out=}'[Path to output a produced SQL file]:file:_files' \
            '(-d --dot)'{-d+,--dot=}'[Path to output a produced GraphViz DOT file]:file:_files' \
            '--skip-build[Skip building a fresh extension shared object]' \
            "*:pg version:($_pg_versions)"
          ;;
        run)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" "${_pgrx_features[@]}" \
            '(-r --release)'{-r,--release}'[Compile for release mode]' \
            '--profile=[Specific profile to use]:profile:' \
            '--pgcli[Use an existing pgcli on the $PATH]' \
            '--install-only[Install without running]' \
            '--valgrind[Run under valgrind]' \
            "1:pg version:($_pg_versions)" \
            '2:dbname:'
          ;;
        connect)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" \
            '--pgcli[Use an existing pgcli on the $PATH]' \
            '--valgrind[Run under valgrind]' \
            "1:pg version:($_pg_versions)" \
            '2:dbname:'
          ;;
        test)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" "${_pgrx_features[@]}" \
            '(-r --release)'{-r,--release}'[Compile for release mode]' \
            '--profile=[Specific profile to use]:profile:' \
            '(-n --no-schema)'{-n,--no-schema}"[Don't regenerate the schema]" \
            '--runas=[Run the Postgres test instance as this system user]:user:_users' \
            '--pgdata=[Initialize the test cluster here]:dir:_directories' \
            "1:pg version:($_pg_versions)" \
            '2:testname:'
          ;;
        get)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" \
            '1:property name:'
          ;;
        cross)
          _arguments "${_pgrx_global[@]}" \
            '1:cross command:(( pgrx-target\:"Build target artifacts for cross-compilation" help\:"Print help" ))'
          ;;
        upgrade)
          _arguments "${_pgrx_global[@]}" \
            '--to=[Version to upgrade to (default: latest)]:version:' \
            '(-m --manifest-path)'{-m+,--manifest-path=}'[Path to the manifest file]:Cargo.toml:_files -g "*.toml"' \
            '--include-prereleases[Allow upgrading PGRX to pre-release versions]' \
            '(-p --package)'{-p+,--package=}'[Package within the workspace to upgrade]:package:' \
            '(-n --dry-run)'{-n,--dry-run}"[Print the new Cargo.toml without modifying it]"
          ;;
        regress)
          _arguments "${_pgrx_global[@]}" "${_pgrx_pkg[@]}" "${_pgrx_features[@]}" \
            '--dbname=[Use this database name instead of $extname_regress]:dbname:' \
            '--resetdb[Recreate the test database]' \
            '(-r --release)'{-r,--release}'[Compile for release mode]' \
            '--profile=[Specific profile to use]:profile:' \
            '(-n --no-schema)'{-n,--no-schema}"[Don't regenerate the schema]" \
            '--runas=[Run the Postgres test instance as this system user]:user:_users' \
            '--pgdata=[Initialize the test cluster here]:dir:_directories' \
            '--psql-verbosity=[Verbosity of error reports]:verbosity:(default verbose terse sqlstate)' \
            '--postgresql-conf=[Custom postgresql.conf settings (key=value)]:setting:' \
            '(-a --auto)'{-a,--auto}'[Overwrite expected output for failed tests]' \
            '--add=[Bootstrap a new test, promote its output, and exit]:testname:' \
            '--dry-run[Print what would happen without doing it]' \
            '--repeat=[Run the test suite this many times]:n:' \
            '*:testname:'
          ;;
        help)
          local -a sub=(
            bench init info start stop status new install package schema run
            connect test get cross upgrade regress help
          )
          _describe -t commands 'command' sub
          ;;
      esac
      ;;
  esac
}

_cargo-pgrx "$@"
