forked from aws-cloudformation/cloudformation-guard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-guard.sh
87 lines (74 loc) · 2.48 KB
/
install-guard.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
85
86
87
#!/bin/sh
# This scripts downloads and installs cfn-guard latest version from github releases
# It detects platforms, downloads the pre-built binary for the latest version installs
# it in the ~/.guard/$MAJOR_VER/cfn-guard-v$MAJOR_VER-$OS_TYPE-latest/cfn-guard and symlinks ~/.guard/bin
# to the latest one
main() {
if ! ( check_cmd curl || check_cmd wget ); then
err "need 'curl' or 'wget' (command not found)"
fi
need_cmd awk
need_cmd mkdir
need_cmd rm
need_cmd uname
need_cmd tar
need_cmd ln
get_os_type
get_latest_release |
while read -r MAJOR_VER; read -r VERSION; do
mkdir -p ~/.guard/"$MAJOR_VER" ~/.guard/bin ||
err "unable to make directories ~/.guard/$MAJOR_VER, ~/.guard/bin"
get_os_type
download https://github.com/aws-cloudformation/cloudformation-guard/releases/download/"$VERSION"/cfn-guard-v"$MAJOR_VER"-"$OS_TYPE"-latest.tar.gz > /tmp/guard.tar.gz ||
err "unable to download https://github.com/aws-cloudformation/cloudformation-guard/releases/download/$VERSION/cfn-guard-v$MAJOR_VER-$OS_TYPE-latest.tar.gz"
tar -C ~/.guard/"$MAJOR_VER" -xzf /tmp/guard.tar.gz ||
err "unable to untar /tmp/guard.tar.gz"
ln -sf ~/.guard/"$MAJOR_VER"/cfn-guard-v"$MAJOR_VER"-"$OS_TYPE"-latest/cfn-guard ~/.guard/bin ||
err "unable to symlink to ~/.guard/bin directory"
~/.guard/bin/cfn-guard help ||
err "cfn-guard was not installed properly"
echo "Remember to SET PATH include PATH=\${PATH}:~/.guard/bin"
done
}
get_os_type() {
_ostype="$(uname -s)"
case "$_ostype" in
Darwin)
OS_TYPE="macos"
;;
Linux)
# IS this RIGHT, we need to build for different ARCH as well.
# Need more ARCH level detections
OS_TYPE="ubuntu"
;;
*)
err "unsupported OS type $_ostype"
;;
esac
}
get_latest_release() {
download https://api.github.com/repos/aws-cloudformation/cloudformation-guard/releases/latest |
awk -F '"' '/tag_name/ { print $4 }' |
awk -F '.' '{ print $1 "\n" $0 }'
}
err() {
echo "$1" >&2
exit 1
}
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
check_cmd() {
command -v "$1" > /dev/null 2>&1
}
download()
{
if check_cmd curl; then
curl -fsSL "$1"
else
wget -qO- "$1"
fi
}
main