-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsm-profiles.sh
62 lines (55 loc) · 1.22 KB
/
awsm-profiles.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
#! /bin/bash
: ${AWS_CONFIG=$HOME/.aws/config}
: ${AWSM_HOME=$HOME/.awsm}
: ${AWSM_PROFILE_FILE=$AWSM_HOME/.awsm-profile}
function list-profiles {
local regex="^\[profile ([A-z_-]+)\]"
cat "$AWS_CONFIG" | \
while read LINE; do
if [[ $LINE =~ $regex ]]
then
name="${BASH_REMATCH[1]}"
local match=$(list-current-profile)
if [[ $name == "$match" ]]
then
echo "$name *"
else
echo $name
fi
fi
done
}
function list-current-profile {
local regex="^export AWS_PROFILE=([A-z_-]+)"
cat "$AWSM_PROFILE_FILE" | \
while read LINE; do
if [[ $LINE =~ $regex ]]; then
name="${BASH_REMATCH[1]}"
echo $name
fi
done
}
function select-profile {
local profile=$(list-profiles | $FUZZY_FILTER)
if [ -n "$profile" ]; then
local regex="^([a-z]+)"
if [[ $profile =~ $regex ]]; then
name="${BASH_REMATCH[1]}"
echo $name
fi
fi
}
function profile {
local profile=$(select-profile)
if [ -n "$profile" ]; then
echo "export AWS_PROFILE=$profile" > $AWSM_PROFILE_FILE
fi
}
function load_profile {
if [ -z "${AWS_PROFILE-}" ]; then
if [ -f "$AWSM_PROFILE_FILE" ]; then
source $AWSM_PROFILE_FILE
fi
fi
}
load_profile