From 2446892a69a1d8c685b638f9adc51960c59b9402 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Mon, 19 Mar 2018 21:52:49 -0400 Subject: [PATCH] update go generated project files Signed-off-by: Jess Frazelle --- .gitignore | 8 +- .travis.yml | 28 +++++++ LICENSE | 3 +- Makefile | 168 ++++++++++++++++++++++++++++------------- VERSION => VERSION.txt | 0 main.go | 23 +++--- rootfs_ops.go | 2 + version/version.go | 7 ++ 8 files changed, 167 insertions(+), 72 deletions(-) create mode 100644 .travis.yml rename VERSION => VERSION.txt (100%) create mode 100644 version/version.go diff --git a/.gitignore b/.gitignore index 716babf..272afd3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,6 @@ *.o *.a *.so -*.swo -*.swp # Folders _obj @@ -45,6 +43,12 @@ Icon .Trashes binctr +cross/ + +# Go coverage results +coverage.txt +profile.out + *.tar rootfs config.json diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..af334cc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,28 @@ +--- + language: go + sudo: false + notifications: + email: true + go: + - 1.x + - tip + env: + global: + - GO15VENDOREXPERIMENT=1 + matrix: + allow_failures: + - go: tip + fast_finish: true + install: + - go get github.com/golang/lint/golint + - go get honnef.co/go/tools/cmd/staticcheck + script: + - make IMAGE=alpine + - go vet $(go list ./... | grep -v vendor) + - staticcheck $(go list ./... | grep -v vendor) + - test -z "$(golint ./... | grep -v vendor | tee /dev/stderr)" + - test -z "$(gofmt -s -l . | grep -v vendor | tee /dev/stderr)" + - go test $(go list ./... | grep -v vendor) + - make cover + after_success: + - bash <(curl -s https://codecov.io/bash) diff --git a/LICENSE b/LICENSE index ce39057..fd05738 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015 Jess Frazelle +Copyright (c) 2018 The Genuinetools Authors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/Makefile b/Makefile index 26e88b2..58ea0d3 100644 --- a/Makefile +++ b/Makefile @@ -1,82 +1,142 @@ -.PHONY: clean clean-rootfs all fmt vet lint build test install image.tar rootfs.go static +# Set an output prefix, which is the local directory if not specified PREFIX?=$(shell pwd) BUILDTAGS=seccomp apparmor -PROJECT := github.com/jessfraz/binctr -VENDOR := vendor +# Setup name variables for the package/tool +NAME := binctr +PKG := github.com/genuinetools/$(NAME) -# Variable to get the current version. -VERSION := $(shell cat VERSION) +# Set any default go build tags +BUILDTAGS := -# Variable to set the current git commit. +# Set the build dir, where built cross-compiled binaries will be output +BUILDDIR := ${PREFIX}/cross + +# Populate version variables +# Add to compile time flags +VERSION := $(shell cat VERSION.txt) GITCOMMIT := $(shell git rev-parse --short HEAD) -GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD) GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no) ifneq ($(GITUNTRACKEDCHANGES),) -GITCOMMIT := $(GITCOMMIT)-dirty + GITCOMMIT := $(GITCOMMIT)-dirty endif +CTIMEVAR=-X $(PKG)/version.GITCOMMIT=$(GITCOMMIT) -X $(PKG)/version.VERSION=$(VERSION) +GO_LDFLAGS=-ldflags "-w $(CTIMEVAR)" +GO_LDFLAGS_STATIC=-ldflags "-w $(CTIMEVAR) -extldflags -static" -IMAGE := alpine -DOCKER_ROOTFS_IMAGE := $(IMAGE) +# List the GOOS and GOARCH to build +GOOSARCHES = darwin/amd64 darwin/386 freebsd/amd64 freebsd/386 linux/arm linux/arm64 linux/amd64 linux/386 solaris/amd64 windows/amd64 windows/386 -LDFLAGS := ${LDFLAGS} \ - -X main.GITCOMMIT=${GITCOMMIT} \ - -X main.VERSION=${VERSION} \ - -X main.IMAGE=$(notdir $(IMAGE)) \ - -X main.IMAGESHA=$(shell docker inspect --format "{{.Id}}" $(IMAGE)) +all: clean build fmt lint test staticcheck vet install ## Runs a clean, build, fmt, lint, test, staticcheck, vet and install -BINDIR := $(CURDIR)/bin +.PHONY: build +build: $(NAME) ## Builds a dynamic executable or package -all: clean static fmt lint test vet install - -build: rootfs.go +$(NAME): *.go VERSION.txt @echo "+ $@" - go build -tags "$(BUILDTAGS)" -ldflags "${LDFLAGS}" . + go build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(NAME) . -$(BINDIR): - @mkdir -p $@ - -static: $(BINDIR) rootfs.go +.PHONY: static +static: ## Builds a static executable @echo "+ $@" - CGO_ENABLED=1 go build -tags "$(BUILDTAGS) cgo static_build" \ - -ldflags "-w -extldflags -static ${LDFLAGS}" -o bin/$(notdir $(IMAGE)) . - @echo "Static container created at: ./bin/$(notdir $(IMAGE))" - @echo "Run with ./bin/$(notdir $(IMAGE))" + CGO_ENABLED=0 go build \ + -tags "$(BUILDTAGS) static_build" \ + ${GO_LDFLAGS_STATIC} -o $(NAME) . -image.tar: - docker pull --disable-content-trust=false $(DOCKER_ROOTFS_IMAGE) - docker export $(shell docker create $(DOCKER_ROOTFS_IMAGE) sh) > $@ - -rootfs.go: image.tar - GOMAXPROCS=1 go generate - -fmt: +.PHONY: fmt +fmt: ## Verifies all files have men `gofmt`ed @echo "+ $@" - @gofmt -s -l . | grep -v $(VENDOR) | tee /dev/stderr + @gofmt -s -l . | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr -lint: +.PHONY: lint +lint: ## Verifies `golint` passes @echo "+ $@" - @golint ./... | grep -v $(VENDOR) | tee /dev/stderr + @golint ./... | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr -test: fmt lint vet +.PHONY: test +test: ## Runs the go tests @echo "+ $@" - @go test -v -tags "$(BUILDTAGS) cgo" $(shell go list ./... | grep -v $(VENDOR)) + @go test -v -tags "$(BUILDTAGS) cgo" $(shell go list ./... | grep -v vendor) -vet: +.PHONY: vet +vet: ## Verifies `go vet` passes @echo "+ $@" - @go vet $(shell go list ./... | grep -v $(VENDOR)) + @go vet $(shell go list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr -clean-rootfs: - @sudo $(RM) -r rootfs - -clean: clean-rootfs +.PHONY: staticcheck +staticcheck: ## Verifies `staticcheck` passes @echo "+ $@" - @$(RM) binctr - @$(RM) *.tar - @$(RM) rootfs.go - @$(RM) -r $(BINDIR) - -@docker rm $(shell docker ps -aq) /dev/null 2>&1 + @staticcheck $(shell go list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr -install: +.PHONY: cover +cover: ## Runs go test with coverage + @echo "" > coverage.txt + @for d in $(shell go list ./... | grep -v vendor); do \ + go test -race -coverprofile=profile.out -covermode=atomic "$$d"; \ + if [ -f profile.out ]; then \ + cat profile.out >> coverage.txt; \ + rm profile.out; \ + fi; \ + done; + +.PHONY: install +install: ## Installs the executable or package @echo "+ $@" - @go install . + go install -a -tags "$(BUILDTAGS)" ${GO_LDFLAGS} . + +define buildpretty +mkdir -p $(BUILDDIR)/$(1)/$(2); +GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build \ + -o $(BUILDDIR)/$(1)/$(2)/$(NAME) \ + -a -tags "$(BUILDTAGS) static_build netgo" \ + -installsuffix netgo ${GO_LDFLAGS_STATIC} .; +md5sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).md5; +sha256sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).sha256; +endef + +.PHONY: cross +cross: *.go VERSION.txt ## Builds the cross-compiled binaries, creating a clean directory structure (eg. GOOS/GOARCH/binary) + @echo "+ $@" + $(foreach GOOSARCH,$(GOOSARCHES), $(call buildpretty,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH)))) + +define buildrelease +GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build \ + -o $(BUILDDIR)/$(NAME)-$(1)-$(2) \ + -a -tags "$(BUILDTAGS) static_build netgo" \ + -installsuffix netgo ${GO_LDFLAGS_STATIC} .; +md5sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).md5; +sha256sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).sha256; +endef + +.PHONY: release +release: *.go VERSION.txt ## Builds the cross-compiled binaries, naming them in such a way for release (eg. binary-GOOS-GOARCH) + @echo "+ $@" + $(foreach GOOSARCH,$(GOOSARCHES), $(call buildrelease,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH)))) + +.PHONY: bump-version +BUMP := patch +bump-version: ## Bump the version in the version file. Set BUMP to [ patch | major | minor ] + @go get -u github.com/jessfraz/junk/sembump # update sembump tool + $(eval NEW_VERSION = $(shell sembump --kind $(BUMP) $(VERSION))) + @echo "Bumping VERSION.txt from $(VERSION) to $(NEW_VERSION)" + echo $(NEW_VERSION) > VERSION.txt + @echo "Updating links to download binaries in README.md" + sed -i s/$(VERSION)/$(NEW_VERSION)/g README.md + git add VERSION.txt README.md + git commit -vsam "Bump version to $(NEW_VERSION)" + @echo "Run make tag to create and push the tag for new version $(NEW_VERSION)" + +.PHONY: tag +tag: ## Create a new git tag to prepare to build a release + git tag -sa $(VERSION) -m "$(VERSION)" + @echo "Run git push origin $(VERSION) to push your new tag to GitHub and trigger a travis build." + +.PHONY: clean +clean: ## Cleanup any build binaries or packages + @echo "+ $@" + $(RM) $(NAME) + $(RM) -r $(BUILDDIR) + +.PHONY: help +help: + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/VERSION b/VERSION.txt similarity index 100% rename from VERSION rename to VERSION.txt diff --git a/main.go b/main.go index a37dadf..be15590 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "strings" aaprofile "github.com/docker/docker/profiles/apparmor" + "github.com/genuinetools/binctr/version" "github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer/apparmor" _ "github.com/opencontainers/runc/libcontainer/nsenter" @@ -31,7 +32,7 @@ const ( Embedded Image: %s - %s Version: %s - GitCommit: %s + Build: %s ` @@ -53,14 +54,8 @@ var ( hooks specs.Hooks hookflags stringSlice - debug bool - version bool - - // GITCOMMIT is git commit the binary was compiled against. - GITCOMMIT = "" - - // VERSION is the binary version. - VERSION = "v0.1.0" + debug bool + vrsn bool // IMAGE is the name of the image that is embedded at compile time. IMAGE = "alpine" @@ -123,19 +118,19 @@ func init() { flag.BoolVar(&detach, "d", false, "detach from the container's process") flag.BoolVar(&readonly, "read-only", false, "make container filesystem readonly") - flag.BoolVar(&version, "version", false, "print version and exit") - flag.BoolVar(&version, "v", false, "print version and exit (shorthand)") + flag.BoolVar(&vrsn, "version", false, "print version and exit") + flag.BoolVar(&vrsn, "v", false, "print version and exit (shorthand)") flag.BoolVar(&debug, "D", false, "run in debug mode") flag.Usage = func() { - fmt.Fprint(os.Stderr, fmt.Sprintf(BANNER, IMAGE, IMAGESHA, VERSION, GITCOMMIT)) + fmt.Fprint(os.Stderr, fmt.Sprintf(BANNER, IMAGE, IMAGESHA, version.VERSION, version.GITCOMMIT)) flag.PrintDefaults() } flag.Parse() - if version { - fmt.Printf("%s, commit: %s, image: %s, image digest: %s", VERSION, GITCOMMIT, IMAGE, IMAGESHA) + if vrsn { + fmt.Printf("%s, commit: %s, image: %s, image digest: %s", version.VERSION, version.GITCOMMIT, IMAGE, IMAGESHA) os.Exit(0) } diff --git a/rootfs_ops.go b/rootfs_ops.go index f899c94..0aeafe4 100644 --- a/rootfs_ops.go +++ b/rootfs_ops.go @@ -11,6 +11,8 @@ import ( "github.com/opencontainers/runtime-spec/specs-go" ) +const DATA = "" + func unpackRootfs(spec *specs.Spec) error { data, err := base64.StdEncoding.DecodeString(DATA) if err != nil { diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000..8088a27 --- /dev/null +++ b/version/version.go @@ -0,0 +1,7 @@ +package version + +// VERSION indicates which version of the binary is running. +var VERSION string + +// GITCOMMIT indicates which git hash the binary was built off of +var GITCOMMIT string