Skip to content

Commit 6123e41

Browse files
authored
feat(aws-cli): πŸ§‘β€πŸ’» add aws-cli completion (devcontainers#1083)
* feat(aws-cli): πŸ§‘β€πŸ’» add aws completion aws supports bash and zsh command completion. * test(aws-cli): βœ… test aws bash completion * chore(aws-cli): πŸ”§ update feature version * fix(aws-cli): cp command fix * refactor(aws-cli): move vendor scripts to specific folder * docs(aws-cli): add README to vendor scripts * build(aws-cli): automate aws-cli vendor completer scripts updates * chore(aws-cli): update vendor completer scripts * build(aws-cli): update message * chore(aws-cli): remove some code remainders
1 parent 878a900 commit 6123e41

File tree

9 files changed

+211
-8
lines changed

9 files changed

+211
-8
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: "Updates vendor 'aws_bash_completer' and 'aws_zsh_completer.sh' scripts"
2+
on:
3+
workflow_dispatch:
4+
schedule:
5+
- cron: '0 0 * * 0' # Runs every Sunday at midnight UTC (adjust as needed)
6+
7+
jobs:
8+
fetch-latest-aws-completer-scripts:
9+
runs-on: ubuntu-latest
10+
environment: documentation # grants access to secrets.PAT, for creating pull requests
11+
permissions:
12+
contents: write
13+
pull-requests: write
14+
steps:
15+
- uses: actions/checkout@v3
16+
17+
- name: Run fetch-latest-completer-scripts.sh
18+
run: src/aws-cli/scripts/fetch-latest-completer-scripts.sh
19+
20+
- name: Create a PR for completer scripts
21+
id: push_image_info
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.PAT }}
24+
run: |
25+
set -e
26+
echo "Start."
27+
28+
# Configure git and Push updates
29+
git config --global user.email [email protected]
30+
git config --global user.name github-actions
31+
git config pull.rebase false
32+
33+
branch=automated-script-update-$GITHUB_RUN_ID
34+
git checkout -b $branch
35+
message='[Updates] Automated vendor 'aws-cli' completer scripts'
36+
37+
# Add / update and commit
38+
git add src/aws-cli/scripts/vendor/aws_bash_completer
39+
git add src/aws-cli/scripts/vendor/aws_zsh_completer.sh
40+
41+
git commit -m 'Automated completer scripts update' || export NO_UPDATES=true
42+
43+
# Bump version and push
44+
if [ "$NO_UPDATES" != "true" ] ; then
45+
echo "$(jq --indent 4 '.version = (.version | split(".") | map(tonumber) | .[2] += 1 | join("."))' src/aws-cli/devcontainer-feature.json)" > src/aws-cli/devcontainer-feature.json
46+
git add src/aws-cli/devcontainer-feature.json
47+
48+
git commit -m 'Bump version'
49+
git push origin "$branch"
50+
gh api \
51+
--method POST \
52+
-H "Accept: application/vnd.github+json" \
53+
/repos/${GITHUB_REPOSITORY}/pulls \
54+
-f title="$message" \
55+
-f body="$message" \
56+
-f head="$branch" \
57+
-f base="$GITHUB_REF_NAME"
58+
fi

β€Žsrc/aws-cli/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "aws-cli",
3-
"version": "1.0.7",
3+
"version": "1.1.0",
44
"name": "AWS CLI",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/aws-cli",
66
"description": "Installs the AWS CLI along with needed dependencies. Useful for base Dockerfiles that often are missing required install dependencies like gpg.",

β€Žsrc/aws-cli/install.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,23 @@ fi
5252

5353
apt_get_update()
5454
{
55-
echo "Running apt-get update..."
56-
apt-get update -y
55+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
56+
echo "Running apt-get update..."
57+
apt-get update -y
58+
fi
5759
}
5860

5961
# Checks if packages are installed and installs them if not
6062
check_packages() {
6163
if ! dpkg -s "$@" > /dev/null 2>&1; then
62-
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
63-
echo "Running apt-get update..."
64-
apt-get update -y
65-
fi
64+
apt_get_update
6665
apt-get -y install --no-install-recommends "$@"
6766
fi
6867
}
6968

