s3 autocomplete
My new team makes heavy use of s3, and rather than always need to open the browser to search around for a file and lose my flow in the terminal I decided to add an autocomplete function for the aws s3
command. Check it out:
function join_by { local IFS="$1"; shift; echo "$*"; }
function _complete_s_three()
{
BASE="s3://<company bucket>"
PROFILE="--profile readonly"
if [[ $COMP_CWORD -gt 1 ]]; then
RANGE=$(join_by / ${COMP_WORDS[@]:1:$COMP_CWORD - 1})
COMP=$(aws s3 ls "${BASE}/${RANGE}/" ${PROFILE} | awk '{print $NF}' | tr '\n' ' ' | sed 's|/||g')
else
COMP=$(aws s3 ls "${BASE}/" ${PROFILE} | awk '{print $NF}' | tr '\n' ' ' | sed 's|/||g')
fi
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W "${COMP}" -- ${cur}))
return 0
}
function s3d() {
BASE="s3://<company bucket>"
PROFILE="--profile readonly"
if [[ $# -eq 6 ]]; then
aws s3 cp "${BASE}/${1}/${2}/${3}/${4}/${5}/${6}" "${6}" ${PROFILE}
else
echo "error: not enough arguments"
fi
}
complete -F _complete_s_three s3d
NOTE: This script assumes that all files are nested exactly five levels down, but this could easily be changed to support any depth of file nesting.