Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
318 lines (279 loc) · 10.9 KB
/
Copy pathMakefile
File metadata and controls
318 lines (279 loc) · 10.9 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# Check for .env file and include it
ifneq (,$(wildcard .env))
include .env
export
endif
# Version information
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
DATE ?= $(shell date +%FT%T%z)
LDFLAGS := -ldflags "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)"
PREFIX ?= /usr/local
# Package list excluding scripts/ (standalone utilities, not part of the module)
PACKAGES = $(shell go list ./... | grep -v /scripts)
# ============================================================================= #
# HELPERS
# ============================================================================= #
## help: Print this help message
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
.PHONY: confirm
confirm:
@echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ]
# ============================================================================= #
# DEVELOPMENT
# ============================================================================= #
## run: Run the sesh application (development mode)
.PHONY: run
run:
@echo "🚀 Running sesh in development mode (not installed to PATH)..."
@echo "ℹ️ For system-wide use, run 'make install' first."
@go run $(LDFLAGS) ./sesh/cmd/sesh/
# ============================================================================= #
# QUALITY CONTROL
# ============================================================================= #
## pre-commit: Run pre-commit checks on all files
.PHONY: pre-commit
pre-commit: format/check
@echo 'Checking compilation...'
@go build $(PACKAGES)
@echo 'Running go vet...'
@go vet $(PACKAGES)
@$(MAKE) lint/golangci
@echo '✅ All pre-commit checks passed!'
## dev/setup/hooks: Install git hooks for pre-commit checks
.PHONY: dev/setup/hooks
dev/setup/hooks:
@echo 'Installing git hooks...'
@if [ ! -f .githooks/pre-commit ]; then \
echo '❌ Error: .githooks/pre-commit not found'; \
exit 1; \
fi
@mkdir -p .git/hooks
@cp .githooks/pre-commit .git/hooks/pre-commit
@chmod +x .git/hooks/pre-commit
@echo '✅ Git hooks installed'
@echo 'Pre-commit hook will now check formatting and run go vet before each commit'
## format: Format all Go code with goimports
.PHONY: format
format:
@echo 'Formatting Go code...'
@if ! command -v goimports >/dev/null 2>&1; then \
echo "Installing goimports..."; \
go install golang.org/x/tools/cmd/goimports@latest; \
fi
@goimports -w -local github.com/bashhack/sesh $$(find . -name '*.go' -not -path "./vendor/*")
@echo '✅ Code formatted'
## format/check: Check if code is properly formatted (non-destructive)
.PHONY: format/check
format/check:
@echo 'Checking Go code formatting...'
@if ! command -v goimports >/dev/null 2>&1; then \
echo "Installing goimports..."; \
go install golang.org/x/tools/cmd/goimports@latest; \
fi
@if [ -n "$$(goimports -l -local github.com/bashhack/sesh $$(find . -name '*.go' -not -path './vendor/*'))" ]; then \
echo "❌ The following files need formatting:"; \
goimports -l -local github.com/bashhack/sesh $$(find . -name '*.go' -not -path './vendor/*'); \
echo "Run 'make format' to fix"; \
exit 1; \
fi
@echo '✅ All files properly formatted'
## lint: Run linters
.PHONY: lint
lint:
@echo 'Formatting code...'
@if ! command -v goimports >/dev/null 2>&1; then \
echo "Installing goimports..."; \
go install golang.org/x/tools/cmd/goimports@latest; \
fi
@goimports -w -local github.com/bashhack/sesh $$(find . -name '*.go' -not -path "./vendor/*")
@echo 'Vetting code...'
go vet $(PACKAGES)
$(MAKE) check-staticcheck
staticcheck $(PACKAGES)
@echo '✅ Linting complete'
## lint/golangci: Run golangci-lint (comprehensive linting tool)
.PHONY: lint/golangci
lint/golangci:
@echo 'Running golangci-lint...'
@REQUIRED_VERSION="2.14.0"; \
INSTALL_NEEDED=false; \
if ! command -v golangci-lint >/dev/null 2>&1; then \
echo "golangci-lint not found"; \
INSTALL_NEEDED=true; \
else \
CURRENT_VERSION=$$(golangci-lint --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1); \
if [ "$$CURRENT_VERSION" != "$$REQUIRED_VERSION" ]; then \
echo "golangci-lint version $$CURRENT_VERSION found, but v$$REQUIRED_VERSION required"; \
INSTALL_NEEDED=true; \
fi; \
fi; \
if [ "$$INSTALL_NEEDED" = "true" ]; then \
echo "Installing golangci-lint v$$REQUIRED_VERSION..."; \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $$(go env GOPATH)/bin v$$REQUIRED_VERSION; \
fi
@golangci-lint run ./...
@echo '✅ golangci-lint complete'
## lint/fieldalignment: Show fieldalignment suggestions safely (runs in /tmp, non-destructive)
.PHONY: lint/fieldalignment
lint/fieldalignment:
@echo 'Checking fieldalignment (safe mode - no files modified)...'
@if ! command -v fieldalignment >/dev/null 2>&1; then \
echo "Installing fieldalignment..."; \
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest; \
fi
@./scripts/fieldalignment_check.sh
## check-staticcheck: Check if staticcheck is installed
.PHONY: check-staticcheck
check-staticcheck:
@if ! command -v staticcheck >/dev/null 2>&1; then \
echo "Error: 'staticcheck' is not installed. Installing..."; \
go install honnef.co/go/tools/cmd/staticcheck@latest; \
fi
## security-scan: Run security vulnerability scanner
.PHONY: security-scan
security-scan:
@if ! command -v gosec >/dev/null 2>&1; then \
echo "Error: 'gosec' is not installed. Installing..."; \
go install github.com/securego/gosec/v2/cmd/gosec@latest; \
fi
gosec ./...
## audit: Tidy dependencies and format, vet and test all code
.PHONY: audit
audit:
@echo 'Tidying and verifying module dependencies...'
go mod tidy
go mod verify
@echo 'Formatting code...'
@if ! command -v goimports >/dev/null 2>&1; then \
echo "Installing goimports..."; \
go install golang.org/x/tools/cmd/goimports@latest; \
fi
@goimports -w -local github.com/bashhack/sesh $$(find . -name '*.go' -not -path "./vendor/*")
@echo 'Vetting code...'
go vet $(PACKAGES)
$(MAKE) check-staticcheck
staticcheck $(PACKAGES)
$(MAKE) lint/golangci
@echo 'Running tests...'
go test -race -vet=off $(PACKAGES)
@echo '✅ Audit complete'
## vendor: Tidy and vendor dependencies
.PHONY: vendor
vendor:
@echo 'Tidying and verifying module dependencies...'
go mod tidy
go mod verify
@echo 'Vendoring dependencies...'
go mod vendor
## test: Run test suite
.PHONY: test
test:
@echo 'Running tests...'
@go test -v $(PACKAGES)
## test/short: Run only fast tests, skipping slow or external tests
.PHONY: test/short
test/short:
@echo 'Running short tests only...'
@go test -short $(PACKAGES)
## test/verbose: Run tests with verbose output
.PHONY: test/verbose
test/verbose:
@echo 'Running tests with verbose output...'
@go test -v $(PACKAGES)
## coverage: Run test suite with coverage
.PHONY: coverage
coverage:
@echo 'Running tests with coverage...'
@go test -coverprofile=coverage.txt $(PACKAGES) | grep -v "no test files" | grep -v "coverage: 0.0%" || true
@echo 'Filtering out testutil, mock files, scripts, interface-only files, and main.go...'
@grep -v "testutil\|mock\|provider/interfaces.go\|scripts/\|cmd/sesh/main.go" coverage.txt > coverage.filtered.txt || true
@go tool cover -html=coverage.filtered.txt -o coverage.html
@echo "Coverage report generated at coverage.html"
@rm -f coverage.filtered.txt
## coverage/func: Show function-level coverage statistics
.PHONY: coverage/func
coverage/func:
@echo 'Generating function-level coverage report...'
@go test -coverprofile=coverage.txt $(PACKAGES) 2>&1 | grep -v "no test files" | grep -v "coverage: 0.0%" || true
@grep -v "testutil\|mock\|provider/interfaces.go\|scripts/\|cmd/sesh/main.go" coverage.txt > coverage.filtered.txt || true
@go tool cover -func=coverage.filtered.txt | grep -v "testutil\|mock\|provider/interfaces.go\|scripts/\|cmd/sesh/main.go" || true
@rm -f coverage.filtered.txt
# ============================================================================= #
# BUILD
# ============================================================================= #
## build: Build the sesh application
.PHONY: build
build:
@echo "Building sesh..."
@mkdir -p build
@go build $(LDFLAGS) -o build/sesh ./sesh/cmd/sesh
## agent-smoke: Build sesh and run the agent end-to-end smoke test
.PHONY: agent-smoke
agent-smoke: build
@scripts/agent-smoke.sh build/sesh
## build/optimize: Build optimized binary (smaller size)
.PHONY: build/optimize
build/optimize:
@echo "Building optimized binary..."
@go build -ldflags "-s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)" -o build/sesh ./sesh/cmd/sesh
## build/all: Build for all supported platforms
.PHONY: build/all
build/all:
@echo "Building for all platforms..."
@mkdir -p build/bin/
GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build $(LDFLAGS) -o build/bin/sesh-darwin-amd64 ./sesh/cmd/sesh
GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 go build $(LDFLAGS) -o build/bin/sesh-darwin-arm64 ./sesh/cmd/sesh
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o build/bin/sesh-linux-amd64 ./sesh/cmd/sesh
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build $(LDFLAGS) -o build/bin/sesh-linux-arm64 ./sesh/cmd/sesh
## install: Install the application
.PHONY: install
install: clean build
@echo "📦 Installing sesh..."
@echo "Installing to ~/.local/bin (standard user location)"
@mkdir -p $(HOME)/.local/bin
@cp build/sesh $(HOME)/.local/bin/
@chmod +x $(HOME)/.local/bin/sesh
@echo "✅ Installation complete!"
@if [[ ":$$PATH:" != *":$(HOME)/.local/bin:"* ]]; then \
echo "⚠️ Please add ~/.local/bin to your PATH:"; \
echo " export PATH=\"$$HOME/.local/bin:\$$PATH\""; \
fi
@echo ""
@echo "🚀 To get started:"
@echo " Run 'sesh' to see every command; the first 'sesh add' creates your vault"
# ============================================================================= #
# RELEASE
# ============================================================================= #
## release: Run GoReleaser in snapshot mode
.PHONY: release
release: confirm
@echo "Running GoReleaser in snapshot mode..."
@if command -v goreleaser > /dev/null; then \
goreleaser release --snapshot --clean; \
else \
echo "goreleaser not found, skipping"; \
fi
# ============================================================================= #
# VERSION INFO
# ============================================================================= #
## version: Print the current version
.PHONY: version
version:
@echo "Version: $(VERSION)"
@echo "Commit: $(COMMIT)"
@echo "Date: $(DATE)"
# ============================================================================= #
# CLEAN
# ============================================================================= #
## clean: Remove build artifacts
.PHONY: clean
clean:
@echo "Cleaning..."
@rm -f coverage.out coverage.txt coverage.html
@rm -rf bin/
@rm -rf dist/
@rm -rf build/