diff --git a/.gitignore b/.gitignore index e0f20f7..a86db2c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,6 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +out/ bin/ +pod-dive diff --git a/Makefile b/Makefile index e96c3d4..145631f 100644 --- a/Makefile +++ b/Makefile @@ -1,34 +1,138 @@ +# Copyright 2019 Cornelius Weig (from ketall) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. -export GO111MODULE=on -export GOROOT=$(PWD) +export GO111MODULE ?= on +export GOARCH ?= amd64 +export CGO_ENABLED ?= 0 -.PHONY: bin -bin: fmt vet - go build -o bin/pod-dive github.com/caiobegotti/pod-dive/cmd/plugin +PROJECT ?= pod-dive +REPOPATH ?= github.com/caiobegotti/$(PROJECT) +COMMIT := $(shell git rev-parse HEAD) +VERSION ?= $(shell git describe --always --tags --dirty="-WIP") +GOOS ?= $(shell go env GOOS) +GOPATH ?= $(shell go env GOPATH) + +BUILDDIR := out +PLATFORMS ?= darwin/amd64 windows/amd64 linux/amd64 +DISTFILE := $(BUILDDIR)/$(VERSION).tar.gz +ASSETS := $(BUILDDIR)/pod-dive-$(GOARCH)-darwin.tar.gz $(BUILDDIR)/pod-dive-$(GOARCH)-linux.tar.gz $(BUILDDIR)/pod-dive-$(GOARCH)-windows.zip +CHECKSUMS := $(patsubst %,%.sha256,$(ASSETS)) + +VERSION_PACKAGE := $(REPOPATH)/pkg/pod-dive/version + +DATE_FMT = %Y-%m-%dT%H:%M:%SZ +ifdef SOURCE_DATE_EPOCH + # GNU and BSD date require different options for a fixed date + BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)" 2>/dev/null) +else + BUILD_DATE ?= $(shell date "+$(DATE_FMT)") +endif +GO_LDFLAGS :="-s -w +GO_LDFLAGS += -X $(VERSION_PACKAGE).version=$(VERSION) +GO_LDFLAGS += -X $(VERSION_PACKAGE).buildDate=$(BUILD_DATE) +GO_LDFLAGS += -X $(VERSION_PACKAGE).gitCommit=$(COMMIT) +GO_LDFLAGS +=" + +ifdef ZOPFLI + COMPRESS:=zopfli -c +else + COMPRESS:=gzip --best -k -c +endif + +GO_FILES := $(shell find . -type f -name '*.go') + +.PHONY: all +all: clean lint test dev .PHONY: test test: - go test ./pkg/... ./cmd/... -coverprofile cover.out + go test ./... -.PHONY: fmt -fmt: - go fmt ./pkg/... ./cmd/... +.PHONY: help +help: + @echo 'Valid make targets:' + @echo ' - all: build binaries for all supported platforms' + @echo ' - clean: clean up build directory' + @echo ' - coverage: run unit tests with coverage' + @echo ' - deploy: build artifacts for a new deployment' + @echo ' - dev: build the binary for the current platform' + @echo ' - dist: create a tar archive of the source code' + @echo ' - help: print this help' + @echo ' - lint: run fmt and vet' + @echo ' - test: run unit tests' + @echo ' - build: build binaries for all supported platforms' + +.PHONY: coverage +coverage: $(BUILDDIR) + go test -coverprofile=$(BUILDDIR)/coverage.txt -covermode=atomic ./... + +.PHONY: dev +dev: CGO_ENABLED := 1 +dev: GO_LDFLAGS := $(subst -s -w,,$(GO_LDFLAGS)) + +# go get k8s.io/client-go@v11.0.0 +# go get k8s.io/api@kubernetes-1.14.0 +# go get k8s.io/apimachinery@kubernetes-1.14.0 +# go get k8s.io/cli-runtime@kubernetes-1.14.0 +dev: + go build -race -ldflags $(GO_LDFLAGS) -o pod-dive $(REPOPATH)/cmd/plugin -.PHONY: vet -vet: +build: $(GO_FILES) $(BUILDDIR) + gox -osarch="$(PLATFORMS)" -ldflags $(GO_LDFLAGS) -output="out/pod-dive-{{.Arch}}-{{.OS}}" $(REPOPATH)/cmd/plugin + +.PHONY: lint +lint: + go fmt ./pkg/... ./cmd/... go vet ./pkg/... ./cmd/... -.PHONY: kubernetes-deps -kubernetes-deps: - go get k8s.io/client-go@v11.0.0 - go get k8s.io/api@kubernetes-1.14.0 - go get k8s.io/apimachinery@kubernetes-1.14.0 - go get k8s.io/cli-runtime@kubernetes-1.14.0 +.PRECIOUS: %.zip +%.zip: %.exe + cp LICENSE $(BUILDDIR) && \ + cd $(BUILDDIR) && \ + zip $(patsubst $(BUILDDIR)/%, %, $@) LICENSE $(patsubst $(BUILDDIR)/%, %, $<) + +.PRECIOUS: %.gz +%.gz: % + $(COMPRESS) "$<" > "$@" + +%.tar: % + cp LICENSE $(BUILDDIR) + tar cf "$@" -C $(BUILDDIR) LICENSE $(patsubst $(BUILDDIR)/%,%,$^) + +$(BUILDDIR): + mkdir -p "$@" + +%.sha256: % + shasum -a 256 $< > $@ + +.INTERMEDIATE: $(DISTFILE:.gz=) +$(DISTFILE:.gz=): $(BUILDDIR) + git archive --prefix="pod-dive-$(VERSION)/" --format=tar HEAD > "$@" + +.PHONY: deploy +deploy: $(CHECKSUMS) + $(RM) $(BUILDDIR)/LICENSE + +.PHONY: dist +dist: $(DISTFILE) -.PHONY: setup -setup: - make -C setup +.PHONY: clean +clean: + $(RM) -r $(BUILDDIR) pod-dive -# https://github.com/guessi/kubectl-grep/blo +$(BUILDDIR)/pod-dive-amd64-linux: build +$(BUILDDIR)/pod-dive-amd64-darwin: build +$(BUILDDIR)/pod-dive-amd64-windows.exe: build -# https://github.com/corneliusweig/ketall/blob/master/Makefile \ No newline at end of file +# https://github.com/guessi/kubectl-grep/blo \ No newline at end of file diff --git a/go.sum b/go.sum index 0a71b50..06366de 100644 --- a/go.sum +++ b/go.sum @@ -466,6 +466,7 @@ github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI= github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jcmturner/gofork v0.0.0-20180107083740-2aebee971930/go.mod h1:MK8+TM0La+2rjBD4jE12Kj1pCCxK7d2LK/UM3ncEo0o= github.com/jcmturner/gofork v0.0.0-20190328161633-dc7c13fece03/go.mod h1:MK8+TM0La+2rjBD4jE12Kj1pCCxK7d2LK/UM3ncEo0o=