|
| 1 | +# System Monitor Server Makefile |
| 2 | +# This Makefile provides common build, test, and development tasks |
| 3 | + |
| 4 | +.PHONY: help build test clean run install deps lint fmt vet |
| 5 | + |
| 6 | +# Default target |
| 7 | +help: |
| 8 | + @echo "Available targets:" |
| 9 | + @echo " build - Build the system-monitor-server binary" |
| 10 | + @echo " test - Run all tests" |
| 11 | + @echo " clean - Clean build artifacts" |
| 12 | + @echo " run - Run the server in stdio mode" |
| 13 | + @echo " install - Install the binary to GOPATH/bin" |
| 14 | + @echo " deps - Download and verify dependencies" |
| 15 | + @echo " lint - Run linters" |
| 16 | + @echo " fmt - Format code" |
| 17 | + @echo " vet - Run go vet" |
| 18 | + @echo " docker - Build Docker image" |
| 19 | + @echo " examples - Run example commands" |
| 20 | + |
| 21 | +# Build the binary |
| 22 | +build: |
| 23 | + @echo "Building system-monitor-server..." |
| 24 | + @go build -o system-monitor-server ./cmd/server |
| 25 | + @echo "Build complete: system-monitor-server" |
| 26 | + |
| 27 | +# Run tests |
| 28 | +test: |
| 29 | + @echo "Running tests..." |
| 30 | + @go test -v -race -coverprofile=coverage.out ./... |
| 31 | + @go tool cover -html=coverage.out -o coverage.html |
| 32 | + @echo "Test coverage report: coverage.html" |
| 33 | + |
| 34 | +# Clean build artifacts |
| 35 | +clean: |
| 36 | + @echo "Cleaning build artifacts..." |
| 37 | + @rm -f system-monitor-server |
| 38 | + @rm -f coverage.out coverage.html |
| 39 | + @go clean |
| 40 | + |
| 41 | +# Run the server in stdio mode |
| 42 | +run: build |
| 43 | + @echo "Running system-monitor-server in stdio mode..." |
| 44 | + @./system-monitor-server -log-level=info |
| 45 | + |
| 46 | +# Install the binary |
| 47 | +install: build |
| 48 | + @echo "Installing system-monitor-server..." |
| 49 | + @go install ./cmd/server |
| 50 | + @echo "Installed to: $(shell go env GOPATH)/bin/system-monitor-server" |
| 51 | + |
| 52 | +# Download dependencies |
| 53 | +deps: |
| 54 | + @echo "Downloading dependencies..." |
| 55 | + @go mod download |
| 56 | + @go mod verify |
| 57 | + @echo "Dependencies downloaded and verified" |
| 58 | + |
| 59 | +# Run linters |
| 60 | +lint: |
| 61 | + @echo "Running linters..." |
| 62 | + @if command -v golangci-lint >/dev/null 2>&1; then \ |
| 63 | + golangci-lint run; \ |
| 64 | + else \ |
| 65 | + echo "golangci-lint not found, installing..."; \ |
| 66 | + go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \ |
| 67 | + golangci-lint run; \ |
| 68 | + fi |
| 69 | + |
| 70 | +# Format code |
| 71 | +fmt: |
| 72 | + @echo "Formatting code..." |
| 73 | + @go fmt ./... |
| 74 | + @if command -v goimports >/dev/null 2>&1; then \ |
| 75 | + goimports -w .; \ |
| 76 | + else \ |
| 77 | + echo "goimports not found, installing..."; \ |
| 78 | + go install golang.org/x/tools/cmd/goimports@latest; \ |
| 79 | + goimports -w .; \ |
| 80 | + fi |
| 81 | + |
| 82 | +# Run go vet |
| 83 | +vet: |
| 84 | + @echo "Running go vet..." |
| 85 | + @go vet ./... |
| 86 | + |
| 87 | +# Build Docker image |
| 88 | +docker: |
| 89 | + @echo "Building Docker image..." |
| 90 | + @docker build -t system-monitor-server:latest . |
| 91 | + @echo "Docker image built: system-monitor-server:latest" |
| 92 | + |
| 93 | +# Run example commands |
| 94 | +examples: build |
| 95 | + @echo "Running example commands..." |
| 96 | + @echo "" |
| 97 | + @echo "1. Get system metrics:" |
| 98 | + @echo 'echo '"'"'{"jsonrpc":"2.0","method":"tools/call","params":{"name":"get_system_metrics","arguments":{}},"id":1}'"'"' | ./system-monitor-server' |
| 99 | + @echo "" |
| 100 | + @echo "2. List processes:" |
| 101 | + @echo 'echo '"'"'{"jsonrpc":"2.0","method":"tools/call","params":{"name":"list_processes","arguments":{"sort_by":"cpu","limit":5}},"id":2}'"'"' | ./system-monitor-server' |
| 102 | + @echo "" |
| 103 | + @echo "3. Check service health:" |
| 104 | + @echo 'echo '"'"'{"jsonrpc":"2.0","method":"tools/call","params":{"name":"check_service_health","arguments":{"services":[{"name":"web","type":"http","target":"http://localhost:8080/health"}]}},"id":3}'"'"' | ./system-monitor-server' |
| 105 | + |
| 106 | +# Development server with hot reload |
| 107 | +dev: |
| 108 | + @echo "Starting development server with hot reload..." |
| 109 | + @if command -v air >/dev/null 2>&1; then \ |
| 110 | + air; \ |
| 111 | + else \ |
| 112 | + echo "air not found, installing..."; \ |
| 113 | + go install github.com/cosmtrek/air@latest; \ |
| 114 | + air; \ |
| 115 | + fi |
| 116 | + |
| 117 | +# Generate mocks for testing |
| 118 | +mocks: |
| 119 | + @echo "Generating mocks..." |
| 120 | + @if command -v mockgen >/dev/null 2>&1; then \ |
| 121 | + mockgen -source=internal/metrics/system.go -destination=internal/metrics/mocks/system_mock.go; \ |
| 122 | + mockgen -source=internal/metrics/process.go -destination=internal/metrics/mocks/process_mock.go; \ |
| 123 | + mockgen -source=internal/monitor/health_checker.go -destination=internal/monitor/mocks/health_checker_mock.go; \ |
| 124 | + mockgen -source=internal/monitor/log_monitor.go -destination=internal/monitor/mocks/log_monitor_mock.go; \ |
| 125 | + else \ |
| 126 | + echo "mockgen not found, installing..."; \ |
| 127 | + go install github.com/golang/mock/mockgen@latest; \ |
| 128 | + mockgen -source=internal/metrics/system.go -destination=internal/metrics/mocks/system_mock.go; \ |
| 129 | + mockgen -source=internal/metrics/process.go -destination=internal/metrics/mocks/process_mock.go; \ |
| 130 | + mockgen -source=internal/monitor/health_checker.go -destination=internal/monitor/mocks/health_checker_mock.go; \ |
| 131 | + mockgen -source=internal/monitor/log_monitor.go -destination=internal/monitor/mocks/log_monitor_mock.go; \ |
| 132 | + fi |
| 133 | + |
| 134 | +# Benchmark tests |
| 135 | +bench: |
| 136 | + @echo "Running benchmarks..." |
| 137 | + @go test -bench=. -benchmem ./... |
| 138 | + |
| 139 | +# Security scan |
| 140 | +security: |
| 141 | + @echo "Running security scan..." |
| 142 | + @if command -v gosec >/dev/null 2>&1; then \ |
| 143 | + gosec ./...; \ |
| 144 | + else \ |
| 145 | + echo "gosec not found, installing..."; \ |
| 146 | + go install github.com/securecodewarrior/gosec/v2/cmd/gosec@latest; \ |
| 147 | + gosec ./...; \ |
| 148 | + fi |
| 149 | + |
| 150 | +# All checks |
| 151 | +check: fmt vet lint test security |
| 152 | + @echo "All checks passed!" |
| 153 | + |
| 154 | +# Release build |
| 155 | +release: clean |
| 156 | + @echo "Building release binaries..." |
| 157 | + @GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o system-monitor-server-linux-amd64 ./cmd/server |
| 158 | + @GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o system-monitor-server-darwin-amd64 ./cmd/server |
| 159 | + @GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o system-monitor-server-windows-amd64.exe ./cmd/server |
| 160 | + @echo "Release binaries built:" |
| 161 | + @ls -la system-monitor-server-* |
0 commit comments