aws.plugin.zsh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. reply=($(grep '\[profile' "${AWS_CONFIG_FILE:-$HOME/.aws/config}"|sed -e 's/.*profile \([a-zA-Z0-9_\.-]*\).*/\1/'))
  30. }
  31. compctl -K aws_profiles asp aws_change_access_key
  32. # AWS prompt
  33. function aws_prompt_info() {
  34. [[ -z $AWS_PROFILE ]] && return
  35. echo "${ZSH_THEME_AWS_PREFIX:=<aws:}${AWS_PROFILE}${ZSH_THEME_AWS_SUFFIX:=>}"
  36. }
  37. if [ "$SHOW_AWS_PROMPT" != false ]; then
  38. RPROMPT='$(aws_prompt_info)'"$RPROMPT"
  39. fi
  40. # Load awscli completions
  41. _awscli-homebrew-installed() {
  42. # check if Homebrew is installed
  43. (( $+commands[brew] )) || return 1
  44. # speculatively check default brew prefix
  45. if [ -h /usr/local/opt/awscli ]; then
  46. _brew_prefix=/usr/local/opt/awscli
  47. else
  48. # ok, it is not in the default prefix
  49. # this call to brew is expensive (about 400 ms), so at least let's make it only once
  50. _brew_prefix=$(brew --prefix awscli)
  51. fi
  52. }
  53. # get aws_zsh_completer.sh location from $PATH
  54. _aws_zsh_completer_path="$commands[aws_zsh_completer.sh]"
  55. # otherwise check common locations
  56. if [[ -z $_aws_zsh_completer_path ]]; then
  57. # Homebrew
  58. if _awscli-homebrew-installed; then
  59. _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh
  60. # Ubuntu
  61. elif [[ -e /usr/share/zsh/vendor-completions/_awscli ]]; then
  62. _aws_zsh_completer_path=/usr/share/zsh/vendor-completions/_awscli
  63. # RPM
  64. else
  65. _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh
  66. fi
  67. fi
  68. [[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path
  69. unset _aws_zsh_completer_path _brew_prefix