|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +if (( ${BASH_VERSION:0:1} < 4 )); then |
| 4 | + echo "This configure script requires bash 4" |
| 5 | + exit 1 |
| 6 | +fi |
| 7 | + |
| 8 | +RED='\033[0;31m' |
| 9 | +GREEN='\033[0;32m' |
| 10 | +BLUE='\033[0;34m' |
| 11 | +NC='\033[0m' |
| 12 | + |
| 13 | +declare -A tools=() |
| 14 | + |
| 15 | +vercomp () { |
| 16 | + if [[ $1 == $2 ]] |
| 17 | + then |
| 18 | + return 0 |
| 19 | + fi |
| 20 | + local IFS=. |
| 21 | + local i ver1=($1) ver2=($2) |
| 22 | + # fill empty fields in ver1 with zeros |
| 23 | + for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) |
| 24 | + do |
| 25 | + ver1[i]=0 |
| 26 | + done |
| 27 | + for ((i=0; i<${#ver1[@]}; i++)) |
| 28 | + do |
| 29 | + if [[ -z ${ver2[i]} ]] |
| 30 | + then |
| 31 | + # fill empty fields in ver2 with zeros |
| 32 | + ver2[i]=0 |
| 33 | + fi |
| 34 | + if ((10#${ver1[i]} > 10#${ver2[i]})) |
| 35 | + then |
| 36 | + return 1 |
| 37 | + fi |
| 38 | + if ((10#${ver1[i]} < 10#${ver2[i]})) |
| 39 | + then |
| 40 | + return 2 |
| 41 | + fi |
| 42 | + done |
| 43 | + return 0 |
| 44 | +} |
| 45 | + |
| 46 | +check_for() { |
| 47 | + echo -n "Checking for $1... " |
| 48 | + TOOL_PATH=$(command -v $1) |
| 49 | + if ! [ -x "$TOOL_PATH" -a -f "$TOOL_PATH" ]; then |
| 50 | + printf "${RED}not found${NC}\n" |
| 51 | + cd - > /dev/null |
| 52 | + exit 1 |
| 53 | + else |
| 54 | + printf "${GREEN}found${NC}\n" |
| 55 | + tools[$1]=$TOOL_PATH |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +check_go_env() { |
| 60 | + echo -n "Checking \$GOPATH... " |
| 61 | + if [ -z "$GOPATH" ]; then |
| 62 | + printf "${RED}invalid${NC} - GOPATH not set\n" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + printf "${GREEN}valid${NC} - $GOPATH\n" |
| 66 | +} |
| 67 | + |
| 68 | +check_go_version() { |
| 69 | + echo -n "Checking go version... " |
| 70 | + GO_VERSION=$(${tools[go]} version | ${tools[awk]} '{where = match($0, /[0-9]\.[0-9]+[\.0-9]*/); if (where != 0) print substr($0, RSTART, RLENGTH)}') |
| 71 | + vercomp $GO_VERSION 1.10 |
| 72 | + case $? in |
| 73 | + 0) ;& |
| 74 | + 1) |
| 75 | + printf "${GREEN}" |
| 76 | + echo $GO_VERSION |
| 77 | + printf "${NC}" |
| 78 | + ;; |
| 79 | + 2) |
| 80 | + printf "${RED}" |
| 81 | + echo "$GO_VERSION < 1.10" |
| 82 | + exit 1 |
| 83 | + ;; |
| 84 | + esac |
| 85 | +} |
| 86 | + |
| 87 | +cd ${0%/*} |
| 88 | + |
| 89 | +check_for make |
| 90 | +check_for awk |
| 91 | +check_for go |
| 92 | +check_for dep |
| 93 | +check_for golangci-lint |
| 94 | +check_go_env |
| 95 | +check_go_version |
| 96 | + |
| 97 | +cat <<- EOF > .env |
| 98 | +MAKE := ${tools[make]} |
| 99 | +SHASUM := ${tools[shasum]} |
| 100 | +GO := ${tools[go]} |
| 101 | +GOVERSION := $GO_VERSION |
| 102 | +DEP := ${tools[dep]} |
| 103 | +LINTER := ${tools[golangci-lint]} |
| 104 | +EOF |
| 105 | + |
| 106 | +echo "Environment configuration written to .env" |
0 commit comments