-
Notifications
You must be signed in to change notification settings - Fork 5
/
amazonrc.sh
47 lines (39 loc) · 872 Bytes
/
amazonrc.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
#!/bin/sh
s3cfg="$HOME/.s3cfg"
key='AWS_ACCESS_KEY_ID'
secret='AWS_SECRET_ACCESS_KEY'
aws_key=''
aws_secret=''
shell=''
err() {
printf "ERROR: %s" "$@"
}
get_shell() {
shell=$(basename ${SHELL:-/bin/sh})
}
get_keys() {
aws_key=$(awk -F'=' '/^access_key/ {gsub(/^[ \t]+/, "", $2); print $2}' "$s3cfg")
aws_secret=$(awk -F'=' '/^secret_key/ {gsub(/^[ \t]+/, "", $2); print $2}' "$s3cfg")
}
export_keys() {
get_keys
get_shell
if [ "${shell}" = 'fish' ]; then
printf 'set -e %s; set -x -U %s %s\n' "$key" "$key" "$aws_key"
printf 'set -e %s; set -x -U %s %s\n' "$secret" "$secret" "$aws_secret"
else
printf 'export %s=%s\n' "$key" "$aws_key"
printf 'export %s=%s\n' "$secret" "$aws_secret"
fi
}
check_s3cfg() {
if [ ! -f "$s3cfg" ]; then
err $s3cfg 'not found'
exit 78
fi
}
main() {
check_s3cfg
export_keys
}
main "$@"