Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command completion does not continue for the execed command #482

Open
viraptor opened this issue Jun 26, 2023 · 3 comments
Open

Command completion does not continue for the execed command #482

viraptor opened this issue Jun 26, 2023 · 3 comments
Labels
blocked Blocked on something else enhancement New feature or request priority:high High Priority Items

Comments

@viraptor
Copy link

Output of aws-sso version:

AWS SSO CLI Version 1.9.10 -- Copyright 2021-2022 Aaron Turner
unknown (nixpkgs) built at unknown

Describe the bug:
When using command completion, the state doesn't "reset" after exec ... --

To Reproduce:

  1. aws-sso exec -p <TAB> - I get profiles completion
  2. aws-sso exec -p some-profile -- aws <TAB> - no completion available

Expected behavior:
exec ... -- should go back to normal completion. I expect: aws-sso exec -p some-profile -- aws <TAB> to show me the awscli options.

Desktop (please complete the following information):

  • OS: macOS
  • Version 13.4.1
  • shell: zsh 5.9
@viraptor viraptor added the bug Something isn't working label Jun 26, 2023
@synfinatic
Copy link
Owner

Can you provide an example of another command which calls another command in this manner and supports auto-completion?

@synfinatic synfinatic added enhancement New feature or request and removed bug Something isn't working labels Jul 27, 2023
@viraptor
Copy link
Author

viraptor commented Jul 28, 2023

sudo does it for example. I believe it's done here https://github.com/scop/bash-completion/blob/master/completions/sudo#L17 (not 100% sure though, I'm not great with bash completions)

I ended up writing a custom zsh completion after all - feel free to reuse:

#compdef aws-sso

function _aws-sso() {
  local context state line curcontext="$curcontext"
  local global_args
  global_args=(
    '(-h --help)'{-h,--help}'[Help]'
    '(-b --browser)'{-b,--browser}'[Browser]: :'
    '--config[Config file]: :_files'
    '(-L --level)'{-L,--level}'[Logging level]: :(error warn info debug trace)'
    '--lines[Print line number in logs]'
    '(-u --url-action)'{-u,--url-action}'[How to handle URLs]: :(clip exec open print printurl granted-containers open-url-in-container)'
    '(-S --sso)'{-S,--sso}'[Override default AWS SSO Instance]: :'
    '--sts-refresh[Force refresh of STS Token Credentials]'
    '--no-config-check[Disable automatic ~/.aws/config updates]'
    '--threads[Override number of threads for talking to AWS]: :_numbers'
  )

  _arguments -C \
    $global_args \
    '1: : _aws-sso_cmds' \
    '*::arg:->args' && ret=0

  case "$state" in
    (args)
      local subcommand; subcommand="$words[1]"
      curcontext="${curcontext%:*:*}:aws-sso-cmd-$subcommand:"
      case "$subcommand" in
        (exec)
          _arguments -S -C \
            $global_args \
            '(-a --arn)'{-a,--arn}'[ARN of role to assume]: :' \
            '(-A --account)'{-A,--account}'[AWS AccountID of role to assume]: :' \
            '(-R --role)'{-R,--role}'[Name of AWS Role to assume]: :' \
            '(-p --profile)'{-p,--profile}'[Name of AWS Profile to assume]: :_aws-sso_profiles' \
            '(-n --no-region)'{-n,--no-region}'[Do not set AWS_DEFAULT_REGION from config.yaml]' \
            '*::cmd:->cmd' && ret=0
          case $state in
            (cmd)
              _normal -P
              ;;
          esac
          ;;

        (console)
          _arguments -C \
            $global_args \
            '--region[AWS Region]: :' \
            '(-d --duration)'{-d,--duration}'[AWS Session duration in minutes]: :_numbers -u minutes' \
            '(-P --prompt)'{-P,--prompt}'[Force interactive prompt to select role]' \
            '(-a --arn)'{-a,--arn}'[ARN of role to assume]: :' \
            '(-A --account)'{-A,--account}'[AWS AccountID of role to assume]: :' \
            '(-R --role)'{-R,--role}'[Name of AWS Role to assume]: :' \
            '(-p --profile)'{-p,--profile}'[Name of AWS Profile to assume]: :_aws-sso_profiles' \
            && ret=0
          ;;

        (eval)
          _arguments -C \
            $global_args \
            '(-a --arn)'{-a,--arn}'[ARN of role to assume]: :' \
            '(-A --account)'{-A,--account}'[AWS AccountID of role to assume]: :' \
            '(-R --role)'{-R,--role}'[Name of AWS Role to assume]: :' \
            '(-p --profile)'{-p,--profile}'[Name of AWS Profile to assume]: :_aws-sso_profiles' \
            '(-c --clear)'{-c,--clear}'[Generate "unset XXXX" commands to clear environment]' \
            '(-n --no-region)'{-n,--no-region}'[Do not set AWS_DEFAULT_REGION from config.yaml]' \
            '(-r --refresh)'{-r,--refresh}'[Refresh current IAM credentials]' \
            && ret=0
          ;;

        (flush)
          _arguments -C \
            $global_args \
            '(-t --type)'{-t,--type}'[Type of credentials to flush]: :(sts sso all)' \
            && ret=0
          ;;
        
        (list)
          _aws-sso_fields
          ;;
      esac
      ;;
  esac

  return ret
}

_aws-sso_cmds() {
  local commands
  commands=("${(@f)$(aws-sso --help | gawk '/Commands:/ {enable="true"} /^  \S/ && enable == "true" {scmd=$1} /^    \S/ && enable == "true" {sub(/^    /,"") ; print scmd ":" $0}')}")
  _describe -t commands 'command' commands "$@"
}

_aws-sso_fields() {
  local fields
  fields=("${(@f)$(aws-sso list -f | gawk '/\S/ && enable=="true" {sub(/\s*\|\s*/, ":"); print} /^=/ {enable="true"}')}")
  _describe -t fields 'fields' fields
}

_aws-sso_profiles() {
  local profiles
  profiles=("${(@f)$(aws-sso list --csv Profile)}")
  _describe -t profiles 'profiles' profiles
}

The magic part here is the

          case $state in
            (cmd)
              _normal -P

@synfinatic synfinatic added the priority:high High Priority Items label Aug 21, 2023
@synfinatic
Copy link
Owner

opened: WillAbides/kongplete#21

@synfinatic synfinatic added the blocked Blocked on something else label Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Blocked on something else enhancement New feature or request priority:high High Priority Items
Projects
None yet
Development

No branches or pull requests

2 participants