aws.plugin.zsh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 iam create-access-key
  30. 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 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. function _awscli-homebrew-installed() {
  53. # check if Homebrew is installed
  54. (( $+commands[brew] )) || return 1
  55. # speculatively check default brew prefix
  56. if [ -h /usr/local/opt/awscli ]; then
  57. _brew_prefix=/usr/local/opt/awscli
  58. else
  59. # ok, it is not in the default prefix
  60. # this call to brew is expensive (about 400 ms), so at least let's make it only once
  61. _brew_prefix=$(brew --prefix awscli)
  62. fi
  63. }
  64. # get aws_zsh_completer.sh location from $PATH
  65. _aws_zsh_completer_path="$commands[aws_zsh_completer.sh]"
  66. # otherwise check common locations
  67. if [[ -z $_aws_zsh_completer_path ]]; then
  68. # Homebrew
  69. if _awscli-homebrew-installed; then
  70. _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh
  71. # Ubuntu
  72. elif [[ -e /usr/share/zsh/vendor-completions/_awscli ]]; then
  73. _aws_zsh_completer_path=/usr/share/zsh/vendor-completions/_awscli
  74. # RPM
  75. else
  76. _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh
  77. fi
  78. fi
  79. [[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path
  80. unset _aws_zsh_completer_path _brew_prefix