Skip to content

Commit

Permalink
Merge pull request #4 from Clever/cleverize
Browse files Browse the repository at this point in the history
update makefile, switch to dep, add circle.yml
  • Loading branch information
nathanleiby authored Jan 16, 2018
2 parents 0acd141 + 339a7ec commit b19d1fd
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin/
build/
release/
vendor/
41 changes: 41 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/google/go-github"

[[constraint]]
name = "golang.org/x/oauth2"
54 changes: 12 additions & 42 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
include golang.mk
.DEFAULT_GOAL :=
PKGS := $(shell go list ./... | grep -v /vendor)
EXECUTABLEPKG := github.com/rgarcia/reposync
EXECUTABLEPKG := github.com/Clever/reposync
EXECUTABLE := reposync
VERSION := $(shell cat VERSION)

.PHONY: all build clean test vendor $(PKGS)
.PHONY: all build clean test vendor release $(PKGS)

$(eval $(call golang-version-check,1.6))
$(eval $(call golang-version-check,1.9))

all: test build

build:
go build -ldflags "-X main.Version=$(VERSION)-dev" -o bin/$(EXECUTABLE) $(EXECUTABLEPKG)
install_deps: golang-dep-vendor-deps

vendor: golang-godep-vendor-deps
$(call golang-godep-vendor,$(PKGS))
$(call golang-dep-vendor)
build:
go build -ldflags "-X main.Version=$(VERSION)" -o bin/$(EXECUTABLE) $(EXECUTABLEPKG)

test: $(PKGS)

Expand All @@ -25,41 +25,11 @@ $(PKGS): golang-test-all-deps
run: build
./bin/$(EXECUTABLE)

BUILDS := \
build/$(EXECUTABLE)-v$(VERSION)-linux-amd64 \
build/$(EXECUTABLE)-v$(VERSION)-darwin-amd64
COMPRESSED_BUILDS := $(BUILDS:%=%.tar.gz)
RELEASE_ARTIFACTS := $(COMPRESSED_BUILDS:build/%=release/%)

build/$(EXECUTABLE)-v$(VERSION)-darwin-amd64:
GOARCH=amd64 GOOS=darwin go build -ldflags "-X main.Version=$(VERSION)" -o "$@/$(EXECUTABLE)" $(EXECUTABLEPKG)
build/$(EXECUTABLE)-v$(VERSION)-linux-amd64:
GOARCH=amd64 GOOS=linux go build -ldflags "-X main.Version=$(VERSION)" -o "$@/$(EXECUTABLE)" $(EXECUTABLEPKG)

%.tar.gz: %
tar -C `dirname $<` -zcvf "$<.tar.gz" `basename $<`

$(RELEASE_ARTIFACTS): release/% : build/%
mkdir -p release
cp $< $@

release: $(RELEASE_ARTIFACTS)

$(GOPATH)/bin/github-release:
# assumes linux dev env
wget https://github.com/aktau/github-release/releases/download/v0.6.2/linux-amd64-github-release.tar.bz2
tar xjf linux-amd64-github-release.tar.bz2
mv bin/linux/amd64/github-release $(GOPATH)/bin/github-release
rm -rf linux-amd64-github-release.tar.bz2 bin/linux

export GITHUB_TOKEN ?= $(GITHUB_API_TOKEN)
export GITHUB_USER ?= rgarcia
export GITHUB_REPO ?= $(EXECUTABLE)
publish: $(GOPATH)/bin/github-release release
$(GOPATH)/bin/github-release release -t $(VERSION) -n $(VERSION) -d $(VERSION)
$(GOPATH)/bin/github-release upload -t $(VERSION) -n $(EXECUTABLE)-v$(VERSION)-darwin-amd64.tar.gz -f release/$(EXECUTABLE)-v$(VERSION)-darwin-amd64.tar.gz
$(GOPATH)/bin/github-release upload -t $(VERSION) -n $(EXECUTABLE)-v$(VERSION)-linux-amd64.tar.gz -f release/$(EXECUTABLE)-v$(VERSION)-linux-amd64.tar.gz
release:
@GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X main.version=$(VERSION)" \
-o="$@/$(EXECUTABLE)-$(VERSION)-linux-amd64"
@GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w -X main.version=$(VERSION)" \
-o="$@/$(EXECUTABLE)-$(VERSION)-darwin-amd64"

