Skip to content

Commit 9ab8753

Browse files
raphaeldouglaswth
andauthored
Example for interceptors (#155)
* Add interceptors example * go mod tidy * Improve interceptors example README * Progress on streaming interceptors in example which uncovered a generation bug * Fix Goa generation with goadesign/goa#3652 * Generate with interceptor fixes from goadesign/goa#3655 --------- Co-authored-by: Douglas Thrift <[email protected]>
1 parent f1d20f0 commit 9ab8753

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+7791
-1596
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ tus/upload-cli
3535
upload_download/upload_download
3636
upload_download/upload_download-cli
3737
basic/cmd/calc/calc
38+
interceptors/cmd/example/example
39+
.vscode/settings.json
40+
interceptors/cmd/tokgen/tokgen
41+
interceptors/bin/

basic/Makefile

+3-96
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,7 @@
11
#! /usr/bin/make
22
#
3-
# Makefile for Goa examples
4-
#
5-
# Targets:
6-
# - "depend" retrieves the Go packages needed to run the linter and tests
7-
# - "gen" invokes the "goa" tool to generate the examples source code
8-
# - "build" compiles the example microservices and client CLIs
9-
# - "clean" deletes the output of "build"
10-
# - "lint" runs the linter and checks the code format using goimports
11-
# - "test" runs the tests
12-
#
13-
# Meta targets:
14-
# - "all" is the default target, it runs all the targets in the order above.
15-
#
16-
GO_FILES=$(shell find . -type f -name '*.go')
17-
MODULE=$(shell go list -m)
18-
APP=calc
19-
20-
# Only list test and build dependencies
21-
# Standard dependencies are installed via go get
22-
DEPEND=\
23-
github.com/hashicorp/go-getter \
24-
github.com/cheggaaa/pb \
25-
github.com/golang/protobuf/protoc-gen-go \
26-
github.com/golang/protobuf/proto \
27-
goa.design/goa/... \
28-
golang.org/x/lint/golint \
29-
golang.org/x/tools/cmd/goimports \
30-
honnef.co/go/tools/cmd/staticcheck
31-
32-
.phony: all depend lint test build clean
33-
34-
all: gen lint test
35-
@echo DONE!
36-
37-
# Install protoc
38-
GOOS=$(shell go env GOOS)
39-
PROTOC_VERSION=3.6.1
40-
ifeq ($(GOOS),linux)
41-
PROTOC=protoc-$(PROTOC_VERSION)-linux-x86_64
42-
PROTOC_EXEC=$(PROTOC)/bin/protoc
43-
GOBIN=$(GOPATH)/bin
44-
else
45-
ifeq ($(GOOS),darwin)
46-
PROTOC=protoc-$(PROTOC_VERSION)-osx-x86_64
47-
PROTOC_EXEC=$(PROTOC)/bin/protoc
48-
GOBIN=$(GOPATH)/bin
49-
else
50-
ifeq ($(GOOS),windows)
51-
PROTOC=protoc-$(PROTOC_VERSION)-win32
52-
PROTOC_EXEC="$(PROTOC)\bin\protoc.exe"
53-
GOBIN="$(GOPATH)\bin"
54-
endif
55-
endif
56-
endif
57-
depend:
58-
@echo INSTALLING DEPENDENCIES...
59-
@go get -v $(DEPEND)
60-
@go install github.com/hashicorp/go-getter/cmd/go-getter && \
61-
go-getter https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/$(PROTOC).zip $(PROTOC) && \
62-
cp $(PROTOC_EXEC) $(GOBIN) && \
63-
rm -r $(PROTOC)
64-
@go install github.com/golang/protobuf/protoc-gen-go
65-
@go get -t -v ./...
66-
67-
lint:
68-
@echo LINTING CODE...
69-
@if [ "`goimports -l $(GO_FILES) | grep -v .pb.go | tee /dev/stderr`" ]; then \
70-
echo "^ - Repo contains improperly formatted go files" && echo && exit 1; \
71-
fi
72-
@if [ "`staticcheck ./... | grep -v ".pb.go" | tee /dev/stderr`" ]; then \
73-
echo "^ - staticcheck errors!" && echo && exit 1; \
74-
fi
75-
76-
.PHONY: gen
77-
gen: clean
78-
@echo GENERATING CODE...
79-
@goa gen "$(MODULE)/design" && \
80-
goa example "$(MODULE)/design"
81-
82-
build:
83-
@go build "./cmd/$(APP)" && go build "./cmd/$(APP)-cli"
84-
85-
clean:
86-
@rm -rf "./cmd/$(APP)" "./cmd/$(APP)-cli"
3+
# Makefile for Goa interceptors example
874

88-
test:
89-
@echo TESTING...
90-
@go test ./... > /dev/null
5+
APP=basic
916

92-
check-freshness:
93-
@if [ "`git diff | wc -l`" -gt "0" ]; then \
94-
echo "[ERROR] generated code not in-sync with design:"; \
95-
echo; \
96-
git status -s; \
97-
git --no-pager diff; \
98-
echo; \
99-
exit 1; \
100-
fi
7+
include ../common.mk

basic/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/gohugoio/hashstructure v0.5.0 // indirect
2222
github.com/google/uuid v1.6.0 // indirect
2323
github.com/gorilla/websocket v1.5.3 // indirect
24-
github.com/kr/pretty v0.1.0 // indirect
24+
github.com/kr/pretty v0.3.1 // indirect
2525
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d // indirect
2626
github.com/pmezard/go-difflib v1.0.0 // indirect
2727
github.com/stretchr/testify v1.10.0 // indirect

basic/go.sum

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
22
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
3+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
34
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
45
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
56
github.com/dimfeld/httppath v0.0.0-20170720192232-ee938bf73598 h1:MGKhKyiYrvMDZsmLR/+RGffQSXwEkXgfLSA08qDn9AI=
@@ -22,15 +23,18 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2223
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2324
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
2425
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
25-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
26-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
26+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
27+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
28+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
2729
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
28-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2930
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
31+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
32+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
3033
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d h1:Zj+PHjnhRYWBK6RqCDBcAhLXoi3TzC27Zad/Vn+gnVQ=
3134
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d/go.mod h1:WZy8Q5coAB1zhY9AOBJP0O6J4BuDfbupUDavKY+I3+s=
3235
github.com/manveru/gobdd v0.0.0-20131210092515-f1a17fdd710b h1:3E44bLeN8uKYdfQqVQycPnaVviZdBLbizFhU49mtbe4=
3336
github.com/manveru/gobdd v0.0.0-20131210092515-f1a17fdd710b/go.mod h1:Bj8LjjP0ReT1eKt5QlKjwgi5AFm5mI6O1A2G4ChI0Ag=
37+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
3438
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3539
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3640
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
@@ -72,7 +76,7 @@ google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40Rmc
7276
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
7377
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
7478
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
75-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
76-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
79+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
80+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
7781
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7882
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

cellar/Makefile

+3-97
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,7 @@
11
#! /usr/bin/make
22
#
3-
# Makefile for Goa examples
4-
#
5-
# Targets:
6-
# - "depend" retrieves the Go packages needed to run the linter and tests
7-
# - "gen" invokes the "goa" tool to generate the examples source code
8-
# - "build" compiles the example microservices and client CLIs
9-
# - "clean" deletes the output of "build"
10-
# - "lint" runs the linter and checks the code format using goimports
11-
# - "test" runs the tests
12-
#
13-
# Meta targets:
14-
# - "all" is the default target, it runs all the targets in the order above.
15-
#
16-
GO_FILES=$(shell find . -type f -name '*.go')
17-
MODULE=$(shell go list -m)
18-
APP=cellar
19-
20-
# Only list test and build dependencies
21-
# Standard dependencies are installed via go get
22-
DEPEND=\
23-
github.com/hashicorp/go-getter \
24-
github.com/cheggaaa/pb \
25-
github.com/golang/protobuf/protoc-gen-go \
26-
github.com/golang/protobuf/proto \
27-
goa.design/goa/... \
28-
golang.org/x/lint/golint \
29-
golang.org/x/tools/cmd/goimports \
30-
honnef.co/go/tools/cmd/staticcheck
31-
32-
.phony: all depend lint test build clean
33-
34-
all: gen lint test
35-
@echo DONE!
3+
# Makefile for Goa v3 cellar example
364

37-
# Install protoc
38-
GOOS=$(shell go env GOOS)
39-
PROTOC_VERSION=3.6.1
40-
ifeq ($(GOOS),linux)
41-
PROTOC=protoc-$(PROTOC_VERSION)-linux-x86_64
42-
PROTOC_EXEC=$(PROTOC)/bin/protoc
43-
GOBIN=$(GOPATH)/bin
44-
else
45-
ifeq ($(GOOS),darwin)
46-
PROTOC=protoc-$(PROTOC_VERSION)-osx-x86_64
47-
PROTOC_EXEC=$(PROTOC)/bin/protoc
48-
GOBIN=$(GOPATH)/bin
49-
else
50-
ifeq ($(GOOS),windows)
51-
PROTOC=protoc-$(PROTOC_VERSION)-win32
52-
PROTOC_EXEC="$(PROTOC)\bin\protoc.exe"
53-
GOBIN="$(GOPATH)\bin"
54-
endif
55-
endif
56-
endif
57-
depend:
58-
@echo INSTALLING DEPENDENCIES...
59-
@go get -v $(DEPEND)
60-
@go install github.com/hashicorp/go-getter/cmd/go-getter && \
61-
go-getter https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/$(PROTOC).zip $(PROTOC) && \
62-
cp $(PROTOC_EXEC) $(GOBIN) && \
63-
rm -r $(PROTOC)
64-
@go install github.com/golang/protobuf/protoc-gen-go
65-
@go get -t -v ./...
66-
67-
lint:
68-
@echo LINTING CODE...
69-
@if [ "`goimports -l $(GO_FILES) | grep -v .pb.go | tee /dev/stderr`" ]; then \
70-
echo "^ - Repo contains improperly formatted go files" && echo && exit 1; \
71-
fi
72-
@if [ "`staticcheck ./... | grep -v ".pb.go" | tee /dev/stderr`" ]; then \
73-
echo "^ - staticcheck errors!" && echo && exit 1; \
74-
fi
75-
76-
.PHONY: gen
77-
gen:
78-
@# NOTE: not all command line tools are generated
79-
@echo GENERATING CODE...
80-
@goa gen "$(MODULE)/design" && \
81-
goa example "$(MODULE)/design"
82-
83-
build:
84-
@go build "./cmd/$(APP)" && go build "./cmd/$(APP)-cli"
85-
86-
clean:
87-
@rm -rf "./cmd/$(APP)" "./cmd/$(APP)-cli"
88-
89-
test:
90-
@echo TESTING...
91-
@go test ./... > /dev/null
5+
APP=cellar
926

93-
check-freshness:
94-
@if [ "`git diff | wc -l`" -gt "0" ]; then \
95-
echo "[ERROR] generated code not in-sync with design:"; \
96-
echo; \
97-
git status -s; \
98-
git --no-pager diff; \
99-
echo; \
100-
exit 1; \
101-
fi
7+
include ../common.mk

cellar/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/gohugoio/hashstructure v0.5.0 // indirect
1919
github.com/google/uuid v1.6.0 // indirect
2020
github.com/gorilla/websocket v1.5.3 // indirect
21-
github.com/kr/pretty v0.1.0 // indirect
21+
github.com/kr/pretty v0.3.1 // indirect
2222
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d // indirect
2323
github.com/pmezard/go-difflib v1.0.0 // indirect
2424
github.com/stretchr/testify v1.10.0 // indirect

cellar/go.sum

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
22
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
3+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
34
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
45
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
56
github.com/dimfeld/httppath v0.0.0-20170720192232-ee938bf73598 h1:MGKhKyiYrvMDZsmLR/+RGffQSXwEkXgfLSA08qDn9AI=
@@ -20,15 +21,18 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2021
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2122
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
2223
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
23-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
24-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
24+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
25+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
26+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
2527
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
26-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2728
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
29+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
30+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2831
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d h1:Zj+PHjnhRYWBK6RqCDBcAhLXoi3TzC27Zad/Vn+gnVQ=
2932
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d/go.mod h1:WZy8Q5coAB1zhY9AOBJP0O6J4BuDfbupUDavKY+I3+s=
3033
github.com/manveru/gobdd v0.0.0-20131210092515-f1a17fdd710b h1:3E44bLeN8uKYdfQqVQycPnaVviZdBLbizFhU49mtbe4=
3134
github.com/manveru/gobdd v0.0.0-20131210092515-f1a17fdd710b/go.mod h1:Bj8LjjP0ReT1eKt5QlKjwgi5AFm5mI6O1A2G4ChI0Ag=
35+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
3236
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3337
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3438
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
@@ -64,7 +68,7 @@ google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40Rmc
6468
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
6569
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
6670
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
67-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
68-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
71+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
72+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
6973
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7074
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

common.mk

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Common variables
2+
GO_FILES=$(shell find . -type f -name '*.go')
3+
MODULE=$(shell head -n1 go.mod | cut -d ' ' -f2)
4+
5+
# Dependencies
6+
DEPEND=\
7+
github.com/hashicorp/go-getter \
8+
github.com/cheggaaa/pb \
9+
github.com/golang/protobuf/protoc-gen-go \
10+
github.com/golang/protobuf/proto \
11+
goa.design/goa/... \
12+
golang.org/x/lint/golint \
13+
golang.org/x/tools/cmd/goimports \
14+
honnef.co/go/tools/cmd/staticcheck
15+
16+
# Protoc setup
17+
GOOS=$(shell go env GOOS)
18+
PROTOC_VERSION=28.3
19+
ifeq ($(GOOS),linux)
20+
PROTOC=protoc-$(PROTOC_VERSION)-linux-x86_64
21+
PROTOC_EXEC=$(PROTOC)/bin/protoc
22+
GOBIN=$(GOPATH)/bin
23+
else
24+
ifeq ($(GOOS),darwin)
25+
PROTOC=protoc-$(PROTOC_VERSION)-osx-x86_64
26+
PROTOC_EXEC=$(PROTOC)/bin/protoc
27+
GOBIN=$(GOPATH)/bin
28+
else
29+
ifeq ($(GOOS),windows)
30+
PROTOC=protoc-$(PROTOC_VERSION)-win32
31+
PROTOC_EXEC="$(PROTOC)\bin\protoc.exe"
32+
GOBIN="$(GOPATH)\bin"
33+
endif
34+
endif
35+
endif
36+
37+
# Common targets
38+
.PHONY: all depend lint test build clean gen check-freshness
39+
40+
all: gen lint test
41+
@echo DONE!
42+
43+
depend:
44+
@echo INSTALLING DEPENDENCIES...
45+
@go get -v $(DEPEND)
46+
@go install github.com/hashicorp/go-getter/cmd/go-getter && \
47+
go-getter https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/$(PROTOC).zip $(PROTOC) && \
48+
cp $(PROTOC_EXEC) $(GOBIN) && \
49+
rm -r $(PROTOC)
50+
@go install github.com/golang/protobuf/protoc-gen-go
51+
@go get -t -v ./...
52+
53+
lint:
54+
@echo LINTING CODE...
55+
@if [ "`goimports -l $(GO_FILES) | grep -v .pb.go | tee /dev/stderr`" ]; then \
56+
echo "^ - Repo contains improperly formatted go files" && echo && exit 1; \
57+
fi
58+
@if [ "`staticcheck ./... | grep -v ".pb.go" | tee /dev/stderr`" ]; then \
59+
echo "^ - staticcheck errors!" && echo && exit 1; \
60+
fi
61+
62+
gen:
63+
@echo GENERATING CODE...
64+
@goa gen "$(MODULE)/design" && \
65+
goa example "$(MODULE)/design"
66+
67+
build:
68+
@go build "./cmd/$(APP)" && go build "./cmd/$(APP)-cli"
69+
70+
clean:
71+
@rm -rf "./cmd/$(APP)" "./cmd/$(APP)-cli"
72+
73+
test:
74+
@echo TESTING...
75+
@go test ./... > /dev/null
76+
77+
check-freshness:
78+
@if [ "`git diff | wc -l`" -gt "0" ]; then \
79+
echo "[ERROR] generated code not in-sync with design:"; \
80+
echo; \
81+
git status -s; \
82+
git --no-pager diff; \
83+
echo; \
84+
exit 1; \
85+
fi

0 commit comments

Comments
 (0)