Skip to content

Commit 1d71a95

Browse files
committed
update makefile
Signed-off-by: Jess Frazelle <[email protected]>
1 parent ac251ab commit 1d71a95

File tree

4 files changed

+194
-146
lines changed

4 files changed

+194
-146
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ cross/
4848
# Go coverage results
4949
coverage.txt
5050
profile.out
51+
52+
!go.mod

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ RUN set -x \
2323
&& rm -rf /go \
2424
&& echo "Build complete."
2525

26-
FROM scratch
26+
FROM alpine:latest
2727

2828
COPY --from=builder /usr/bin/pastebinit /usr/bin/pastebinit
2929
COPY --from=builder /etc/ssl/certs/ /etc/ssl/certs
3030

31-
COPY server/static /src/static
32-
3331
ENTRYPOINT [ "pastebinit" ]
3432
CMD [ "--help" ]

Makefile

Lines changed: 6 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,13 @@
1-
# Set an output prefix, which is the local directory if not specified
2-
PREFIX?=$(shell pwd)
3-
41
# Setup name variables for the package/tool
52
NAME := pastebinit
63
PKG := github.com/jessfraz/$(NAME)
74

8-
# Set any default go build tags
9-
BUILDTAGS :=
10-
11-
# Set the build dir, where built cross-compiled binaries will be output
12-
BUILDDIR := ${PREFIX}/cross
13-
14-
# Populate version variables
15-
# Add to compile time flags
16-
VERSION := $(shell cat VERSION.txt)
17-
GITCOMMIT := $(shell git rev-parse --short HEAD)
18-
GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no)
19-
ifneq ($(GITUNTRACKEDCHANGES),)
20-
GITCOMMIT := $(GITCOMMIT)-dirty
21-
endif
22-
CTIMEVAR=-X $(PKG)/version.GITCOMMIT=$(GITCOMMIT) -X $(PKG)/version.VERSION=$(VERSION)
23-
GO_LDFLAGS=-ldflags "-w $(CTIMEVAR)"
24-
GO_LDFLAGS_STATIC=-ldflags "-w $(CTIMEVAR) -extldflags -static"
25-
26-
# Set our default go compiler
27-
GO := go
28-
29-
# List the GOOS and GOARCH to build
30-
GOOSARCHES = $(shell cat .goosarch)
31-
32-
.PHONY: build
33-
build: $(NAME) ## Builds a dynamic executable or package
34-
35-
$(NAME): $(wildcard *.go) $(wildcard */*.go) VERSION.txt
36-
@echo "+ $@"
37-
$(GO) build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(NAME) .
38-
39-
.PHONY: static
40-
static: ## Builds a static executable
41-
@echo "+ $@"
42-
CGO_ENABLED=0 $(GO) build \
43-
-tags "$(BUILDTAGS) static_build" \
44-
${GO_LDFLAGS_STATIC} -o $(NAME) .
45-
46-
all: clean build fmt lint test staticcheck vet install ## Runs a clean, build, fmt, lint, test, staticcheck, vet and install
47-
48-
.PHONY: fmt
49-
fmt: ## Verifies all files have been `gofmt`ed
50-
@echo "+ $@"
51-
@gofmt -s -l . | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr
52-
53-
.PHONY: lint
54-
lint: ## Verifies `golint` passes
55-
@echo "+ $@"
56-
@golint ./... | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr
5+
CGO_ENABLED := 0
576

58-
.PHONY: test
59-
test: ## Runs the go tests
60-
@echo "+ $@"
61-
@$(GO) test -v -tags "$(BUILDTAGS) cgo" $(shell $(GO) list ./... | grep -v vendor)
62-
63-
.PHONY: vet
64-
vet: ## Verifies `go vet` passes
65-
@echo "+ $@"
66-
@$(GO) vet $(shell $(GO) list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr
67-
68-
.PHONY: staticcheck
69-
staticcheck: ## Verifies `staticcheck` passes
70-
@echo "+ $@"
71-
@staticcheck $(shell $(GO) list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr
72-
73-
.PHONY: cover
74-
cover: ## Runs go test with coverage
75-
@echo "" > coverage.txt
76-
@for d in $(shell $(GO) list ./... | grep -v vendor); do \
77-
$(GO) test -race -coverprofile=profile.out -covermode=atomic "$$d"; \
78-
if [ -f profile.out ]; then \
79-
cat profile.out >> coverage.txt; \
80-
rm profile.out; \
81-
fi; \
82-
done;
83-
84-
.PHONY: install
85-
install: ## Installs the executable or package
86-
@echo "+ $@"
87-
$(GO) install -a -tags "$(BUILDTAGS)" ${GO_LDFLAGS} .
88-
89-
define buildpretty
90-
mkdir -p $(BUILDDIR)/$(1)/$(2);
91-
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 $(GO) build \
92-
-o $(BUILDDIR)/$(1)/$(2)/$(NAME) \
93-
-a -tags "$(BUILDTAGS) static_build netgo" \
94-
-installsuffix netgo ${GO_LDFLAGS_STATIC} .;
95-
md5sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).md5;
96-
sha256sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).sha256;
97-
endef
98-
99-
.PHONY: cross
100-
cross: *.go VERSION.txt ## Builds the cross-compiled binaries, creating a clean directory structure (eg. GOOS/GOARCH/binary)
101-
@echo "+ $@"
102-
$(foreach GOOSARCH,$(GOOSARCHES), $(call buildpretty,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH))))
103-
104-
define buildrelease
105-
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 $(GO) build \
106-
-o $(BUILDDIR)/$(NAME)-$(1)-$(2) \
107-
-a -tags "$(BUILDTAGS) static_build netgo" \
108-
-installsuffix netgo ${GO_LDFLAGS_STATIC} .;
109-
md5sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).md5;
110-
sha256sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).sha256;
111-
endef
112-
113-
.PHONY: release
114-
release: *.go VERSION.txt ## Builds the cross-compiled binaries, naming them in such a way for release (eg. binary-GOOS-GOARCH)
115-
@echo "+ $@"
116-
$(foreach GOOSARCH,$(GOOSARCHES), $(call buildrelease,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH))))
117-
118-
.PHONY: bump-version
119-
BUMP := patch
120-
bump-version: ## Bump the version in the version file. Set BUMP to [ patch | major | minor ]
121-
@$(GO) get -u github.com/jessfraz/junk/sembump # update sembump tool
122-
$(eval NEW_VERSION = $(shell sembump --kind $(BUMP) $(VERSION)))
123-
@echo "Bumping VERSION.txt from $(VERSION) to $(NEW_VERSION)"
124-
echo $(NEW_VERSION) > VERSION.txt
125-
@echo "Updating links to download binaries in README.md"
126-
sed -i s/$(VERSION)/$(NEW_VERSION)/g README.md
127-
git add VERSION.txt README.md
128-
git commit -vsam "Bump version to $(NEW_VERSION)"
129-
@echo "Run make tag to create and push the tag for new version $(NEW_VERSION)"
130-
131-
.PHONY: tag
132-
tag: ## Create a new git tag to prepare to build a release
133-
git tag -sa $(VERSION) -m "$(VERSION)"
134-
@echo "Run git push origin $(VERSION) to push your new tag to GitHub and trigger a travis build."
135-
136-
.PHONY: AUTHORS
137-
AUTHORS:
138-
@$(file >$@,# This file lists all individuals having contributed content to the repository.)
139-
@$(file >>$@,# For how it is generated, see `make AUTHORS`.)
140-
@echo "$(shell git log --format='\n%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf)" >> $@
7+
# Set any default go build tags.
8+
BUILDTAGS :=
1419

142-
.PHONY: clean
143-
clean: ## Cleanup any build binaries or packages
144-
@echo "+ $@"
145-
$(RM) $(NAME)
146-
$(RM) -r $(BUILDDIR)
10+
include basic.mk
14711

148-
.PHONY: help
149-
help:
150-
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
12+
.PHONY: prebuild
13+
prebuild:

basic.mk

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Set an output prefix, which is the local directory if not specified
2+
PREFIX?=$(shell pwd)
3+
4+
# Set the build dir, where built cross-compiled binaries will be output
5+
BUILDDIR := ${PREFIX}/cross
6+
7+
# Populate version variables
8+
# Add to compile time flags
9+
VERSION := $(shell cat VERSION.txt)
10+
GITCOMMIT := $(shell git rev-parse --short HEAD)
11+
GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no)
12+
ifneq ($(GITUNTRACKEDCHANGES),)
13+
GITCOMMIT := $(GITCOMMIT)-dirty
14+
endif
15+
ifeq ($(GITCOMMIT),)
16+
GITCOMMIT := ${GITHUB_SHA}
17+
endif
18+
CTIMEVAR=-X $(PKG)/version.GITCOMMIT=$(GITCOMMIT) -X $(PKG)/version.VERSION=$(VERSION)
19+
GO_LDFLAGS=-ldflags "-w $(CTIMEVAR)"
20+
GO_LDFLAGS_STATIC=-ldflags "-w $(CTIMEVAR) -extldflags -static"
21+
22+
# Set our default go compiler
23+
GO := go
24+
25+
# List the GOOS and GOARCH to build
26+
GOOSARCHES = $(shell cat .goosarch)
27+
28+
# Set the graph driver as the current graphdriver if not set.
29+
DOCKER_GRAPHDRIVER := $(if $(DOCKER_GRAPHDRIVER),$(DOCKER_GRAPHDRIVER),$(shell docker info 2>&1 | grep "Storage Driver" | sed 's/.*: //'))
30+
export DOCKER_GRAPHDRIVER
31+
32+
# If this session isn't interactive, then we don't want to allocate a
33+
# TTY, which would fail, but if it is interactive, we do want to attach
34+
# so that the user can send e.g. ^C through.
35+
INTERACTIVE := $(shell [ -t 0 ] && echo 1 || echo 0)
36+
ifeq ($(INTERACTIVE), 1)
37+
DOCKER_FLAGS += -t
38+
endif
39+
40+
.PHONY: build
41+
build: prebuild $(NAME) ## Builds a dynamic executable or package.
42+
43+
$(NAME): $(wildcard *.go) $(wildcard */*.go) VERSION.txt
44+
@echo "+ $@"
45+
$(GO) build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(NAME) .
46+
47+
.PHONY: static
48+
static: prebuild ## Builds a static executable.
49+
@echo "+ $@"
50+
CGO_ENABLED=$(CGO_ENABLED) $(GO) build \
51+
-tags "$(BUILDTAGS) static_build" \
52+
${GO_LDFLAGS_STATIC} -o $(NAME) .
53+
54+
all: clean build fmt lint test staticcheck vet install ## Runs a clean, build, fmt, lint, test, staticcheck, vet and install.
55+
56+
.PHONY: fmt
57+
fmt: ## Verifies all files have been `gofmt`ed.
58+
@echo "+ $@"
59+
@gofmt -s -l . | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr
60+
61+
.PHONY: lint
62+
lint: ## Verifies `golint` passes.
63+
@echo "+ $@"
64+
@golint ./... | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr
65+
66+
.PHONY: test
67+
test: prebuild ## Runs the go tests.
68+
@echo "+ $@"
69+
@$(GO) test -v -tags "$(BUILDTAGS) cgo" $(shell $(GO) list ./... | grep -v vendor)
70+
71+
.PHONY: vet
72+
vet: ## Verifies `go vet` passes.
73+
@echo "+ $@"
74+
@$(GO) vet $(shell $(GO) list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr
75+
76+
.PHONY: staticcheck
77+
staticcheck: ## Verifies `staticcheck` passes.
78+
@echo "+ $@"
79+
@staticcheck $(shell $(GO) list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr
80+
81+
.PHONY: cover
82+
cover: prebuild ## Runs go test with coverage.
83+
@echo "" > coverage.txt
84+
@for d in $(shell $(GO) list ./... | grep -v vendor); do \
85+
$(GO) test -race -coverprofile=profile.out -covermode=atomic "$$d"; \
86+
if [ -f profile.out ]; then \
87+
cat profile.out >> coverage.txt; \
88+
rm profile.out; \
89+
fi; \
90+
done;
91+
92+
.PHONY: install
93+
install: prebuild ## Installs the executable or package.
94+
@echo "+ $@"
95+
$(GO) install -a -tags "$(BUILDTAGS)" ${GO_LDFLAGS} .
96+
97+
define buildpretty
98+
mkdir -p $(BUILDDIR)/$(1)/$(2);
99+
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=$(CGO_ENABLED) $(GO) build \
100+
-o $(BUILDDIR)/$(1)/$(2)/$(NAME) \
101+
-a -tags "$(BUILDTAGS) static_build netgo" \
102+
-installsuffix netgo ${GO_LDFLAGS_STATIC} .;
103+
md5sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).md5;
104+
sha256sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).sha256;
105+
endef
106+
107+
.PHONY: cross
108+
cross: *.go VERSION.txt prebuild ## Builds the cross-compiled binaries, creating a clean directory structure (eg. GOOS/GOARCH/binary).
109+
@echo "+ $@"
110+
$(foreach GOOSARCH,$(GOOSARCHES), $(call buildpretty,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH))))
111+
112+
define buildrelease
113+
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=$(CGO_ENABLED) $(GO) build \
114+
-o $(BUILDDIR)/$(NAME)-$(1)-$(2) \
115+
-a -tags "$(BUILDTAGS) static_build netgo" \
116+
-installsuffix netgo ${GO_LDFLAGS_STATIC} .;
117+
md5sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).md5;
118+
sha256sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).sha256;
119+
endef
120+
121+
.PHONY: release
122+
release: *.go VERSION.txt prebuild ## Builds the cross-compiled binaries, naming them in such a way for release (eg. binary-GOOS-GOARCH).
123+
@echo "+ $@"
124+
$(foreach GOOSARCH,$(GOOSARCHES), $(call buildrelease,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH))))
125+
126+
.PHONY: bump-version
127+
BUMP := patch
128+
bump-version: ## Bump the version in the version file. Set BUMP to [ patch | major | minor ].
129+
@$(GO) get -u github.com/jessfraz/junk/sembump # update sembump tool
130+
$(eval NEW_VERSION = $(shell sembump --kind $(BUMP) $(VERSION)))
131+
@echo "Bumping VERSION.txt from $(VERSION) to $(NEW_VERSION)"
132+
echo $(NEW_VERSION) > VERSION.txt
133+
@echo "Updating links to download binaries in README.md"
134+
sed -i s/$(VERSION)/$(NEW_VERSION)/g README.md
135+
git add VERSION.txt README.md
136+
git commit -vsam "Bump version to $(NEW_VERSION)"
137+
@echo "Run make tag to create and push the tag for new version $(NEW_VERSION)"
138+
139+
.PHONY: tag
140+
tag: ## Create a new git tag to prepare to build a release.
141+
git tag -sa $(VERSION) -m "$(VERSION)"
142+
@echo "Run git push origin $(VERSION) to push your new tag to GitHub and trigger a travis build."
143+
144+
REGISTRY := r.j3ss.co
145+
.PHONY: image
146+
image: ## Create the docker image from the Dockerfile.
147+
@docker build --rm --force-rm -t $(REGISTRY)/$(NAME) .
148+
149+
.PHONY: image-dev
150+
image-dev:
151+
@docker build --rm --force-rm -f Dockerfile.dev -t $(REGISTRY)/$(NAME):dev .
152+
153+
.PHONY: AUTHORS
154+
AUTHORS:
155+
@$(file >$@,# This file lists all individuals having contributed content to the repository.)
156+
@$(file >>$@,# For how it is generated, see `make AUTHORS`.)
157+
@echo "$(shell git log --format='\n%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf)" >> $@
158+
159+
.PHONY: vendor
160+
vendor: ## Updates the vendoring directory.
161+
@$(RM) go.sum
162+
@$(RM) -r vendor
163+
GO111MODULE=on $(GO) mod init || true
164+
GO111MODULE=on $(GO) mod tidy
165+
GO111MODULE=on $(GO) mod vendor
166+
@$(RM) Gopkg.toml Gopkg.lock
167+
168+
.PHONY: clean
169+
clean: ## Cleanup any build binaries or packages.
170+
@echo "+ $@"
171+
$(RM) $(NAME)
172+
$(RM) -r $(BUILDDIR)
173+
174+
.PHONY: help
175+
help:
176+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | sed 's/^[^:]*://g' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
177+
178+
check_defined = \
179+
$(strip $(foreach 1,$1, \
180+
$(call __check_defined,$1,$(strip $(value 2)))))
181+
182+
__check_defined = \
183+
$(if $(value $1),, \
184+
$(error Undefined $1$(if $2, ($2))$(if $(value @), \
185+
required by target `$@')))

0 commit comments

Comments
 (0)