clean:
rm -rf bin/*
rm -rf build release
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.3
0.6.4
24 changes: 24 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
machine:
post:
- cd $HOME && git clone --depth 1 -v [email protected]:clever/ci-scripts.git && cd ci-scripts && git show --oneline -s
- $HOME/ci-scripts/circleci/golang-install 1.9
services:
- docker
checkout:
post:
- $HOME/ci-scripts/circleci/golang-move-project
compile:
override:
- make install_deps
- make build
test:
override:
- make test
deployment:
all:
branch: master
owner: Clever
commands:
- make release && $HOME/ci-scripts/circleci/github-release $GH_RELEASE_TOKEN release
general:
build_dir: ../.go_workspace/src/github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME
98 changes: 69 additions & 29 deletions golang.mk
Original file line number Diff line number Diff line change
@@ -1,51 +1,73 @@
# This is the default Clever Golang Makefile.
# It is stored in the dev-handbook repo, github.com/Clever/dev-handbook
# Please do not alter this file directly.
GOLANG_MK_VERSION := 0.1.0
GOLANG_MK_VERSION := 0.3.1

SHELL := /bin/bash
.PHONY: golang-godep-vendor golang-test-deps $(GODEP)
SYSTEM := $(shell uname -a | cut -d" " -f1 | tr '[:upper:]' '[:lower:]')
.PHONY: golang-test-deps bin/dep golang-ensure-curl-installed

# if the gopath includes several directories, use only the first
GOPATH=$(shell echo $$GOPATH | cut -d: -f1)

# This block checks and confirms that the proper Go toolchain version is installed.
# It uses ^ matching in the semver sense -- you can be ahead by a minor
# version, but not a major version (patch is ignored).
# arg1: golang version
define golang-version-check
GOVERSION := $(shell go version | grep $(1))
_ := $(if \
$(shell go version | grep $(1)), \
@echo "", \
$(error "must be running Go version $(1)"))
_ := $(if \
$(shell \
expr >/dev/null \
`go version | cut -d" " -f3 | cut -c3- | cut -d. -f2` \
\>= `echo $(1) | cut -d. -f2` \
\& \
`go version | cut -d" " -f3 | cut -c3- | cut -d. -f1` \
= `echo $(1) | cut -d. -f1` \
&& echo 1), \
@echo "", \
$(error must be running Go version ^$(1) - you are running $(shell go version | cut -d" " -f3 | cut -c3-)))
endef

export GO15VENDOREXPERIMENT=1

# FGT is a utility that exits with 1 whenever any stderr/stdout output is recieved.
FGT := $(GOPATH)/bin/fgt
$(FGT):
go get github.com/GeertJohan/fgt

# Godep is a tool used to manage Golang dependencies in the style of the Go 1.5
# vendoring experiment.
GODEP := $(GOPATH)/bin/godep
$(GODEP):
go get -u github.com/tools/godep
golang-ensure-curl-installed:
@command -v curl >/dev/null 2>&1 || { echo >&2 "curl not installed. Please install curl."; exit 1; }

DEP_VERSION = v0.3.2
DEP_INSTALLED := $(shell [[ -e "bin/dep" ]] && bin/dep version | grep version | grep -v go | cut -d: -f2 | tr -d '[:space:]')
# Dep is a tool used to manage Golang dependencies. It is the offical vendoring experiment, but
# not yet the official tool for Golang.
bin/dep: golang-ensure-curl-installed
@mkdir -p bin
@[[ "$(DEP_VERSION)" != "$(DEP_INSTALLED)" ]] && \
echo "Updating dep..." && \
curl -o bin/dep -sL https://github.com/golang/dep/releases/download/$(DEP_VERSION)/dep-$(SYSTEM)-amd64 && \
chmod +x bin/dep || true

golang-dep-vendor-deps: bin/dep

# golang-godep-vendor is a target for saving dependencies with the dep tool
# to the vendor/ directory. All nested vendor/ directories are deleted via
# the prune command.
# In CI, -vendor-only is used to avoid updating the lock file.
ifndef CI
define golang-dep-vendor
bin/dep ensure -v
endef
else
define golang-dep-vendor
bin/dep ensure -v -vendor-only
endef
endif

# Golint is a tool for linting Golang code for common errors.
GOLINT := $(GOPATH)/bin/golint
$(GOLINT):
go get github.com/golang/lint/golint

# golang-vendor-deps installs all dependencies needed for different test cases.
golang-godep-vendor-deps: $(GODEP)

# golang-godep-vendor is a target for saving dependencies with the godep tool
# to the vendor/ directory. All nested vendor/ directories are deleted as they
# are not handled well by the Go toolchain.
# arg1: pkg path
define golang-godep-vendor
$(GODEP) save $(1)
@# remove any nested vendor directories
find vendor/ -path '*/vendor' -type d | xargs -IX rm -r X
endef

# golang-fmt-deps requires the FGT tool for checking output
golang-fmt-deps: $(FGT)

Expand All @@ -63,7 +85,7 @@ golang-lint-deps: $(GOLINT)
# arg1: pkg path
define golang-lint
@echo "LINTING $(1)..."
@$(GOLINT) $(GOPATH)/src/$(1)/*.go
@find $(GOPATH)/src/$(1)/*.go -type f | grep -v gen_ | xargs $(GOLINT)
endef

# golang-lint-deps-strict requires the golint tool for golang linting.
Expand All @@ -74,7 +96,7 @@ golang-lint-deps-strict: $(GOLINT) $(FGT)
# arg1: pkg path
define golang-lint-strict
@echo "LINTING $(1)..."
@$(FGT) $(GOLINT) $(GOPATH)/src/$(1)/*.go
@find $(GOPATH)/src/$(1)/*.go -type f | grep -v gen_ | xargs $(FGT) $(GOLINT)
endef

# golang-test-deps is here for consistency
Expand Down Expand Up @@ -131,3 +153,21 @@ $(call golang-lint-strict,$(1))
$(call golang-vet,$(1))
$(call golang-test-strict,$(1))
endef

# golang-build: builds a golang binary. ensures CGO build is done during CI. This is needed to make a binary that works with a Docker alpine image.
# arg1: pkg path
# arg2: executable name
define golang-build
@echo "BUILDING..."
@if [ -z "$$CI" ]; then \
go build -o bin/$(2) $(1); \
else \
echo "-> Building CGO binary"; \
CGO_ENABLED=0 go build -installsuffix cgo -o bin/$(2) $(1); \
fi;
endef

# golang-update-makefile downloads latest version of golang.mk
golang-update-makefile:
@wget https://raw.githubusercontent.com/Clever/dev-handbook/master/make/golang.mk -O /tmp/golang.mk 2>/dev/null
@if ! grep -q $(GOLANG_MK_VERSION) /tmp/golang.mk; then cp /tmp/golang.mk golang.mk && echo "golang.mk updated"; else echo "golang.mk is up-to-date"; fi

0 comments on commit b19d1fd

Please sign in to comment.