diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a080e1f --- /dev/null +++ b/Makefile @@ -0,0 +1,134 @@ +MODULE := raven +CMDS := $(notdir $(wildcard ./cmd/*)) +BINARIES := $(addprefix bin/,$(CMDS)) +CMD ?= raven + +GO := go +GOFMT := go fmt +GOVET := go vet +GOIMPORTS := go tool goimports +DEADCODE := go tool deadcode +GOVULNCHECK := go tool govulncheck + +BUILDFLAGS := -buildvcs=true +PLATFORMS := linux/amd64 + +.PHONY: all build build-all buildinfo check clean deadcode deps fmt help \ + imports install lint run test tidy tools uninstall verify vet \ + vulncheck $(CMDS) + +## Build all binaries (default target) +all: build + +## Display this help screen +help: + @awk ' \ + /^##/ { doc = substr($$0, 4); next } \ + /^[a-zA-Z0-9_-]+:/ && doc { \ + split($$1, target, ":"); \ + printf "\033[36m%-20s\033[0m %s\n", target[1], doc; \ + doc = "" \ + } \ + ' $(MAKEFILE_LIST) + +## Build all binaries to bin/ +build: $(BINARIES) + +## Build binaries for all defined platforms +build-all: + @for cmd in $(CMDS); do \ + for platform in $(PLATFORMS); do \ + echo "Building $$cmd for $$platform..."; \ + out="bin/$$cmd-$${platform%/*}-$${platform#*/}"; \ + GOOS=$${platform%/*} GOARCH=$${platform#*/} \ + $(GO) build $(BUILDFLAGS) -o $$out ./cmd/$$cmd; \ + done; \ + done + +## Build specific binaries (pattern rule) +bin/%: ./cmd/% + $(GO) build $(BUILDFLAGS) -o $@ ./$< + +## Shortcut targets to build specific commands (e.g., 'make raven') +$(CMDS): + $(GO) build $(BUILDFLAGS) -o bin/$@ ./cmd/$@ + +## Show version info for built binaries +buildinfo: build + @for bin in $(BINARIES); do \ + if [ -x $$bin ]; then \ + echo "Info for $$bin:"; \ + $(GO) version -m $$bin || true; \ + fi; \ + done + +## Run tests with race detection and coverage +test: + $(GO) test -cover -race ./... + +## Run all linters (fmt, vet, imports, deadcode, vulncheck) +lint: fmt vet imports deadcode vulncheck + +## Format code using go fmt +fmt: + $(GOFMT) ./... + +## Fix imports and format using goimports +imports: + $(GOIMPORTS) -w -local "$(MODULE)" . + +## Run go vet +vet: + $(GOVET) ./... + +## Find dead code +deadcode: + $(DEADCODE) ./... + +## Check for vulnerabilities +vulncheck: + $(GOVULNCHECK) ./... + +## Run integration checks (alias for lint) +check: lint + +## Tidy go.mod dependencies +tidy: + $(GO) mod tidy + +## Download dependencies +deps: + $(GO) mod download + +## Verify dependencies +verify: + $(GO) mod verify + +## Install development tools +tools: + go get -tool golang.org/x/tools/cmd/deadcode@latest + go get -tool golang.org/x/tools/cmd/goimports@latest + go get -tool golang.org/x/vuln/cmd/govulncheck@latest + +## Run the default binary (use ARGS="..." to pass arguments) +run: bin/$(CMD) + ./bin/$(CMD) $(ARGS) + +## Clean build artifacts +clean: + $(GO) clean + rm -f bin/* + +## Install binaries to $GOBIN +install: + $(GO) install $(BUILDFLAGS) ./cmd/... + +## Uninstall binaries from $GOBIN +uninstall: + @for cmd in $(CMDS); do \ + path=$$( $(GO) env GOBIN )/$$cmd; \ + if [ -z "$$( $(GO) env GOBIN )" ]; \ + then path=$$( $(GO) env GOPATH )/bin/$$cmd; fi; \ + echo "Removing $$path"; \ + rm -f $$path; \ + done