diff --git a/.gitignore b/.gitignore index 407f51d..15cec27 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +/_output image-inspector diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b20e9db --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: go + +go: + - 1.5.3 + - 1.6 + +script: + - make verify test-unit + +notifications: + irc: "chat.freenode.net#openshift-dev" + +sudo: false diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ffb550e --- /dev/null +++ b/Makefile @@ -0,0 +1,51 @@ +# Old-skool build tools. +# +# Targets (see each target for more information): +# all: Build code. +# build: Build code. +# test-unit: Run unit tests. +# clean: Clean up. + +OUT_DIR = _output +OUT_PKG_DIR = Godeps/_workspace/pkg + +# Build code. +# +# Example: +# make +# make all +all build: + hack/build-go.sh +.PHONY: all build + +# Remove all build artifacts. +# +# Example: +# make clean +clean: + rm -rf $(OUT_DIR) $(OUT_PKG_DIR) +.PHONY: clean + +# Verify code conventions are properly setup. +# +# Example: +# make verify +verify: build + hack/verify-gofmt.sh +.PHONY: verify + +# Run unit tests. +# +# Args: +# WHAT: Directory names to test. All *_test.go files under these +# directories will be run. If not specified, "everything" will be tested. +# TESTS: Same as WHAT. +# GOFLAGS: Extra flags to pass to 'go' when building. +# TESTFLAGS: Extra flags that should only be passed to hack/test-go.sh +# +# Example: +# make test-unit +# make test-unit WHAT=pkg/build GOFLAGS=-v +test-unit: + GOTEST_FLAGS="$(TESTFLAGS)" hack/test-go.sh $(WHAT) $(TESTS) +.PHONY: test-unit diff --git a/hack/build-go.sh b/hack/build-go.sh new file mode 100755 index 0000000..dbdb317 --- /dev/null +++ b/hack/build-go.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# This script sets up a go workspace locally and builds all go components. + +set -o errexit +set -o nounset +set -o pipefail + +STARTTIME=$(date +%s) +CODE_ROOT=$(dirname "${BASH_SOURCE}")/.. +source "${CODE_ROOT}/hack/util.sh" +source "${CODE_ROOT}/hack/common.sh" +ii::log::install_errexit + +ii::build::build_binaries "$@" + +ret=$?; ENDTIME=$(date +%s); echo "$0 took $(($ENDTIME - $STARTTIME)) seconds"; exit "$ret" diff --git a/hack/common.sh b/hack/common.sh new file mode 100755 index 0000000..9bc2990 --- /dev/null +++ b/hack/common.sh @@ -0,0 +1,85 @@ +#!/bin/bash + +# The root of the build/dist directory +readonly II_ROOT=$( + unset CDPATH + ii_root=$(dirname "${BASH_SOURCE}")/.. + + cd "${ii_root}" + ii_root=`pwd` + if [ -h "${ii_root}" ]; then + readlink "${ii_root}" + else + pwd + fi +) + +readonly II_GOPATH=$( + unset CDPATH + cd ${II_ROOT}/../../../.. + pwd +) + +readonly II_GO_PACKAGE=github.com/simon3z/image-inspector +readonly II_OUTPUT_SUBPATH="${II_OUTPUT_SUBPATH:-_output/local}" +readonly II_OUTPUT="${II_ROOT}/${II_OUTPUT_SUBPATH}" +readonly II_OUTPUT_BINPATH="${II_OUTPUT}/bin" + +# ii::build::setup_env will check that the `go` commands is available in +# ${PATH}. If not running on Travis, it will also check that the Go version is +# good enough for the webdav code requirements (1.5+). +# +# Output Vars: +# export GOPATH - A modified GOPATH to our created tree along with extra +# stuff. +# export GOBIN - This is actively unset if already set as we want binaries +# placed in a predictable place. +ii::build::setup_env() { + if [[ -z "$(which go)" ]]; then + cat <&2 + [[ -z ${1-} ]] || { + echo " ${1}" >&2 + } + + ii::log::stack $stack_skip + + echo "Exiting with status ${code}" >&2 + exit "${code}" +} + +# Print out the stack trace +# +# Args: +# $1 The number of stack frames to skip when printing. +ii::log::stack() { + local stack_skip=${1:-0} + stack_skip=$((stack_skip + 1)) + if [[ ${#FUNCNAME[@]} -gt $stack_skip ]]; then + echo "Call stack:" >&2 + local i + for ((i=1 ; i <= ${#FUNCNAME[@]} - $stack_skip ; i++)) + do + local frame_no=$((i - 1 + stack_skip)) + local source_file=${BASH_SOURCE[$frame_no]} + local source_lineno=${BASH_LINENO[$((frame_no - 1))]} + local funcname=${FUNCNAME[$frame_no]} + echo " $i: ${source_file}:${source_lineno} ${funcname}(...)" >&2 + done + fi +} + +find_files() { + find . -not \( \ + \( \ + -wholename './_output' \ + -o -wholename './_tools' \ + -o -wholename './.*' \ + -o -wholename '*/Godeps/*' \ + \) -prune \ + \) -name '*.go' | sort -u +} + diff --git a/hack/verify-gofmt.sh b/hack/verify-gofmt.sh new file mode 100755 index 0000000..69d7031 --- /dev/null +++ b/hack/verify-gofmt.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# GoFmt apparently is changing @ head... + +set -o errexit +set -o nounset +set -o pipefail + +GO_VERSION=($(go version)) + +if [[ -z $(echo "${GO_VERSION[2]}" | grep -E 'go1.4|go1.5') ]]; then + echo "Unknown go version '${GO_VERSION}', skipping gofmt." >&2 + exit 0 +fi + +CODE_ROOT=$(dirname "${BASH_SOURCE}")/.. +source "${CODE_ROOT}/hack/common.sh" +source "${CODE_ROOT}/hack/util.sh" + +cd "${CODE_ROOT}" + +bad_files=$(find_files | xargs gofmt -s -l) +if [[ -n "${bad_files}" ]]; then + echo "!!! gofmt needs to be run on the following files: " >&2 + echo "${bad_files}" + echo "Try running 'gofmt -s -d [path]'" >&2 + echo "Or autocorrect with 'hack/verify-gofmt.sh | xargs -n 1 gofmt -s -w'" >&2 + exit 1 +fi