aws.plugin.zsh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # AWS profile selection
  2. function agp {
  3. echo $AWS_PROFILE
  4. }
  5. function asp {
  6. if [[ -z "$1" ]]; then
  7. unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_EB_PROFILE
  8. echo AWS profile cleared.
  9. return
  10. fi
  11. export AWS_DEFAULT_PROFILE=$1
  12. export AWS_PROFILE=$1
  13. export AWS_EB_PROFILE=$1
  14. }
  15. function aws_change_access_key {
  16. if [[ -z "$1" ]] then
  17. echo "usage: $0 <profile>"
  18. return 1
  19. fi
  20. echo Insert the credentials when asked.
  21. asp "$1"
  22. aws iam create-access-key
  23. aws configure --profile "$1"
  24. echo You can now safely delete the old access key running \`aws iam delete-access-key --access-key-id ID\`
  25. echo Your current keys are:
  26. aws iam list-access-keys
  27. }
  28. function aws_profiles {
  29. [[ -r "${AWS_CONFIG_FILE:-$HOME/.aws/config}" ]] || return 1
  30. reply=($(grep '\[profile' "${AWS_CONFIG_FILE:-$HOME/.aws/config}"|sed -e 's/.*profile \([a-zA-Z0-9_\.-]*\).*/\1/'))
  31. }
  32. compctl -K aws_profiles asp aws_change_access_key
  33. # AWS prompt
  34. function aws_prompt_info() {
  35. [[ -z $AWS_PROFILE ]] && return
  36. echo "${ZSH_THEME_AWS_PREFIX:=<aws:}${AWS_PROFILE}${ZSH_THEME_AWS_SUFFIX:=>}"
  37. }
  38. if [ "$SHOW_AWS_PROMPT" != false ]; then
  39. RPROMPT='$(aws_prompt_info)'"$RPROMPT"
  40. fi
  41. # Load awscli completions
  42. _awscli-homebrew-installed() {
  43. # check if Homebrew is installed
  44. (( $+commands[brew] )) || return 1
  45. # speculatively check default brew prefix
  46. if [ -h /usr/local/opt/awscli ]; then
  47. _brew_prefix=/usr/local/opt/awscli
  48. else
  49. # ok, it is not in the default prefix
  50. # this call to brew is expensive (about 400 ms), so at least let's make it only once
  51. _brew_prefix=$(brew --prefix awscli)
  52. fi
  53. }
  54. # get aws_zsh_completer.sh location from $PATH
  55. _aws_zsh_completer_path="$commands[aws_zsh_completer.sh]"
  56. # otherwise check common locations
  57. if [[ -z $_aws_zsh_completer_path ]]; then
  58. # Homebrew
  59. if _awscli-homebrew-installed; then
  60. _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh
  61. # Ubuntu
  62. elif [[ -e /usr/share/zsh/vendor-completions/_awscli ]]; then
  63. _aws_zsh_completer_path=/usr/share/zsh/vendor-completions/_awscli
  64. # RPM
  65. else
  66. _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh
  67. fi
  68. fi
  69. [[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path
  70. unset _aws_zsh_completer_path _brew_prefix