Skip to content

Instantly share code, notes, and snippets.

@vandot
Created October 14, 2016 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vandot/e9815b1581d0dfc01f4eec9b9a0b07da to your computer and use it in GitHub Desktop.
Save vandot/e9815b1581d0dfc01f4eec9b9a0b07da to your computer and use it in GitHub Desktop.
Bash completion script for curl for exploring internal instance (169.254.169.254) API on AWS and GCP
# curl metadata api explorer -*- shell-script -*-
# you can copy paste it inside ~/.bash_completion
_curl_api()
{
local cur prev word cword
_init_completion -n : || return
case "$cur" in
http://169.254.169.254/*)
POS=$((`echo $cur | grep -aob '/' | grep -oE '^[0-9]+' | tail -n1` + 1 ))
_metadata=`curl -f -s "${cur:0:$POS}"`
if [[ ! -z "$_metadata" ]]; then
args=""
for i in $_metadata; do
if [[ "${i: -1}" != "/" && -z `curl -f -s "${cur:0:$POS}$i"` ]]; then i=$i"/"; fi
args="$args ${cur:0:$POS}$i"
done
args="${args:1}"
COMPREPLY=( $( compgen -W "$args" -- "$cur" ) )
# Work-around bash_completion issue where bash interprets a colon as a separator.
# Work-around borrowed from the darcs work-around for the same issue.
local colonprefixes=${cur%"${cur##*:}"}
local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do
COMPREPLY[$i]=${COMPREPLY[$i]#"$colonprefixes"}
done
compopt -o nospace
fi
return
;;
h*)
COMPREPLY=( $( compgen -W 'http://169.254.169.254/' -- "$cur" ) )
# Work-around bash_completion issue where bash interprets a colon as a separator.
# Work-around borrowed from the darcs work-around for the same issue.
local colonprefixes=${cur%"${cur##*:}"}
local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do
COMPREPLY[$i]=${COMPREPLY[$i]#"$colonprefixes"}
done
compopt -o nospace
return
;;
esac
} &&
complete -F _curl_api curl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment