-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshell-aws-autoprofile.plugin.zsh
84 lines (75 loc) · 2.5 KB
/
shell-aws-autoprofile.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
### Thanks to @laggardkernel for his code for CHPWD in BASH. https://gist.github.com/laggardkernel/6cb4e1664574212b125fbfd115fe90a4
# create a PROPMT_COMMAND equivalent to store chpwd functions
CHPWD_COMMAND=""
_chpwd_hook() {
shopt -s nullglob
local f
# run commands in CHPWD_COMMAND variable on dir change
if [[ "$PREVPWD" != "$PWD" ]]; then
local IFS=$';'
for f in $CHPWD_COMMAND; do
"$f"
done
unset IFS
fi
# refresh last working dir record
export PREVPWD="$PWD"
}
# add `;` after _chpwd_hook if PROMPT_COMMAND is not empty
PROMPT_COMMAND="_chpwd_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
awsprofile_find_up() {
local path_
path_="${PWD}"
while [ "${path_}" != "" ] && [ ! -f "${path_}/${1-}" ]; do
path_=${path_%/*}
done
echo "${path_}"
}
awsprofile_find_config() {
local dir
dir="$(awsprofile_find_up '.awsprofile')"
if [ -e "${dir}/.awsprofile" ]; then
echo "${dir}/.awsprofile"
else
if [ -e "${HOME}/.awsprofile" ]; then
echo "${HOME}/.awsprofile"
else
echo "No AWS profile found."
fi
fi
}
awsprofile_config_profile() {
export AWSPROFILE_CONFIG_PROFILE=''
export AWSREGION_CONFIG_REGION=''
local AWSPROFILE_CONFIG_PATH
AWSPROFILE_CONFIG_PATH="$(awsprofile_find_config)"
if [ ! -e "${AWSPROFILE_CONFIG_PATH}" ]; then
echo "No .awsprofile file found"
return 1
fi
AWSPROFILE_CONFIG_PROFILE="$(command sed -n 1p "${AWSPROFILE_CONFIG_PATH}" | command tr -d '\r')" || command printf ''
AWSREGION_CONFIG_REGION="$(command sed -n 2p "${AWSPROFILE_CONFIG_PATH}" | command tr -d '\r')" || command printf ''
if grep -q none "$AWSPROFILE_CONFIG_PATH"; then
if [[ -z "${AWSPROFILE_IGNORE_EXPLICIT_NONE_PROFILE}"] && ["$AWSPROFILE_IGNORE_EXPLICIT_NONE_PROFILE" = true ]]; then
echo "explicit 'none' profile found in .awsprofile file, unsetting profile"
fi
AWSPROFILE_CONFIG_PROFILE=''
elif [ -z "${AWSPROFILE_CONFIG_PROFILE}" ]; then
echo "Warning: empty .awsprofile file found at \"${AWSPROFILE_CONFIG_PATH}\""
return 2
fi
export AWS_PROFILE="${AWSPROFILE_CONFIG_PROFILE}"
if [ -n "${AWSREGION_CONFIG_REGION}" ]; then
export AWS_REGION="${AWSREGION_CONFIG_REGION}"
fi
}
if [ "${0:${#0}-4:4}" = "bash" ]; then
CHPWD_COMMAND="${CHPWD_COMMAND:+$CHPWD_COMMAND;}awsprofile_config_profile"
elif [ "${0[-3,-1]}" = "zsh" ]; then
if [ "${ZSH[-9,-1]}" = "oh-my-zsh" ]; then
chpwd_functions+=(awsprofile_config_profile)
else
add-zsh-hook chpwd awsprofile_config_profile
fi
fi
awsprofile_config_profile