This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Makefile
169 lines (137 loc) · 4.83 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#############
# Variables #
#############
GO_SOURCE = $(shell find cmd pkg -type f -name '*.go') $(shell find cmd pkg -type f -name '*.tmpl')
CLI_SOURCE = $(GO_SOURCE) $(wildcard cmd/*.txt) go.mod go.sum
MOCKS = $(wildcard pkg/mocks/*.go)
REGO_LIB_SOURCE = $(shell find rego/lib -type f -name '*.rego')
REGO_RULES_SOURCE = $(shell find rego/rules -type f -name '*.rego')
GITCOMMIT = $(shell git rev-parse --short HEAD 2> /dev/null || true)
BUILD_TYPE ?= dev
define LDFLAGS
-X \"github.com/fugue/regula/v3/pkg/version.Version=$(VERSION)-$(BUILD_TYPE)\" \
-X \"github.com/fugue/regula/v3/pkg/version.GitCommit=$(GITCOMMIT)\"
endef
CLI_BUILD = go build -ldflags="$(LDFLAGS) -s -w"
GO_BIN_DIR= $(shell go env GOPATH)/bin
NEXT_MAJOR = $(shell $(CHANGIE) next major)
NEXT_MINOR = $(shell $(CHANGIE) next minor)
NEXT_PATCH = $(shell $(CHANGIE) next patch)
VERSION = $(shell $(CHANGIE) latest)
# Executables
GO ?= go
DOCKER ?= docker
MOCKGEN ?= $(GO_BIN_DIR)/mockgen
CHANGIE ?= $(GO_BIN_DIR)/changie
GORELEASER ?= $(GO_BIN_DIR)/goreleaser
# Internal targets
BINARY = bin/regula
INSTALLED_BINARY = /usr/local/bin/regula
REMEDIATION_LINKS = rego/remediation.yaml
YQ = $(DOCKER) run --rm -v $(shell pwd):/workdir mikefarah/yq:4
.PHONY: binary
binary: ## Build regula binary
binary: $(BINARY)
# https://gist.github.com/prwhite/8168133#gistcomment-3456785
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##############
# Swagger #
##############
SWAGGER_URL=https://api.riskmanager.fugue.co/v0/swagger
SWAGGER=swagger.yaml
$(SWAGGER):
wget -q -O $@ $(SWAGGER_URL)
.PHONY: swagger_gen
swagger_gen: ## Regenerate go-swagger bindings
swagger_gen: $(SWAGGER)
mkdir -p pkg/swagger
$(DOCKER) run --rm -it \
--volume $(shell pwd):/regula \
--user $(shell id -u):$(shell id -g) \
--workdir /regula \
quay.io/goswagger/swagger:v0.23.0 \
generate client -t pkg/swagger -f "$(SWAGGER)"
#############
# Tools #
#############
$(MOCKGEN):
$(GO) install github.com/golang/mock/mockgen@v1.5.0
$(CHANGIE):
$(GO) install github.com/miniscruff/changie@v1.7.0
$(GORELEASER):
$(GO) install github.com/goreleaser/goreleaser@v0.183.0
.PHONY: install_tools
install_tools: ## Download and install mockgen, changie, and goreleaser
install_tools: $(MOCKGEN) $(CHANGIE) $(GORELEASER)
##############
# Dev builds #
##############
$(BINARY): $(CLI_SOURCE) $(REGO_LIB_SOURCE) $(REGO_RULES_SOURCE) $(REMEDIATION_LINKS)
$(CLI_BUILD) -v -o $@
$(INSTALLED_BINARY): $(BINARY)
install -m 0755 $(BINARY) $(INSTALLED_BINARY)
.PHONY: install
install: ## Install regula
install: $(INSTALLED_BINARY)
.PHONY: clean
clean: ## Delete generated files
rm -f coverage.out
rm -f $(BINARY)
.PHONY: test
test: ## Run test suite
test:
$(GO) test -v -cover ./...
.PHONY: coverage
coverage: ## Run coverage analysis
$(GO) test ./... -coverprofile=coverage.out
$(GO) tool cover -html=coverage.out
.PHONY: lint
lint: ## Run the `go vet` linter
$(GO) vet ./...
.PHONY: docker
docker: ## Build Docker container
docker: $(CLI_SOURCE) $(REGO_LIB_SOURCE) $(REGO_RULES_SOURCE)
rm -rf dist
mkdir -p dist
GOOS=linux GOARCH=amd64 $(CLI_BUILD) -v -o dist/regula
cp Dockerfile dist
cd dist && $(DOCKER) build --tag fugue/regula:dev .
################################
# Remediation documentation #
################################
.PHONY: remediation
remediation: ## Generate remediation links
remediation:
curl -s "https://docs.fugue.co/remediation.html" | grep -Eo 'FG_R[0-9]+' | sort -u | awk '{ print $$1 ":\n url: https://docs.fugue.co/" $$1 ".html" }' > "rego/remediation.yaml"
#####################
# Release processes #
#####################
.PHONY: change
change: $(CHANGIE)
$(CHANGIE) new
define bump_rule
.PHONY: bump_$(1)_version
bump_$(1)_version: $$(CHANGIE) remediation
$$(CHANGIE) batch $(2)
$$(CHANGIE) merge
$$(YQ) eval --inplace '.extra.version = "$(2)"' ./mkdocs.yml
git add changes CHANGELOG.md mkdocs.yml rego/remediation.yaml
@echo "Run the following to complete the release:"
@echo " git commit -m 'Bump version to $(2)'"
@echo " git tag -a -F changes/$(2).md $(2)"
@echo " git push origin master $(2)"
endef
$(eval $(call bump_rule,major,$(NEXT_MAJOR)))
$(eval $(call bump_rule,minor,$(NEXT_MINOR)))
$(eval $(call bump_rule,patch,$(NEXT_PATCH)))
.PHONY: list_next_versions
list_next_versions:
@echo "The current version is: $(VERSION)"
@echo "The next patch version would be: $(NEXT_PATCH)"
@echo "The next minor version would be: $(NEXT_MINOR)"
@echo "The next major version would be: $(NEXT_MAJOR)"
# Only intended to be run as part of release automations to accommodate release
# candidates. Use the bump_* targets to prepare for an official release.
changes/%.md:
$(CHANGIE) batch $*