forked from rkalis/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathut_aws.sh
executable file
·84 lines (79 loc) · 2.45 KB
/
ut_aws.sh
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
#!/usr/bin/env zsh
ZSH_CONFIG_FOLDER="$HOME/.zshrc.d"
ZSH_DEFAULT_CONFIG_FILE="$HOME/.zshrc"
AWS_SCRIPT_PATH="$HOME/.zshrc.d/aws.sh"
# Check if you have zsh
if ! command -v zsh &> /dev/null
then
echo "You need zsh for this script to work"
exit 1
fi
# Check to see if ~/.zshrc.d folder exists
if [ -d "$ZSH_CONFIG_FOLDER" ]; then
echo "$ZSH_CONFIG_FOLDER exists."
else
# Create the folder if it doesn't
echo "$ZSH_CONFIG_FOLDER does not exist. Creating..."
mkdir $ZSH_CONFIG_FOLDER
fi
# Check to see if aws.sh file exists
if [ -f "$AWS_SCRIPT_PATH" ]; then
echo "$AWS_SCRIPT_PATH already exists."
else
# Create the file if it doesn't
echo "$AWS_SCRIPT_PATH does not exist. Creating..."
tee $AWS_SCRIPT_PATH <<\EOF > /dev/null
# A helper function to set your AWS Profile for proper execution context-switching
#
# usage: a <aws_profile>
#
# Alias a() to get or set current aws profile
a() {
NEWPROFILE=$1
MYPROFILES=("${(@f)$(aws configure list-profiles)}")
if [[ ${NEWPROFILE} == "--list" ]]; then
if [[ -n "${AWS_DEFAULT_PROFILE}" ]]; then
echo "Current profile is: ${AWS_DEFAULT_PROFILE}"
return
else
echo "No profile set"
return
fi
elif [[ -z ${NEWPROFILE} ]]; then
echo -e "No profile passed. Please use one of...\n\t ${MYPROFILES[*]}"
return
fi
# Check to see if the requested profile exists and change to it
if [[ "${MYPROFILES[*]}" =~ ${NEWPROFILE} ]]; then
echo "changing aws profile to: ${NEWPROFILE}"
export AWS_DEFAULT_PROFILE=${NEWPROFILE}
else
echo -e "Profile: \`${NEWPROFILE}\` doesn't exist, please use one of...\n\t${MYPROFILES[*]}"
fi
}
autoload bashcompinit && bashcompinit
autoload -Uz compinit && compinit
compinit
# completion stuff for `a` command
_acomp() {
# this is comps function for a
local profiles
profiles=("${(@f)$(aws configure list-profiles)}")
_arguments '--list'
compadd ${profiles[@]}
}
compdef _acomp a
EOF
fi
# Check to see if ~/.zshrc file exists
if [ -f "$ZSH_DEFAULT_CONFIG_FILE" ]; then
# Add source command to .zshrc
if ! grep -q "source $AWS_SCRIPT_PATH" "$ZSH_DEFAULT_CONFIG_FILE"; then
echo "source $AWS_SCRIPT_PATH" >> $ZSH_DEFAULT_CONFIG_FILE
fi
echo "Success. Please open a new terminal window or run 'source ~/.zshrc' to use the 'a' command"
exit 0
else
echo "$ZSH_DEFAULT_CONFIG_FILE doesn't exist. I'm not sure what to do. Change ZSH_DEFAULT_CONFIG_FILE variable"
exit 1
fi