#!/usr/bin/env bash set -euo pipefail # Universal Go Project Template # This template can be used to quickly set up a new Go project with standardized structure and tooling PROJECT_NAME="${1:-}" if [ -z "$PROJECT_NAME" ]; then echo "Usage: $0 " echo "Creates a new Go project template with standardized structure and tooling" exit 1 fi echo "Creating new Go project: $PROJECT_NAME" # Create project directory structure mkdir -p "$PROJECT_NAME"/{cmd,pkg,scripts,docs,tests,examples,tools,config,logs,reports,storage} # Create main.go in cmd directory cat > "$PROJECT_NAME/cmd/main.go" << EOF package main import ( "fmt" "log" ) func main() { fmt.Println("Hello, $PROJECT_NAME!") log.Println("Starting $PROJECT_NAME application...") // Your application logic here log.Println("Application finished.") } EOF # Create README cat > "$PROJECT_NAME/README.md" << EOF # $PROJECT_NAME A Go project with standardized structure and tooling. ## Project Structure - \`cmd/\` - Main applications - \`pkg/\` - Library code - \`scripts/\` - Build and deployment scripts - \`tests/\` - Test files (unit, integration, e2e) - \`docs/\` - Documentation - \`examples/\` - Example code - \`tools/\` - Development tools - \`config/\` - Configuration files ## Getting Started 1. Install dependencies: \`make deps\` 2. Run tests: \`make test\` 3. Build: \`make build\` 4. Run: \`make run\` ## Development Commands \`\`\` make build # Build the application make test # Run tests make test-coverage # Run tests with coverage make lint # Lint the code make fmt # Format the code make vet # Vet the code make run # Build and run the application make dev-setup # Setup development environment make audit-full # Run comprehensive audit \`\`\` ## License MIT EOF # Create go.mod cat > "$PROJECT_NAME/go.mod" << EOF module $PROJECT_NAME go 1.24 require ( ) EOF # Create comprehensive Makefile cat > "$PROJECT_NAME/Makefile" << 'MAKEFILE' # Universal Go Project Makefile Template # Variables BINARY=$(notdir $(CURDIR)) BINARY_PATH=bin/$(BINARY) MAIN_FILE=cmd/main.go # Default target .PHONY: all all: build # Build the application .PHONY: build build: @echo "Building $(BINARY)..." @mkdir -p bin @go build -o $(BINARY_PATH) $(MAIN_FILE) @echo "Build successful!" # Build with race detection .PHONY: build-race build-race: @echo "Building $(BINARY) with race detection..." @mkdir -p bin @go build -race -o $(BINARY_PATH) $(MAIN_FILE) @echo "Race-build successful!" # Run the application .PHONY: run run: build @echo "Running $(BINARY)..." @$(BINARY_PATH) # Run the application in development mode .PHONY: run-dev run-dev: @echo "Running $(BINARY) in development mode..." @go run $(MAIN_FILE) # Multi-level Testing System # Basic tests (fast) .PHONY: test-basic test-basic: @echo "Running basic tests (fast)..." @go test -v -short ./... # Unit tests .PHONY: test-unit test-unit: @echo "Running unit tests..." @go test -v ./tests/unit/... ./pkg/... # Integration tests .PHONY: test-integration test-integration: @echo "Running integration tests..." @go test -v ./tests/integration/... # End-to-end tests .PHONY: test-e2e test-e2e: @echo "Running end-to-end tests..." @go test -v ./tests/e2e/... # Property tests .PHONY: test-property test-property: @echo "Running property tests..." @go test -v ./tests/property/... # Fuzzing tests .PHONY: test-fuzzing test-fuzzing: @echo "Running fuzzing tests..." @go test -v ./tests/fuzzing/... # Stress tests .PHONY: test-stress test-stress: @echo "Running stress tests..." @go test -v ./tests/stress/... # Security tests .PHONY: test-security test-security: @echo "Running security tests..." @go test -v ./tests/security/... # Benchmark tests .PHONY: test-bench test-bench: @echo "Running benchmark tests..." @go test -bench=. -benchmem -run=^$$ ./... # Comprehensive tests (all test types) .PHONY: test-comprehensive test-comprehensive: @echo "Running comprehensive tests..." @$(MAKE) test-unit @$(MAKE) test-integration @$(MAKE) test-e2e # Full audit tests (comprehensive + security + stress + benchmarks) .PHONY: test-audit test-audit: @echo "Running full audit tests..." @$(MAKE) test-comprehensive @$(MAKE) test-security @$(MAKE) test-stress @$(MAKE) test-bench # Run tests with coverage .PHONY: test-coverage test-coverage: @echo "Running tests with coverage..." @go test -coverprofile=coverage.out ./... @go tool cover -html=coverage.out -o coverage.html @echo "Coverage report generated: coverage.html" # Run tests with coverage for specific package .PHONY: test-coverage-pkg test-coverage-pkg: @echo "Running tests with coverage for specific package..." @go test -coverprofile=coverage.out $(PKG) && go tool cover -html=coverage.out -o coverage.html @echo "Coverage report generated: coverage.html" # Code Quality Tools # Format code .PHONY: fmt fmt: @echo "Formatting code..." @go fmt ./... # Vet code .PHONY: vet vet: @echo "Vetting code..." @go vet ./... # Lint code (requires golangci-lint) .PHONY: lint lint: @echo "Linting code..." @which golangci-lint > /dev/null || (echo "golangci-lint not found, installing..." && go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest) @golangci-lint run # Security audit .PHONY: audit-security audit-security: @echo "Running security audit..." @which gosec > /dev/null || (echo "gosec not found, installing..." && go install github.com/securego/gosec/v2/cmd/gosec@latest) @gosec ./... @which govulncheck > /dev/null || (echo "govulncheck not found, installing..." && go install golang.org/x/vuln/cmd/govulncheck@latest) @govulncheck ./... # Dependency audit .PHONY: audit-deps audit-deps: @echo "Running dependency audit..." @go list -m -u all @govulncheck ./... # Code quality audit .PHONY: audit-quality audit-quality: @echo "Running code quality audit..." @$(MAKE) vet @$(MAKE) lint # Comprehensive audit (all checks) .PHONY: audit-full audit-full: @echo "Running comprehensive audit..." @$(MAKE) audit-quality @$(MAKE) audit-security @$(MAKE) audit-deps @$(MAKE) test-audit # Development helpers # Install dependencies .PHONY: deps deps: @echo "Installing dependencies..." @go mod tidy # Install development dependencies .PHONY: dev-deps dev-deps: @echo "Installing development dependencies..." @go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest @go install github.com/securego/gosec/v2/cmd/gosec@latest @go install golang.org/x/vuln/cmd/govulncheck@latest @go install github.com/go-delve/delve/cmd/dlv@latest @go mod tidy @echo "Development dependencies installed!" # Development setup .PHONY: dev-setup dev-setup: @echo "Setting up development environment..." @$(MAKE) deps @$(MAKE) dev-deps @./scripts/setup-dev.sh @echo "Development environment setup complete!" # Development workflow (fmt + vet + lint + basic test) .PHONY: dev-workflow dev-workflow: @echo "Running development workflow..." @$(MAKE) fmt @$(MAKE) vet @$(MAKE) lint @$(MAKE) test-basic # Development workflow with coverage .PHONY: dev-workflow-full dev-workflow-full: @echo "Running development workflow with coverage..." @$(MAKE) fmt @$(MAKE) vet @$(MAKE) lint @$(MAKE) test-coverage # Debug run .PHONY: debug debug: @echo "Running application in debug mode..." @which dlv > /dev/null || (echo "delve not found, install with: go install github.com/go-delve/delve/cmd/dlv@latest && make dev-deps") @dlv exec -- $(BINARY_PATH) # Watch and run tests (requires 'entr' command) .PHONY: watch-tests watch-tests: @echo "Watching for file changes and running tests..." @echo "Note: Requires 'entr' to be installed. Install with: apt-get install entr (or brew install entr)" @find . -name "*.go" -not -path "./vendor/*" -not -path "./bin/*" | entr -c $(MAKE) test-basic # Watch and run dev workflow (requires 'entr' command) .PHONY: watch-dev watch-dev: @echo "Watching for file changes and running dev workflow..." @echo "Note: Requires 'entr' to be installed. Install with: apt-get install entr (or brew install entr)" @find . -name "*.go" -not -path "./vendor/*" -not -path "./bin/*" | entr -c $(MAKE) dev-workflow # Documentation generation .PHONY: docs docs: @echo "Generating code documentation..." @mkdir -p docs/gen @go doc -all ./... > docs/gen/code-documentation.txt @echo "Code documentation generated in docs/gen/code-documentation.txt" # Clean build artifacts .PHONY: clean clean: @echo "Cleaning..." @rm -rf bin/ @rm -f coverage.out coverage.html @rm -rf reports/ @echo "Clean complete!" # Update dependencies .PHONY: update update: @echo "Updating dependencies..." @go get -u ./... @go mod tidy @echo "Dependencies updated!" # Help .PHONY: help help: @echo "Available targets:" @echo "" @echo "Build & Run:" @echo " all - Build the application (default)" @echo " build - Build the application" @echo " build-race - Build with race detection" @echo " run - Build and run the application" @echo " run-dev - Run without building" @echo "" @echo "Testing (Multi-Level):" @echo " test-basic - Run basic tests (fast)" @echo " test-unit - Run unit tests" @echo " test-integration - Run integration tests" @echo " test-e2e - Run end-to-end tests" @echo " test-property - Run property tests" @echo " test-fuzzing - Run fuzzing tests" @echo " test-stress - Run stress tests" @echo " test-security - Run security tests" @echo " test-bench - Run benchmark tests" @echo " test-comprehensive - Run comprehensive tests (all test types)" @echo " test-audit - Run full audit tests (comprehensive + security + stress)" @echo " test-coverage - Run tests with coverage report" @echo " test-coverage-pkg - Run tests with coverage for specific package (use with PKG=package/path)" @echo "" @echo "Quality & Auditing:" @echo " fmt - Format code" @echo " vet - Vet code" @echo " lint - Lint code (requires golangci-lint)" @echo " audit-security - Run security audit" @echo " audit-deps - Run dependency audit" @echo " audit-quality - Run code quality audit" @echo " audit-full - Run comprehensive audit (all checks)" @echo "" @echo "Development:" @echo " dev-setup - Setup development environment" @echo " dev-deps - Install development dependencies" @echo " dev-workflow - Run development workflow (fmt + vet + lint + basic test)" @echo " dev-workflow-full - Run development workflow with coverage" @echo " debug - Run application in debug mode" @echo " watch-tests - Watch for changes and run basic tests (requires entr)" @echo " watch-dev - Watch for changes and run dev workflow (requires entr)" @echo "" @echo "Maintenance:" @echo " clean - Clean build artifacts" @echo " deps - Install dependencies" @echo " update - Update dependencies" @echo " docs - Generate code documentation" @echo " help - Show this help" @echo "" @echo "Examples:" @echo " make test-coverage PKG=./pkg/my-package/ # Coverage for specific package" @echo " make watch-dev # Watch for changes and run dev workflow" # Create setup script cat > "$PROJECT_NAME/scripts/setup-dev.sh" << 'SETUP' #!/usr/bin/env bash set -euo pipefail # Development environment setup script echo "Setting up development environment for $(basename $(pwd))..." # Create directories if they don't exist mkdir -p logs mkdir -p reports mkdir -p reports/coverage mkdir -p reports/test-results mkdir -p reports/augments mkdir -p storage mkdir -p storage/keystore mkdir -p storage/cache mkdir -p .gocache # Check if Go is installed if ! command -v go &> /dev/null; then echo "Error: Go is not installed" >&2 exit 1 fi # Check if required tools are installed echo "Checking for required tools..." # Install golangci-lint if not present if ! command -v golangci-lint &> /dev/null; then echo "Installing golangci-lint..." go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest fi # Install gosec if not present if ! command -v gosec &> /dev/null; then echo "Installing gosec..." go install github.com/securego/gosec/v2/cmd/gosec@latest fi # Install govulncheck if not present if ! command -v govulncheck &> /dev/null; then echo "Installing govulncheck..." go install golang.org/x/vuln/cmd/govulncheck@latest fi # Install delve if not present if ! command -v dlv &> /dev/null; then echo "Installing delve..." go install github.com/go-delve/delve/cmd/dlv@latest fi echo "Development environment setup complete!" SETUP # Make setup script executable chmod +x "$PROJECT_NAME/scripts/setup-dev.sh" echo "Project template created successfully in $PROJECT_NAME/" echo "To get started:" echo " cd $PROJECT_NAME" echo " make dev-setup" echo " make test" echo " make build" echo " make run" EOF chmod +x "$PROJECT_NAME/scripts/setup-dev.sh" echo "Project template created successfully in $PROJECT_NAME/" echo "To get started:" echo " cd $PROJECT_NAME" echo " make dev-setup" echo " make test" echo " make build" echo " make run"