aws.plugin.zsh 3.2 KB

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