starter-kit/Makefile
2025-05-21 12:39:40 +07:00

172 lines
5.6 KiB
Makefile

# ULFlow Golang Starter Kit Makefile
# Provides common commands for development, testing, and deployment
.PHONY: help init dev test lint build clean docker-build docker-run docker-clean docker-prune docker-compose-up docker-compose-down docker-compose-prod-up docker-compose-prod-down ci setup-git all
# Default target executed when no arguments are given to make.
default: help
# Show help message for all Makefile commands
help:
@echo "ULFlow Golang Starter Kit"
@echo ""
@echo "Usage:"
@echo " make <target>"
@echo ""
@echo "Targets:"
@echo " init - Initialize project dependencies and tools"
@echo " dev - Start development server with hot reload (Air Tomb)"
@echo " test - Run all tests"
@echo " lint - Run linters and code quality tools"
@echo " build - Build the application binary"
@echo " clean - Clean temporary files, build artifacts, and cache"
@echo " docker-build - Build Docker image"
@echo " docker-run - Run application in Docker container"
@echo " docker-clean - Remove project Docker containers"
@echo " docker-prune - Remove all unused Docker resources"
@echo " docker-compose-up - Start all services with Docker Compose for local development"
@echo " docker-compose-down - Stop all services started with Docker Compose"
@echo " docker-compose-prod-up - Start all services with Docker Compose for production"
@echo " docker-compose-prod-down - Stop all production services"
@echo " ci - Run CI workflow locally"
@echo " setup-git - Configure Git with commit message template and hooks"
@echo " all - Run lint, test, and build"
# Initialize project dependencies and tools
init:
@echo "Installing project dependencies and tools..."
go mod tidy
go install github.com/cosmtrek/air@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
@echo "Creating necessary directories..."
mkdir -p tmp
mkdir -p logs
@echo "Initialization complete!"
# Start development server with hot reload
dev:
@echo "Starting development server with Air Tomb..."
air
# Run all tests
test:
@echo "Running tests..."
go test -v -cover ./...
# Run linters and code quality tools
lint:
@echo "Running linters..."
golangci-lint run ./...
# Build the application binary
build:
@echo "Building application..."
go build -o bin/api cmd/api/main.go
# Clean temporary files, build artifacts, and cache
clean:
@echo "Cleaning project..."
rm -rf tmp/
rm -rf bin/
rm -rf logs/*.log
go clean -cache -testcache -modcache
@echo "Project cleaned!"
# Build Docker image
docker-build:
@echo "Building Docker image..."
docker build -t ulflow-starter-kit:latest .
# Run application in Docker container
docker-run:
@echo "Running application in Docker container..."
docker run -p 8080:8080 --env-file .env ulflow-starter-kit:latest
# Run Docker Compose for local development
docker-compose-up:
@echo "Starting all services with Docker Compose for local development..."
docker-compose up -d
@echo "Services started! API is available at http://localhost:8080"
# Stop Docker Compose services for local development
docker-compose-down:
@echo "Stopping all development services..."
docker-compose down
# Run Docker Compose for production
docker-compose-prod-up:
@echo "Starting all services with Docker Compose for production..."
docker-compose -f docker-compose.prod.yml up -d
@echo "Production services started! API is available at http://localhost:8080"
# Stop Docker Compose services for production
docker-compose-prod-down:
@echo "Stopping all production services..."
docker-compose -f docker-compose.prod.yml down
# Setup Git configuration
setup-git:
@echo "Setting up Git configuration..."
git config --local commit.template .gitea/commit-template.txt
cp .gitea/hooks/pre-commit .git/hooks/
cp .gitea/hooks/prepare-commit-msg .git/hooks/
chmod +x .git/hooks/pre-commit
chmod +x .git/hooks/prepare-commit-msg
git config --local core.hooksPath .git/hooks
@echo "Git setup complete!"
# Create git message template
setup-git-message:
@echo "Creating Git commit message template..."
mkdir -p .gitea
echo "# <type>: <subject>\n\n# <body>\n\n# <footer>\n\n# Types:\n# feat (new feature)\n# fix (bug fix)\n# docs (documentation changes)\n# style (formatting, no code change)\n# refactor (refactoring code)\n# test (adding tests, refactoring tests)\n# chore (updating tasks etc; no production code change)" > .gitea/commit-template.txt
git config --local commit.template .gitea/commit-template.txt
@echo "Git commit message template created!"
# Clean Docker containers related to this project
docker-clean:
@echo "Cleaning Docker containers..."
docker ps -a | grep ulflow-starter-kit | awk '{print $$1}' | xargs -r docker rm -f
@echo "Docker containers cleaned!"
# Prune all unused Docker resources
docker-prune:
@echo "Pruning unused Docker resources..."
docker system prune -af --volumes
@echo "Docker resources pruned!"
# Run local CI simulation
ci:
@echo "Running CI workflow locally..."
make lint
make test
make build
make docker-build
@echo "CI simulation completed!"
@echo "Cleaning up after CI..."
make docker-clean
# Run everything (lint, test, build)
all: lint test build
# Create a new migration
migrate-create:
@read -p "Enter migration name: " name; \
migrate create -ext sql -dir migrations -seq $$name
# Run migrations up
migrate-up:
migrate -path migrations -database "$(DATABASE_URL)" up
# Run migrations down
migrate-down:
migrate -path migrations -database "$(DATABASE_URL)" down
# Run application (default: without hot reload)
run:
go run ./cmd/app/main.go
# Run application with hot reload
dev:
air -c .air.toml