From 6123e41230425fc830c5e190005f91e88309efc2 Mon Sep 17 00:00:00 2001 From: Ruben Suarez Alvarez Date: Wed, 28 Aug 2024 02:00:05 +0200 Subject: [PATCH] =?UTF-8?q?feat(aws-cli):=20=F0=9F=A7=91=E2=80=8D?= =?UTF-8?q?=F0=9F=92=BB=20add=20aws-cli=20completion=20(#1083)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .../update-aws-cli-completer-scripts.yml | 58 ++++++++++++++++++ src/aws-cli/devcontainer-feature.json | 2 +- src/aws-cli/install.sh | 24 +++++--- .../scripts/fetch-latest-completer-scripts.sh | 20 +++++++ src/aws-cli/scripts/vendor/README.md | 12 ++++ src/aws-cli/scripts/vendor/aws_bash_completer | 6 ++ .../scripts/vendor/aws_zsh_completer.sh | 60 +++++++++++++++++++ test/aws-cli/checkBashCompletion.sh | 27 +++++++++ test/aws-cli/test.sh | 10 ++++ 9 files changed, 211 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/update-aws-cli-completer-scripts.yml create mode 100755 src/aws-cli/scripts/fetch-latest-completer-scripts.sh create mode 100644 src/aws-cli/scripts/vendor/README.md create mode 100755 src/aws-cli/scripts/vendor/aws_bash_completer create mode 100755 src/aws-cli/scripts/vendor/aws_zsh_completer.sh create mode 100755 test/aws-cli/checkBashCompletion.sh diff --git a/.github/workflows/update-aws-cli-completer-scripts.yml b/.github/workflows/update-aws-cli-completer-scripts.yml new file mode 100644 index 000000000..b46d43f39 --- /dev/null +++ b/.github/workflows/update-aws-cli-completer-scripts.yml @@ -0,0 +1,58 @@ +name: "Updates vendor 'aws_bash_completer' and 'aws_zsh_completer.sh' scripts" +on: + workflow_dispatch: + schedule: + - cron: '0 0 * * 0' # Runs every Sunday at midnight UTC (adjust as needed) + +jobs: + fetch-latest-aws-completer-scripts: + runs-on: ubuntu-latest + environment: documentation # grants access to secrets.PAT, for creating pull requests + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@v3 + + - name: Run fetch-latest-completer-scripts.sh + run: src/aws-cli/scripts/fetch-latest-completer-scripts.sh + + - name: Create a PR for completer scripts + id: push_image_info + env: + GITHUB_TOKEN: ${{ secrets.PAT }} + run: | + set -e + echo "Start." + + # Configure git and Push updates + git config --global user.email github-actions@github.com + git config --global user.name github-actions + git config pull.rebase false + + branch=automated-script-update-$GITHUB_RUN_ID + git checkout -b $branch + message='[Updates] Automated vendor 'aws-cli' completer scripts' + + # Add / update and commit + git add src/aws-cli/scripts/vendor/aws_bash_completer + git add src/aws-cli/scripts/vendor/aws_zsh_completer.sh + + git commit -m 'Automated completer scripts update' || export NO_UPDATES=true + + # Bump version and push + if [ "$NO_UPDATES" != "true" ] ; then + echo "$(jq --indent 4 '.version = (.version | split(".") | map(tonumber) | .[2] += 1 | join("."))' src/aws-cli/devcontainer-feature.json)" > src/aws-cli/devcontainer-feature.json + git add src/aws-cli/devcontainer-feature.json + + git commit -m 'Bump version' + git push origin "$branch" + gh api \ + --method POST \ + -H "Accept: application/vnd.github+json" \ + /repos/${GITHUB_REPOSITORY}/pulls \ + -f title="$message" \ + -f body="$message" \ + -f head="$branch" \ + -f base="$GITHUB_REF_NAME" + fi diff --git a/src/aws-cli/devcontainer-feature.json b/src/aws-cli/devcontainer-feature.json index 35a035917..3c3128ac3 100644 --- a/src/aws-cli/devcontainer-feature.json +++ b/src/aws-cli/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "aws-cli", - "version": "1.0.7", + "version": "1.1.0", "name": "AWS CLI", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/aws-cli", "description": "Installs the AWS CLI along with needed dependencies. Useful for base Dockerfiles that often are missing required install dependencies like gpg.", diff --git a/src/aws-cli/install.sh b/src/aws-cli/install.sh index d802400ef..e7c03f728 100755 --- a/src/aws-cli/install.sh +++ b/src/aws-cli/install.sh @@ -52,24 +52,23 @@ fi apt_get_update() { - echo "Running apt-get update..." - apt-get update -y + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then + echo "Running apt-get update..." + apt-get update -y + fi } # Checks if packages are installed and installs them if not check_packages() { if ! dpkg -s "$@" > /dev/null 2>&1; then - if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then - echo "Running apt-get update..." - apt-get update -y - fi + apt_get_update apt-get -y install --no-install-recommends "$@" fi } export DEBIAN_FRONTEND=noninteractive -check_packages curl ca-certificates gnupg2 dirmngr unzip +check_packages curl ca-certificates gnupg2 dirmngr unzip bash-completion verify_aws_cli_gpg_signature() { local filePath=$1 @@ -114,6 +113,17 @@ install() { unzip "${scriptZipFile}" ./aws/install + # kubectl bash completion + mkdir -p /etc/bash_completion.d + cp ./scripts/vendor/aws_bash_completer /etc/bash_completion.d/aws + + # kubectl zsh completion + if [ -e "${USERHOME}/.oh-my-zsh" ]; then + mkdir -p "${USERHOME}/.oh-my-zsh/completions" + cp ./scripts/vendor/aws_zsh_completer.sh "${USERHOME}/.oh-my-zsh/completions/_aws" + chown -R "${USERNAME}" "${USERHOME}/.oh-my-zsh" + fi + rm -rf ./aws } diff --git a/src/aws-cli/scripts/fetch-latest-completer-scripts.sh b/src/aws-cli/scripts/fetch-latest-completer-scripts.sh new file mode 100755 index 000000000..47dc6ee1d --- /dev/null +++ b/src/aws-cli/scripts/fetch-latest-completer-scripts.sh @@ -0,0 +1,20 @@ +#!/bin/bash +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- +# +# Docs: https://github.com/devcontainers/features/tree/main/src/aws-cli +# Maintainer: The Dev Container spec maintainers +# +# Run this script to replace aws_bash_completer and aws_zsh_completer.sh with the latest and greatest available version +# +COMPLETER_SCRIPTS=$(dirname "${BASH_SOURCE[0]}") +BASH_COMPLETER_SCRIPT="$COMPLETER_SCRIPTS/vendor/aws_bash_completer" +ZSH_COMPLETER_SCRIPT="$COMPLETER_SCRIPTS/vendor/aws_zsh_completer.sh" + +wget https://raw.githubusercontent.com/aws/aws-cli/v2/bin/aws_bash_completer -O "$BASH_COMPLETER_SCRIPT" +chmod +x "$BASH_COMPLETER_SCRIPT" + +wget https://raw.githubusercontent.com/aws/aws-cli/v2/bin/aws_zsh_completer.sh -O "$ZSH_COMPLETER_SCRIPT" +chmod +x "$ZSH_COMPLETER_SCRIPT" diff --git a/src/aws-cli/scripts/vendor/README.md b/src/aws-cli/scripts/vendor/README.md new file mode 100644 index 000000000..1f3838d46 --- /dev/null +++ b/src/aws-cli/scripts/vendor/README.md @@ -0,0 +1,12 @@ +### **IMPORTANT NOTE** + +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. + +## aws_bash_completer + +`aws_bash_completer` is a copy of . + +## aws_zsh_completer.sh + +`aws_zsh_completer.sh` is a copy of . + diff --git a/src/aws-cli/scripts/vendor/aws_bash_completer b/src/aws-cli/scripts/vendor/aws_bash_completer new file mode 100755 index 000000000..32cd1dd8f --- /dev/null +++ b/src/aws-cli/scripts/vendor/aws_bash_completer @@ -0,0 +1,6 @@ +# Typically that would be added under one of the following paths: +# - /etc/bash_completion.d +# - /usr/local/etc/bash_completion.d +# - /usr/share/bash-completion/completions + +complete -C aws_completer aws diff --git a/src/aws-cli/scripts/vendor/aws_zsh_completer.sh b/src/aws-cli/scripts/vendor/aws_zsh_completer.sh new file mode 100755 index 000000000..c1b2c1244 --- /dev/null +++ b/src/aws-cli/scripts/vendor/aws_zsh_completer.sh @@ -0,0 +1,60 @@ +# Source this file to activate auto completion for zsh using the bash +# compatibility helper. Make sure to run `compinit` before, which should be +# given usually. +# +# % source /path/to/zsh_complete.sh +# +# Typically that would be called somewhere in your .zshrc. +# +# Note, the overwrite of _bash_complete() is to export COMP_LINE and COMP_POINT +# That is only required for zsh <= edab1d3dbe61da7efe5f1ac0e40444b2ec9b9570 +# +# https://github.com/zsh-users/zsh/commit/edab1d3dbe61da7efe5f1ac0e40444b2ec9b9570 +# +# zsh releases prior to that version do not export the required env variables! + +autoload -Uz bashcompinit +bashcompinit -i + +_bash_complete() { + local ret=1 + local -a suf matches + local -x COMP_POINT COMP_CWORD + local -a COMP_WORDS COMPREPLY BASH_VERSINFO + local -x COMP_LINE="$words" + local -A savejobstates savejobtexts + + (( COMP_POINT = 1 + ${#${(j. .)words[1,CURRENT]}} + $#QIPREFIX + $#IPREFIX + $#PREFIX )) + (( COMP_CWORD = CURRENT - 1)) + COMP_WORDS=( $words ) + BASH_VERSINFO=( 2 05b 0 1 release ) + + savejobstates=( ${(kv)jobstates} ) + savejobtexts=( ${(kv)jobtexts} ) + + [[ ${argv[${argv[(I)nospace]:-0}-1]} = -o ]] && suf=( -S '' ) + + matches=( ${(f)"$(compgen $@ -- ${words[CURRENT]})"} ) + + if [[ -n $matches ]]; then + if [[ ${argv[${argv[(I)filenames]:-0}-1]} = -o ]]; then + compset -P '*/' && matches=( ${matches##*/} ) + compset -S '/*' && matches=( ${matches%%/*} ) + compadd -Q -f "${suf[@]}" -a matches && ret=0 + else + compadd -Q "${suf[@]}" -a matches && ret=0 + fi + fi + + if (( ret )); then + if [[ ${argv[${argv[(I)default]:-0}-1]} = -o ]]; then + _default "${suf[@]}" && ret=0 + elif [[ ${argv[${argv[(I)dirnames]:-0}-1]} = -o ]]; then + _directories "${suf[@]}" && ret=0 + fi + fi + + return ret +} + +complete -C aws_completer aws diff --git a/test/aws-cli/checkBashCompletion.sh b/test/aws-cli/checkBashCompletion.sh new file mode 100755 index 000000000..376461b52 --- /dev/null +++ b/test/aws-cli/checkBashCompletion.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +command=$1 +expected=$2 + +echo -e "Checking completion for command '$command'..." + +# Send command as a character stream, followed by two tab characters, into an interactive bash shell. +# Also note the 'y' which responds to the possible Bash question "Display all xxx possibilities? (y or n)". +# Bash produces the autocompletion output on stderr, so redirect that to stdout. +# The sed bit captures the lines between Header and Footer (used as output delimiters). +# The first grep removes the "Display all" message (that is atomatically answered to "y" by the script). +# The last grep filters the output to lines containing the expected result. +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 ') +echo -e "\nCompletion output:\n" +echo -e "$COMPLETE_OUTPUT" +echo -e "\n" + +FILTERED_COMPLETE_OUTPUT=$(echo "$COMPLETE_OUTPUT" | grep "$expected") + +if [ -z "$FILTERED_COMPLETE_OUTPUT" ]; then + echo -e "Completion output does not contains '$expected'." + exit 1 +else + echo -e "Completion output contains '$expected'." + exit 0 +fi diff --git a/test/aws-cli/test.sh b/test/aws-cli/test.sh index d213eac38..45ef63db1 100755 --- a/test/aws-cli/test.sh +++ b/test/aws-cli/test.sh @@ -8,5 +8,15 @@ source dev-container-features-test-lib # Definition specific tests check "version" aws --version +# By default bash complete is disabled for the root user +# Enable it by replacing current ~/.bashrc with the /etc/skel/.bashrc file +mv ~/.bashrc ~/.bashrc.bak +cp /etc/skel/.bashrc ~/ + +check "aws-bash-completion-contains-version-option" ./checkBashCompletion.sh "aws --" "version" + +# Restore original ~/.bashrc +mv ~/.bashrc.bak ~/.bashrc + # Report result reportResults \ No newline at end of file