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

Add aliases to tab completion #26

Open
stefansundin opened this issue Nov 4, 2017 · 3 comments
Open

Add aliases to tab completion #26

stefansundin opened this issue Nov 4, 2017 · 3 comments

Comments

@stefansundin
Copy link

I've been a bit frustrated that I can't tab complete my aliases.

This test seems to indicate that it is by design, but why? https://github.com/aws/aws-cli/blob/3db7be30dfdd76f4e322b83b67af3134ae0b32ac/tests/functional/docs/test_help_output.py#L457

@yermulnik
Copy link

Any updates on this request?

@rothgar
Copy link

rothgar commented Mar 8, 2022

Aliases would require manual shell completion files. It's possible to create your own completion files but not something that is supported by aws_completer

@yermulnik
Copy link

So to add AWS CLI aliases to bash completions for aws one would need to create their own bash-completion function (e.g. _aws_cli_aliases), withing this function extract AWS CLI alias names like awk '$2 == "=" && $1 !~ /^#/ {print $1}' ~/.aws/cli/alias | sort and then add them to the completion stack like complete -C aws_completer -F _aws_cli_aliases aws.
This is a bit cumbersome and needs fiddling though... Here's what I came up with long ago (you would have to apologize the mess as it's a way far from ideal thing):
~/.bash_completion.d/aws.completion:

_aws_cli_aliases()
{
	COMPREPLY=()
	local cur=${COMP_WORDS[COMP_CWORD]}
	local prev=${COMP_WORDS[COMP_CWORD-1]}
	local cword=${COMP_WORDS[0]}

	if [[ $cur =~ ^- ]]; then
		if [[ $(aws version 2>&1 | awk -F"[/ .]" '{print $2}') -gt 1 ]]; then
			local all_global_opts=$(~/bin/aws-list-all-available-commands.py global_opts)
			COMPREPLY=($(compgen -W "$all_global_opts" -- $cur))
		else
			return 0
		fi
	elif [[ $prev == --profile ]]; then
		COMPREPLY=($(compgen -W "$(awk '/^\[/' ${AWS_SHARED_CREDENTIALS_FILE:=${HOME}/.aws/credentials} | tr -d '[]' | sort)" $cur))
	elif [[ $prev == --region ]]; then
		# aws ec2 describe-regions --query 'Regions[].RegionName[]' --output text | xargs -n1
		COMPREPLY=($(compgen -W "eu-north-1 ap-south-1 eu-west-3 eu-west-2 eu-west-1 ap-northeast-2 ap-northeast-1 sa-east-1 ca-central-1 ap-southeast-1 ap-southeast-2 eu-central-1 us-east-1 us-east-2 us-west-1 us-west-2" $cur))
	elif [[ $prev == --output ]]; then
		COMPREPLY=($(compgen -W "json yaml yaml-stream text table" $cur))
	else
		local cmds_hits
		local all_cmds=$(~/bin/aws-list-all-available-commands.py)
		local all_aliases=$(awk '$2 == "=" && $1 !~ /^#/ {print $1}' ~/.aws/cli/alias | sort)

		for cli_alias in $all_aliases; do
			if [[ $prev == $cli_alias ]]; then
				local all_global_opts=$(~/bin/aws-list-all-available-commands.py global_opts)
				COMPREPLY=($(compgen -W "$all_global_opts" -- $cur))
				return 0
			fi
		done

		for cli_cmd in ${COMP_WORDS[@]}; do
			for aws_cmd in $all_cmds $all_aliases; do
				if [[ $aws_cmd == $cli_cmd ]]; then
					((cmds_hits++))
					break 2
				fi
			done
		done
		[[ ! $cmds_hits ]] && COMPREPLY=($(compgen -W "$all_aliases" $cur))
	fi

	return 0
}
complete -C aws_completer -F _aws_cli_aliases aws

~/bin/aws-list-all-available-commands.py:

#!/usr/bin/env python
from sys import argv

if len(argv) > 1:
    if argv[1] == 'global_opts':
        import awscli.completer
        awscli.completer.complete("-", None)
else:
    import awscli.clidriver
    for cmd in awscli.clidriver.create_clidriver().create_help_command().command_table.keys():
        print(cmd)

Hope this helps someone...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants