Makefile 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. .PHONY: all push container clean container-name container-latest push-latest fmt lint test unit vendor header
  2. BINS := $(addprefix bin/,kg kgctl)
  3. PROJECT := kilo
  4. PKG := github.com/squat/$(PROJECT)
  5. REGISTRY ?= index.docker.io
  6. IMAGE ?= squat/$(PROJECT)
  7. TAG := $(shell git describe --abbrev=0 --tags HEAD 2>/dev/null)
  8. COMMIT := $(shell git rev-parse HEAD)
  9. VERSION := $(COMMIT)
  10. ifneq ($(TAG),)
  11. ifeq ($(COMMIT), $(shell git rev-list -n1 $(TAG)))
  12. VERSION := $(TAG)
  13. endif
  14. endif
  15. DIRTY := $(shell test -z "$$(git diff --shortstat 2>/dev/null)" || echo -dirty)
  16. VERSION := $(VERSION)$(DIRTY)
  17. LD_FLAGS := -ldflags '-X $(PKG)/pkg/version.Version=$(VERSION)'
  18. SRC := $(shell find . -type f -name '*.go' -not -path "./vendor/*")
  19. GO_FILES ?= $$(find . -name '*.go' -not -path './vendor/*')
  20. GO_PKGS ?= $$(go list ./... | grep -v "$(PKG)/vendor")
  21. BUILD_IMAGE ?= golang:1.11.1-alpine
  22. all: build
  23. build: $(BINS)
  24. $(BINS): $(SRC) go.mod
  25. @mkdir -p bin
  26. @echo "building: $@"
  27. @docker run --rm \
  28. -u $$(id -u):$$(id -g) \
  29. -v $$(pwd):/$(PROJECT) \
  30. -w /$(PROJECT) \
  31. $(BUILD_IMAGE) \
  32. /bin/sh -c " \
  33. rm -rf ./.cache && \
  34. GOOS=linux \
  35. GOCACHE=./.cache \
  36. CGO_ENABLED=0 \
  37. go build -mod=vendor -o $@ \
  38. $(LD_FLAGS) \
  39. ./cmd/$(@F)/... \
  40. "
  41. fmt:
  42. @echo $(GO_PKGS)
  43. gofmt -w -s $(GO_FILES)
  44. lint: header
  45. @echo 'go vet $(GO_PKGS)'
  46. @vet_res=$$(go vet $(GO_PKGS) 2>&1); if [ -n "$$vet_res" ]; then \
  47. echo ""; \
  48. echo "Go vet found issues. Please check the reported issues"; \
  49. echo "and fix them if necessary before submitting the code for review:"; \
  50. echo "$$vet_res"; \
  51. exit 1; \
  52. fi
  53. @echo 'golint $(GO_PKGS)'
  54. @lint_res=$$(golint $(GO_PKGS)); if [ -n "$$lint_res" ]; then \
  55. echo ""; \
  56. echo "Golint found style issues. Please check the reported issues"; \
  57. echo "and fix them if necessary before submitting the code for review:"; \
  58. echo "$$lint_res"; \
  59. exit 1; \
  60. fi
  61. @echo 'gofmt -d -s $(GO_FILES)'
  62. @fmt_res=$$(gofmt -d -s $(GO_FILES)); if [ -n "$$fmt_res" ]; then \
  63. echo ""; \
  64. echo "Gofmt found style issues. Please check the reported issues"; \
  65. echo "and fix them if necessary before submitting the code for review:"; \
  66. echo "$$fmt_res"; \
  67. exit 1; \
  68. fi
  69. unit:
  70. go test --race ./...
  71. test: lint unit
  72. header: .header
  73. @HEADER=$$(sed "s/YEAR/$$(date '+%Y')/" .header); \
  74. FILES=; \
  75. for f in $(GO_FILES); do \
  76. FILE=$$(head -n $$(wc -l .header | awk '{print $$1}') $$f); \
  77. [ "$$FILE" != "$$HEADER" ] && FILES="$$FILES$$f "; \
  78. done; \
  79. if [ -n "$$FILES" ]; then \
  80. printf 'the following files are missing the license header: %s\n' "$$FILES"; \
  81. exit 1; \
  82. fi
  83. container: .container-$(VERSION) container-name
  84. .container-$(VERSION): $(BINS) Dockerfile
  85. @docker build -t $(IMAGE):$(VERSION) .
  86. @docker images -q $(IMAGE):$(VERSION) > $@
  87. container-latest: .container-$(VERSION)
  88. @docker tag $(IMAGE):$(VERSION) $(IMAGE):latest
  89. @echo "container: $(IMAGE):latest"
  90. container-name:
  91. @echo "container: $(IMAGE):$(VERSION)"
  92. push: .push-$(VERSION) push-name
  93. .push-$(VERSION): .container-$(VERSION)
  94. @docker push $(REGISTRY)/$(IMAGE):$(VERSION)
  95. @docker images -q $(IMAGE):$(VERSION) > $@
  96. push-latest: container-latest
  97. @docker push $(REGISTRY)/$(IMAGE):latest
  98. @echo "pushed: $(IMAGE):latest"
  99. push-name:
  100. @echo "pushed: $(IMAGE):$(VERSION)"
  101. clean: container-clean bin-clean
  102. rm -r .cache
  103. container-clean:
  104. rm -rf .container-* .push-*
  105. bin-clean:
  106. rm -rf bin
  107. vendor:
  108. go mod tidy
  109. go mod vendor