From e1bdb0c6d650cda5a759e97a876996032d313f6e Mon Sep 17 00:00:00 2001 From: Mrinal Wadhwa Date: Sat, 10 Nov 2018 15:25:27 -0800 Subject: [PATCH] build: add build script and include metalinter --- azure-pipelines.yml | 7 ++--- build | 62 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) create mode 100755 build diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 3a6741f..802d6e7 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -16,12 +16,9 @@ steps: mv !(gopath) '$(modulePath)' echo '##vso[task.prependpath]$(GOBIN)' echo '##vso[task.prependpath]$(GOROOT)/bin' + go version displayName: 'Set up the Go workspace' -- script: go build -v . +- script: ./build workingDirectory: '$(modulePath)' displayName: 'Build' - -- script: go test -v -cover ./... - workingDirectory: '$(modulePath)' - displayName: 'Run tests' diff --git a/build b/build new file mode 100755 index 0000000..1717df2 --- /dev/null +++ b/build @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +# +# This script is used build this project + +# TRACING: +# +# If the TRACE environment variable is truthy turn on tracing, +# using the bash -x option. Useful when debugging this script. +# +# TRACE=1 ./build +# +[[ "${TRACE}" ]] && set -x + +# Set Bash Options. +# +# -e option will cause the script to exit immediately when a command fails. +# +# -o pipefail option sets the exit code of a pipeline to that of the +# rightmost command to exit with a non-zero status, or zero if all commands +# of the pipeline exit successfully. +set -eo pipefail + +# Check if git is available, go is available and GOPATH is not empty +check() { + if ! [ -x "$(command -v git)" ]; then + echo 'ERROR: could not find the git command.' >&2 + exit 1 + fi + + if ! [ -x "$(command -v go)" ]; then + echo 'ERROR: could not find the go command.' >&2 + exit 1 + fi + + if [ -z "$GOPATH" ]; then + echo 'ERROR: GOPATH is empty.' >&2 + exit 1 + fi +} + +# run gometalinter, install it if not present +lint() { + # if gometalinter is not installed, install it + if [ ! -f "$GOPATH/bin/gometalinter" ]; then + go get -u github.com/alecthomas/gometalinter + gometalinter --install + fi + + gometalinter ./... +} + +# build the code +build() { + go build -v ./... +} + +# run tests with coverage analysis +test() { + go test -v -cover ./... +} + +check && lint && build && test