aws.plugin.zsh 2.6 KB

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