make (1277B)
1 #!/bin/sh 2 3 set -e 4 5 readonly COMMIT="$(git rev-parse HEAD)" 6 readonly DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" 7 readonly LDFLAGS="-X code.dwrz.net/src/pkg/build.Commit=${COMMIT} \ 8 -X code.dwrz.net/src/pkg/build.Hostname=${HOSTNAME} \ 9 -X code.dwrz.net/src/pkg/build.Time=${DATE}" 10 11 # Disable the race detector unless RACE is set to true in the environment. 12 RACE="${RACE:-false}" 13 14 all() { 15 clean 16 17 for f in cmd/*; do 18 if [ -d "$f" ]; then 19 cmd=$(basename "$f") 20 21 build "$cmd" 22 fi 23 done 24 } 25 26 build() { 27 cmd="$1" 28 29 go build \ 30 -ldflags "${LDFLAGS}" \ 31 -o bin/"${cmd}" \ 32 -race="${RACE}" \ 33 ./cmd/"${cmd}"/*.go 34 if [[ "$?" != 0 ]]; then 35 printf "\nFailed to build %s.\n" "${cmd}" 36 exit 1 37 fi 38 } 39 40 clean() { 41 go clean 42 rm -rf bin/* 43 } 44 45 gotest() { 46 clean 47 all 48 go test ./... 49 } 50 51 52 lint() { 53 goimports -e -w -local="code.dwrz.net/src" ./cmd/ ./pkg/ 54 go fmt ./... 55 deadcode ./... 56 go vet ./... 57 govulncheck ... 58 } 59 60 run() { 61 cmd="$1" 62 63 build "${cmd}" 64 65 ./bin/"${cmd}" "${@:2}" 66 } 67 68 main() { 69 action="$1"; 70 if [ -n "${action}" ]; then 71 shift; 72 fi 73 74 case "${action}" in 75 "all"|"") all ;; 76 "build") build "$@" ;; 77 "clean") clean ;; 78 "lint") lint ;; 79 "run") run "$@" ;; 80 "test") gotest ;; 81 *) all ;; 82 esac 83 } 84 85 main "$@"