@@ -0,0 +1,137 @@
1+#!/usr/bin/env bash
2+
3+RED='\033[0;31m'
4+GREEN='\033[0;32m'
5+BLUE='\033[0;34m'
6+NC='\033[0m'
7+
8+declare -A tools=()
9+declare -A desired=()
10+
11+for arg in "$@"; do
12+ case ${arg%%=*} in
13+ "--with-go")
14+ desired[go]="${arg##*=}"
15+ ;;
16+ "--with-dep")
17+ desired[dep]="${arg##*=}"
18+ ;;
19+ "--help")
20+ printf "${GREEN}$0${NC}\n"
21+ printf " available options:\n"
22+ printf " --with-dep=${BLUE}<path_to_dep_binary>${NC}\n"
23+ printf " --with-go=${BLUE}<path_to_go_binary>${NC}\n"
24+ exit 0
25+ ;;
26+ *)
27+ echo "Unknown option: $arg"
28+ exit 2
29+ ;;
30+ esac
31+done
32+
33+vercomp () {
34+ if [[ $1 == $2 ]]
35+ then
36+ return 0
37+ fi
38+ local IFS=.
39+ local i ver1=($1) ver2=($2)
40+ # fill empty fields in ver1 with zeros
41+ for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
42+ do
43+ ver1[i]=0
44+ done
45+ for ((i=0; i<${#ver1[@]}; i++))
46+ do
47+ if [[ -z ${ver2[i]} ]]
48+ then
49+ # fill empty fields in ver2 with zeros
50+ ver2[i]=0
51+ fi
52+ if ((10#${ver1[i]} > 10#${ver2[i]}))
53+ then
54+ return 1
55+ fi
56+ if ((10#${ver1[i]} < 10#${ver2[i]}))
57+ then
58+ return 2
59+ fi
60+ done
61+ return 0
62+}
63+
64+check_for() {
65+ echo -n "Checking for $1... "
66+ if ! [ -z "${desired[$1]}" ]; then
67+ TOOL_PATH="${desired[$1]}"
68+ else
69+ TOOL_PATH=$(command -v $1)
70+ fi
71+ if ! [ -x "$TOOL_PATH" -a -f "$TOOL_PATH" ]; then
72+ printf "${RED}not found${NC}\n"
73+ cd -
74+ exit 1
75+ else
76+ printf "${GREEN}found${NC}\n"
77+ tools[$1]=$TOOL_PATH
78+ fi
79+}
80+
81+check_go_version() {
82+ echo -n "Checking go version... "
83+ GO_VERSION=$(${tools[go]} version | ${tools[awk]} '{where = match($0, /[0-9]\.[0-9]+\.[0-9]*/); if (where != 0) print substr($0, RSTART, RLENGTH)}')
84+ vercomp $GO_VERSION 1.10
85+ case $? in
86+ 0) ;&
87+ 1)
88+ printf "${GREEN}"
89+ echo $GO_VERSION
90+ printf "${NC}"
91+ ;;
92+ 2)
93+ printf "${RED}"
94+ echo "$GO_VERSION < 1.10"
95+ exit 1
96+ ;;
97+ esac
98+}
99+
100+check_docker_version() {
101+ echo -n "Checking docker version... "
102+ DOCKER_VERSION=$(${tools[docker]} version | ${tools[awk]})
103+}
104+
105+check_go_env() {
106+ echo -n "Checking \$GOPATH... "
107+ if [ -z "$GOPATH" ]; then
108+ printf "${RED}invalid${NC} - GOPATH not set\n"
109+ exit 1
110+ fi
111+ printf "${GREEN}valid${NC} - $GOPATH\n"
112+}
113+
114+cd ${0%/*}
115+
116+if [ ! -f .env ]; then
117+ rm .env
118+fi
119+
120+check_for make
121+check_for awk
122+check_for go
123+check_go_version
124+check_go_env
125+check_for dep
126+
127+echo
128+
129+cat <<- EOF > .env
130+ MAKE := "${tools[make]}"
131+ GO := "${tools[go]}"
132+ DEP := "${tools[dep]}"
133+EOF
134+
135+echo "Environment configuration written to .env"
136+
137+cd - > /dev/null