dtan4/terraforming

View on GitHub
contrib/zsh-completion/_terraforming

Summary

Maintainability
Test Coverage
#compdef terraforming

function _terraforming() {
  local context curcontext=$curcontext state line
  typeset -A opt_args
  local ret=1

  _arguments -C \
    '1: :__terraforming_sub_commands' \
    '*:: :->args' \
    && ret=0

  case $state in
    (args)
      case $words[1] in
        (help)
          _arguments -C \
            '1: :__terraforming_sub_commands' \
            '(-)*:: :->null_state' \
            && ret=0
        ;;
      (*)
          _arguments -C \
            '--merge=[(TFSTATE) tfstate file to merge]:tfstate file to merge:_files' \
            '--overwrite[Overwrite existing tfstate]' \
            '--no-overwrite[Do not overwrite existing tfstate]' \
            '--tfstate[Generate tfstate]' \
            '--no-tfstate[Do not generate tfstate]' \
            '--profile[(PROFILE) AWS credentials profile]:AWS credentials profile:__profiles' \
            && ret=0
        ;;
      esac
      ;;
  esac

  return ret
}

__terraforming_sub_commands() {
  local -a _c

  _c=(
    "${(@f)$(terraforming --help \
      | grep '  terraforming' \
      | sed -E 's/^  terraforming //g' \
      | sed -E 's/ *(\[COMMAND\])? *# /:/g')}"
    )

  _describe -t commands terraforming_sub_commands _c
}

__profiles() {
  local _profile_path="${HOME}/.aws/credentials"
  local -a _profiles

  _profiles=(
    ${(@f)"$(_call_program profiles \
      "grep -E '^\[.*\]$' "$_profile_path" \
        | sed -e 's/\[//' -e 's/\]//'")"}
    )

  _describe -t profiles Profiles _profiles
}

_terraforming "$@"