7069
export DEBIAN_FRONTEND=noninteractive
7170

72-
check_packages curl ca-certificates gnupg2 dirmngr unzip
71+
check_packages curl ca-certificates gnupg2 dirmngr unzip bash-completion
7372

7473
verify_aws_cli_gpg_signature() {
7574
local filePath=$1
@@ -114,6 +113,17 @@ install() {
114113
unzip "${scriptZipFile}"
115114
./aws/install
116115

116+
# kubectl bash completion
117+
mkdir -p /etc/bash_completion.d
118+
cp ./scripts/vendor/aws_bash_completer /etc/bash_completion.d/aws
119+
120+
# kubectl zsh completion
121+
if [ -e "${USERHOME}/.oh-my-zsh" ]; then
122+
mkdir -p "${USERHOME}/.oh-my-zsh/completions"
123+
cp ./scripts/vendor/aws_zsh_completer.sh "${USERHOME}/.oh-my-zsh/completions/_aws"
124+
chown -R "${USERNAME}" "${USERHOME}/.oh-my-zsh"
125+
fi
126+
117127
rm -rf ./aws
118128
}
119129

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
#
7+
# Docs: https://github.com/devcontainers/features/tree/main/src/aws-cli
8+
# Maintainer: The Dev Container spec maintainers
9+
#
10+
# Run this script to replace aws_bash_completer and aws_zsh_completer.sh with the latest and greatest available version
11+
#
12+
COMPLETER_SCRIPTS=$(dirname "${BASH_SOURCE[0]}")
13+
BASH_COMPLETER_SCRIPT="$COMPLETER_SCRIPTS/vendor/aws_bash_completer"
14+
ZSH_COMPLETER_SCRIPT="$COMPLETER_SCRIPTS/vendor/aws_zsh_completer.sh"
15+
16+
wget https://raw.githubusercontent.com/aws/aws-cli/v2/bin/aws_bash_completer -O "$BASH_COMPLETER_SCRIPT"
17+
chmod +x "$BASH_COMPLETER_SCRIPT"
18+
19+
wget https://raw.githubusercontent.com/aws/aws-cli/v2/bin/aws_zsh_completer.sh -O "$ZSH_COMPLETER_SCRIPT"
20+
chmod +x "$ZSH_COMPLETER_SCRIPT"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### **IMPORTANT NOTE**
2+
3+
Scripts in this directory are sourced externally and not maintained by the Dev Container spec maintainers. Do not make changes directly as they might be overwritten at any moment.
4+
5+
## aws_bash_completer
6+
7+
`aws_bash_completer` is a copy of <https://raw.githubusercontent.com/aws/aws-cli/v2/bin/aws_bash_completer>.
8+
9+
## aws_zsh_completer.sh
10+
11+
`aws_zsh_completer.sh` is a copy of <https://raw.githubusercontent.com/aws/aws-cli/v2/bin/aws_zsh_completer.sh>.
12+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Typically that would be added under one of the following paths:
2+
# - /etc/bash_completion.d
3+
# - /usr/local/etc/bash_completion.d
4+
# - /usr/share/bash-completion/completions
5+
6+
complete -C aws_completer aws
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Source this file to activate auto completion for zsh using the bash
2+
# compatibility helper. Make sure to run `compinit` before, which should be
3+
# given usually.
4+
#
5+
# % source /path/to/zsh_complete.sh
6+
#
7+
# Typically that would be called somewhere in your .zshrc.
8+
#
9+
# Note, the overwrite of _bash_complete() is to export COMP_LINE and COMP_POINT
10+
# That is only required for zsh <= edab1d3dbe61da7efe5f1ac0e40444b2ec9b9570
11+
#
12+
# https://github.com/zsh-users/zsh/commit/edab1d3dbe61da7efe5f1ac0e40444b2ec9b9570
13+
#
14+
# zsh releases prior to that version do not export the required env variables!
15+
16+
autoload -Uz bashcompinit
17+
bashcompinit -i
18+
19+
_bash_complete() {
20+
local ret=1
21+
local -a suf matches
22+
local -x COMP_POINT COMP_CWORD
23+
local -a COMP_WORDS COMPREPLY BASH_VERSINFO
24+
local -x COMP_LINE="$words"
25+
local -A savejobstates savejobtexts
26+
27+
(( COMP_POINT = 1 + ${#${(j. .)words[1,CURRENT]}} + $#QIPREFIX + $#IPREFIX + $#PREFIX ))
28+
(( COMP_CWORD = CURRENT - 1))
29+
COMP_WORDS=( $words )
30+
BASH_VERSINFO=( 2 05b 0 1 release )
31+
32+
savejobstates=( ${(kv)jobstates} )
33+
savejobtexts=( ${(kv)jobtexts} )
34+
35+
[[ ${argv[${argv[(I)nospace]:-0}-1]} = -o ]] && suf=( -S '' )
36+
37+
matches=( ${(f)"$(compgen $@ -- ${words[CURRENT]})"} )
38+
39+
if [[ -n $matches ]]; then
40+
if [[ ${argv[${argv[(I)filenames]:-0}-1]} = -o ]]; then
41+
compset -P '*/' && matches=( ${matches##*/} )
42+
compset -S '/*' && matches=( ${matches%%/*} )
43+
compadd -Q -f "${suf[@]}" -a matches && ret=0
44+
else
45+
compadd -Q "${suf[@]}" -a matches && ret=0
46+
fi
47+
fi
48+
49+
if (( ret )); then
50+
if [[ ${argv[${argv[(I)default]:-0}-1]} = -o ]]; then
51+
_default "${suf[@]}" && ret=0
52+
elif [[ ${argv[${argv[(I)dirnames]:-0}-1]} = -o ]]; then
53+
_directories "${suf[@]}" && ret=0
54+
fi
55+
fi
56+
57+
return ret
58+
}
59+
60+
complete -C aws_completer aws
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
command=$1
4+
expected=$2
5+
6+
echo -e "Checking completion for command '$command'..."
7+
8+
# Send command as a character stream, followed by two tab characters, into an interactive bash shell.
9+
# Also note the 'y' which responds to the possible Bash question "Display all xxx possibilities? (y or n)".
10+
# Bash produces the autocompletion output on stderr, so redirect that to stdout.
11+
# The sed bit captures the lines between Header and Footer (used as output delimiters).
12+
# The first grep removes the "Display all" message (that is atomatically answered to "y" by the script).
13+
# The last grep filters the output to lines containing the expected result.
14+
COMPLETE_OUTPUT=$(echo if false\; then "Header"\; $command$'\t'$'\t'y\; "Footer" fi | bash -i 2>&1 | sed -n '/Header/{:a;n;/Footer/q;p;ba}' | grep -v ^'Display all ')
15+
echo -e "\nCompletion output:\n"
16+
echo -e "$COMPLETE_OUTPUT"
17+
echo -e "\n"
18+
19+
FILTERED_COMPLETE_OUTPUT=$(echo "$COMPLETE_OUTPUT" | grep "$expected")
20+
21+
if [ -z "$FILTERED_COMPLETE_OUTPUT" ]; then
22+
echo -e "Completion output does not contains '$expected'."
23+
exit 1
24+
else
25+
echo -e "Completion output contains '$expected'."
26+
exit 0
27+
fi

β€Žtest/aws-cli/test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@ source dev-container-features-test-lib
88
# Definition specific tests
99
check "version" aws --version
1010

11+
# By default bash complete is disabled for the root user
12+
# Enable it by replacing current ~/.bashrc with the /etc/skel/.bashrc file
13+
mv ~/.bashrc ~/.bashrc.bak
14+
cp /etc/skel/.bashrc ~/
15+
16+
check "aws-bash-completion-contains-version-option" ./checkBashCompletion.sh "aws --" "version"
17+
18+
# Restore original ~/.bashrc
19+
mv ~/.bashrc.bak ~/.bashrc
20+
1121
# Report result
1222
reportResults

0 commit comments

Comments
Β (0)