-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
274 lines (249 loc) · 9.38 KB
/
Makefile
File metadata and controls
274 lines (249 loc) · 9.38 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# Define the root directory of the project
LIB_AUTH := $(shell pwd)
#-------------------------------------------------------
# Color definitions (ANSI codes)
#-------------------------------------------------------
GREEN := \033[32m
RED := \033[31m
BOLD := \033[1m
NC := \033[0m
#-------------------------------------------------------
# Utility functions
#-------------------------------------------------------
# Check if a command exists
define check_command
@if ! command -v $(1) >/dev/null 2>&1; then \
echo "$(RED)$(BOLD)[error]$(NC) $(1) is not installed."; \
echo "Install it with: $(2)"; \
exit 1; \
fi
endef
# Print section title
define print_title
@echo ""
@echo "------------------------------------------"
@echo " 📝 $(1) "
@echo "------------------------------------------"
endef
# Include test targets
MK_DIR := $(abspath mk)
include $(MK_DIR)/tests.mk
#-------------------------------------------------------
# Help Command
#-------------------------------------------------------
.PHONY: help
help:
@echo ""
@echo ""
@echo "Lib-Auth Project Management Commands"
@echo ""
@echo ""
@echo "Core Commands:"
@echo " make help - Display this help message"
@echo " make test - Run all tests"
@echo " make build - Build all packages"
@echo " make clean - Clean all build artifacts"
@echo ""
@echo ""
@echo "Test Suite Commands:"
@echo " make test-unit - Run unit tests"
@echo " make test-integration - Run integration tests with testcontainers (RUN=<test>, LOW_RESOURCE=1)"
@echo " make test-all - Run all tests (unit + integration)"
@echo ""
@echo ""
@echo "Coverage Commands:"
@echo " make coverage-unit - Run unit tests with coverage report (PKG=./path, uses .ignorecoverunit)"
@echo " make coverage-integration - Run integration tests with coverage report (PKG=./path)"
@echo " make coverage - Run all coverage targets (unit + integration)"
@echo ""
@echo ""
@echo "Test Tooling:"
@echo " make tools - Install test tools (gotestsum)"
@echo ""
@echo ""
@echo "Code Quality Commands:"
@echo " make lint - Run linting on all packages"
@echo " make format - Format code in all packages"
@echo " make tidy - Clean dependencies"
@echo " make check-tests - Verify test coverage for packages"
@echo " make sec - Run security checks using gosec"
@echo " make sec SARIF=1 - Run security checks with SARIF output"
@echo ""
@echo ""
@echo "Git Hook Commands:"
@echo " make setup-git-hooks - Install and configure git hooks"
@echo " make check-hooks - Verify git hooks installation status"
@echo " make check-envs - Check if github hooks are installed and secret env files are not exposed"
@echo ""
@echo ""
@echo "Release Commands:"
@echo " make goreleaser - Create release snapshot with goreleaser"
@echo ""
@echo ""
#-------------------------------------------------------
# Core Commands
#-------------------------------------------------------
.PHONY: build
build:
$(call print_title,Building all packages)
$(call check_command,go,"Install Go from https://golang.org/doc/install")
go build ./...
@echo "$(GREEN)$(BOLD)[ok]$(NC) All packages built successfully$(GREEN) ✔️$(NC)"
.PHONY: clean
clean:
$(call print_title,Cleaning build artifacts)
@rm -rf ./bin ./dist ./reports coverage.out coverage.html
@go clean -cache -testcache
@echo "$(GREEN)$(BOLD)[ok]$(NC) All build artifacts cleaned$(GREEN) ✔️$(NC)"
#-------------------------------------------------------
# Code Quality Commands
#-------------------------------------------------------
.PHONY: lint
lint:
$(call print_title,Running linters on all packages)
$(call check_command,golangci-lint,"go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest")
@out=$$(golangci-lint run --fix ./... 2>&1); \
out_err=$$?; \
echo "$$out"; \
if [ $$out_err -ne 0 ]; then \
echo -e "\n$(BOLD)$(RED)An error has occurred during the lint process: \n $$out\n"; \
exit 1; \
fi
@echo "$(GREEN)$(BOLD)[ok]$(NC) Lint checks passed successfully$(GREEN) ✔️$(NC)"
.PHONY: format
format:
$(call print_title,Formatting code in all packages)
$(call check_command,gofmt,"Install Go from https://golang.org/doc/install")
@gofmt -w ./
@if command -v goimports >/dev/null 2>&1; then \
goimports -w .; \
else \
echo "goimports not found, skipping import organization"; \
echo "Install with: go install golang.org/x/tools/cmd/goimports@latest"; \
fi
@echo "$(GREEN)$(BOLD)[ok]$(NC) All go files formatted$(GREEN) ✔️$(NC)"
.PHONY: check-tests
check-tests:
$(call print_title,Verifying test coverage for packages)
@if [ -f "./scripts/check-tests.sh" ]; then \
sh ./scripts/check-tests.sh; \
else \
echo "Running basic test coverage check..."; \
go test -cover ./...; \
fi
@echo "$(GREEN)$(BOLD)[ok]$(NC) Test coverage verification completed$(GREEN) ✔️$(NC)"
#-------------------------------------------------------
# Git Hook Commands
#-------------------------------------------------------
.PHONY: setup-git-hooks
setup-git-hooks:
$(call print_title,Installing and configuring git hooks)
@if [ -d ".githooks" ]; then \
for hook in .githooks/*; do \
if [ -f "$$hook" ]; then \
hook_name=$$(basename $$hook); \
cp "$$hook" ".git/hooks/$$hook_name"; \
chmod +x ".git/hooks/$$hook_name"; \
echo "Installed $$hook_name"; \
fi; \
done; \
else \
echo "No .githooks directory found"; \
fi
@echo "$(GREEN)$(BOLD)[ok]$(NC) All hooks installed and updated$(GREEN) ✔️$(NC)"
.PHONY: check-hooks
check-hooks:
$(call print_title,Verifying git hooks installation status)
@err=0; \
if [ -d ".githooks" ]; then \
for hook in .githooks/*; do \
if [ -f "$$hook" ]; then \
hook_name=$$(basename $$hook); \
f=".githooks/$$hook_name"; \
FILE2=.git/hooks/$$hook_name; \
if [ -f "$$FILE2" ]; then \
if cmp -s "$$hook" "$$FILE2"; then \
echo "$(GREEN)$(BOLD)[ok]$(NC) Hook file $$f installed and updated$(GREEN) ✔️$(NC)"; \
else \
echo "$(RED)Hook file $$f installed but out-of-date [OUT-OF-DATE] ✗$(NC)"; \
err=1; \
fi; \
else \
echo "$(RED)Hook file $$f not installed [NOT INSTALLED] ✗$(NC)"; \
err=1; \
fi; \
fi; \
done; \
else \
echo "No .githooks directory found"; \
fi; \
if [ $$err -ne 0 ]; then \
echo ""; \
echo "Run $(BOLD)make setup-git-hooks$(NC) to setup your development environment, then try again."; \
echo ""; \
exit 1; \
else \
echo "$(GREEN)$(BOLD)[ok]$(NC) All hooks are properly installed$(GREEN) ✔️$(NC)"; \
fi
.PHONY: check-envs
check-envs:
$(call print_title,Checking git hooks and environment files for security issues)
$(MAKE) check-hooks
@echo "Checking for exposed secrets in environment files..."
@if grep -rq "SECRET.*=" --include=".env" .; then \
echo "$(RED)Warning: Secrets found in environment files. Make sure these are not committed to the repository.$(NC)"; \
exit 1; \
else \
echo "$(GREEN)No exposed secrets found in environment files$(GREEN) ✔️$(NC)"; \
fi
@echo "$(GREEN)$(BOLD)[ok]$(NC) Environment check completed$(GREEN) ✔️$(NC)"
#-------------------------------------------------------
# Development Commands
#-------------------------------------------------------
.PHONY: tidy
tidy:
$(call print_title,Cleaning dependencies)
$(call check_command,go,"Install Go from https://golang.org/doc/install")
go mod tidy
@echo "$(GREEN)$(BOLD)[ok]$(NC) Dependencies cleaned successfully$(GREEN) ✔️$(NC)"
# SARIF output for GitHub Security tab integration (optional)
# Usage: make sec SARIF=1
SARIF ?= 0
.PHONY: sec
sec:
$(call print_title,Running security checks using gosec)
@if ! command -v gosec >/dev/null 2>&1; then \
echo "Installing gosec..."; \
go install github.com/securego/gosec/v2/cmd/gosec@latest; \
fi
@if find . -name "*.go" -type f -not -path './vendor/*' | grep -q .; then \
echo "Running security checks on all packages..."; \
if [ "$(SARIF)" = "1" ]; then \
echo "Generating SARIF output: gosec-report.sarif"; \
if gosec -fmt sarif -out gosec-report.sarif ./...; then \
echo "$(GREEN)$(BOLD)[ok]$(NC) SARIF report generated: gosec-report.sarif$(GREEN) ✔️$(NC)"; \
else \
echo -e "\n$(BOLD)$(RED)Security issues found by gosec. Please address them before proceeding.$(NC)\n"; \
echo "SARIF report with details: gosec-report.sarif"; \
exit 1; \
fi; \
else \
if gosec ./...; then \
echo "$(GREEN)$(BOLD)[ok]$(NC) Security checks completed$(GREEN) ✔️$(NC)"; \
else \
echo -e "\n$(BOLD)$(RED)Security issues found by gosec. Please address them before proceeding.$(NC)\n"; \
exit 1; \
fi; \
fi; \
else \
echo "No Go files found, skipping security checks"; \
fi
#-------------------------------------------------------
# Release Commands
#-------------------------------------------------------
.PHONY: goreleaser
goreleaser:
$(call print_title,Creating release snapshot with goreleaser)
$(call check_command,goreleaser,"go install github.com/goreleaser/goreleaser@latest")
goreleaser release --snapshot --skip-publish --clean
@echo "$(GREEN)$(BOLD)[ok]$(NC) Release snapshot created successfully$(GREEN) ✔️$(NC)"