From 372ecd0cf81721b81b59a8ca4a90359410f6554b Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Fri, 4 Jan 2019 10:58:30 +0000 Subject: [PATCH] Introduce Makefile --- .env | 3 ++ .travis.yml | 9 +--- CHANGELOG.md | 6 +++ CONTRIBUTING.md | 2 +- Dockerfile | 6 +-- Makefile | 55 +++++++++++++++++++ configure | 137 ++++++++++++++++++++++++++++++++++++++++++++++++ lint.sh | 11 ---- main.go | 2 +- test.sh | 2 - version.go | 2 +- 11 files changed, 209 insertions(+), 26 deletions(-) create mode 100644 .env create mode 100644 Makefile create mode 100755 configure delete mode 100755 lint.sh delete mode 100755 test.sh diff --git a/.env b/.env new file mode 100644 index 0000000..6f135d6 --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +MAKE := "/usr/bin/make" + GO := "/usr/local/bin/go" + DEP := "/usr/local/bin/dep" diff --git a/.travis.yml b/.travis.yml index 54e072a..ce55714 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,15 +6,10 @@ install: # Fetch dependencies - wget -O dep https://github.com/golang/dep/releases/download/v0.3.2/dep-linux-amd64 - chmod +x dep - - ./dep ensure --vendor-only script: - - set -e - # Lint - - go get -u github.com/alecthomas/gometalinter - - gometalinter --install - - ./lint.sh + - ./configure # Run tests - - ./test.sh + - make test sudo: false notifications: email: false diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bdb3a0..4e378da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## Changes since v2.2: +- Move automated build to debian base image +- Add Makefile + - Update CI to run `make test` + - Update Dockerfile to use `make clean oauth2_proxy` + - Update `VERSION` parameter to be set by `ldflags` from Git Status + - Remove lint and test scripts - Remove Go v1.8.x from Travis CI testing - Add CODEOWNERS file - Add CONTRIBUTING guide diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f92d18c..17f7620 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,7 +8,7 @@ Download the dependencies using [`dep`](https://github.com/golang/dep). ```bash cd $GOPATH/src/github.com # Create this directory if it doesn't exist git clone git@github.com:/oauth2_proxy pusher/oauth2_proxy -dep ensure # Installs dependencies to vendor folder. +make dep ``` ## Pull Requests and Issues diff --git a/Dockerfile b/Dockerfile index af11f4f..c141678 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,10 +7,10 @@ RUN go get -u github.com/golang/dep/cmd/dep RUN dep ensure --vendor-only # Build image -RUN CGO_ENABLED=0 GOOS=linux go build +RUN ./configure && make clean oauth2_proxy -# Copy binary to alpine -FROM alpine:3.8 +# Copy binary to debian +FROM debian:stretch COPY --from=builder /go/src/github.com/pusher/oauth2_proxy/oauth2_proxy /bin/oauth2_proxy ENTRYPOINT ["/bin/oauth2_proxy"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9cccf56 --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +include .env +BINARY := oauth2_proxy +VERSION := $(shell git describe --always --long --dirty --tags 2>/dev/null || echo "undefined") + +.PHONY: all +all: dep lint $(BINARY) + +.PHONY: clean +clean: + rm -rf release + rm -f $(BINARY) + +.PHONY: distclean +distclean: clean + rm -rf vendor + +BIN_DIR := $(GOPATH)/bin +GOMETALINTER := $(BIN_DIR)/gometalinter + +$(GOMETALINTER): + $(GO) get -u github.com/alecthomas/gometalinter + gometalinter --install %> /dev/null + +.PHONY: lint +lint: $(GOMETALINTER) + $(GOMETALINTER) --vendor --disable-all \ + --enable=vet \ + --enable=vetshadow \ + --enable=golint \ + --enable=ineffassign \ + --enable=goconst \ + --enable=deadcode \ + --enable=gofmt \ + --enable=goimports \ + --tests ./... + +.PHONY: dep +dep: + $(DEP) ensure --vendor-only + +.PHONY: build +build: clean $(BINARY) + +$(BINARY): + $(GO) build -ldflags="-X main.VERSION=${VERSION}" -o $(BINARY) github.com/pusher/oauth2_proxy + +.PHONY: test +test: dep lint + $(GO) test -v -race $(go list ./... | grep -v /vendor/) + +.PHONY: release +release: dep lint test + mkdir release + GOOS=darwin GOARCH=amd64 go build -ldflags="-X main.VERSION=${VERSION}" -o release/$(BINARY)-darwin-amd64 github.com/pusher/oauth2_proxy + GOOS=linux GOARCH=amd64 go build -ldflags="-X main.VERSION=${VERSION}" -o release/$(BINARY)-linux-amd64 github.com/pusher/oauth2_proxy diff --git a/configure b/configure new file mode 100755 index 0000000..2de227f --- /dev/null +++ b/configure @@ -0,0 +1,137 @@ +#!/usr/bin/env bash + +RED='\033[0;31m' +GREEN='\033[0;32m' +BLUE='\033[0;34m' +NC='\033[0m' + +declare -A tools=() +declare -A desired=() + +for arg in "$@"; do + case ${arg%%=*} in + "--with-go") + desired[go]="${arg##*=}" + ;; + "--with-dep") + desired[dep]="${arg##*=}" + ;; + "--help") + printf "${GREEN}$0${NC}\n" + printf " available options:\n" + printf " --with-dep=${BLUE}${NC}\n" + printf " --with-go=${BLUE}${NC}\n" + exit 0 + ;; + *) + echo "Unknown option: $arg" + exit 2 + ;; + esac +done + +vercomp () { + if [[ $1 == $2 ]] + then + return 0 + fi + local IFS=. + local i ver1=($1) ver2=($2) + # fill empty fields in ver1 with zeros + for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) + do + ver1[i]=0 + done + for ((i=0; i<${#ver1[@]}; i++)) + do + if [[ -z ${ver2[i]} ]] + then + # fill empty fields in ver2 with zeros + ver2[i]=0 + fi + if ((10#${ver1[i]} > 10#${ver2[i]})) + then + return 1 + fi + if ((10#${ver1[i]} < 10#${ver2[i]})) + then + return 2 + fi + done + return 0 +} + +check_for() { + echo -n "Checking for $1... " + if ! [ -z "${desired[$1]}" ]; then + TOOL_PATH="${desired[$1]}" + else + TOOL_PATH=$(command -v $1) + fi + if ! [ -x "$TOOL_PATH" -a -f "$TOOL_PATH" ]; then + printf "${RED}not found${NC}\n" + cd - + exit 1 + else + printf "${GREEN}found${NC}\n" + tools[$1]=$TOOL_PATH + fi +} + +check_go_version() { + echo -n "Checking go version... " + GO_VERSION=$(${tools[go]} version | ${tools[awk]} '{where = match($0, /[0-9]\.[0-9]+\.[0-9]*/); if (where != 0) print substr($0, RSTART, RLENGTH)}') + vercomp $GO_VERSION 1.10 + case $? in + 0) ;& + 1) + printf "${GREEN}" + echo $GO_VERSION + printf "${NC}" + ;; + 2) + printf "${RED}" + echo "$GO_VERSION < 1.10" + exit 1 + ;; + esac +} + +check_docker_version() { + echo -n "Checking docker version... " + DOCKER_VERSION=$(${tools[docker]} version | ${tools[awk]}) +} + +check_go_env() { + echo -n "Checking \$GOPATH... " + if [ -z "$GOPATH" ]; then + printf "${RED}invalid${NC} - GOPATH not set\n" + exit 1 + fi + printf "${GREEN}valid${NC} - $GOPATH\n" +} + +cd ${0%/*} + +if [ ! -f .env ]; then + rm .env +fi + +check_for make +check_for awk +check_for go +check_go_version +check_go_env +check_for dep + +echo + +cat <<- EOF > .env + MAKE := "${tools[make]}" + GO := "${tools[go]}" + DEP := "${tools[dep]}" +EOF + +echo "Environment configuration written to .env" + +cd - > /dev/null diff --git a/lint.sh b/lint.sh deleted file mode 100755 index 0fa9f5a..0000000 --- a/lint.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -gometalinter --vendor --disable-all \ ---enable=vet \ ---enable=vetshadow \ ---enable=golint \ ---enable=ineffassign \ ---enable=goconst \ ---enable=deadcode \ ---enable=gofmt \ ---enable=goimports \ ---tests ./... diff --git a/main.go b/main.go index 287dc48..c1cde80 100644 --- a/main.go +++ b/main.go @@ -84,7 +84,7 @@ func main() { flagSet.Parse(os.Args[1:]) if *showVersion { - fmt.Printf("oauth2_proxy v%s (built with %s)\n", VERSION, runtime.Version()) + fmt.Printf("oauth2_proxy %s (built with %s)\n", VERSION, runtime.Version()) return } diff --git a/test.sh b/test.sh deleted file mode 100755 index c99556a..0000000 --- a/test.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -go test -v -race $(go list ./... | grep -v /vendor/) diff --git a/version.go b/version.go index c89a1f6..d6fa19b 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,4 @@ package main // VERSION contains version information -const VERSION = "2.2.1-alpha" +var VERSION = "undefined"