Makefile 1.75 KB
ifneq (,$(wildcard .env))
include .env
export
endif

BINDIR := bin
APPS := $(notdir $(wildcard cmd/*))
BUILD_TARGETS := $(filter-out build clean, $(MAKECMDGOALS))

.PHONY: build clean default

default: build

build:
	@mkdir -p $(BINDIR)
	@if [ -z "$(strip $(BUILD_TARGETS))" ]; then \
	  echo "No specific app provided. Building all apps..."; \
	  for app in $(APPS); do \
	    echo "Building $$app..."; \
	    go build -o $(BINDIR)/$$app ./cmd/$$app; \
	  done; \
	else \
	  for arg in $(BUILD_TARGETS); do \
	    if echo $$arg | grep -q -- '-$$'; then \
	      prefix=$${arg%-}; \
	      echo "Building all apps starting with '$$prefix'..."; \
	      for app in $(APPS); do \
	        case "$$app" in \
	          $$prefix*) \
	            echo "Building $$app..."; \
	            go build -o $(BINDIR)/$$app ./cmd/$$app;; \
	        esac; \
	      done; \
	    else \
	      echo "Building $$arg..."; \
	      go build -o $(BINDIR)/$$arg ./cmd/$$arg; \
	    fi; \
	  done; \
	fi

clean:
	@echo "Cleaning up binaries..."
	@rm -rf $(BINDIR)

codecheck:
	@echo "checking code. Running vet and fmt"
	@go vet ./...
	@go fmt ./...

test:
	@echo "Running test"
	@go test ./...

migrate-postgres:
	@if [ -z "$(DATABASE_URL)" ]; then \
	  echo "Error: DATABASE_URL is not set. Please set it in .env or your environment."; \
	  exit 1; \
	fi; \
	echo "Running database migrations with goose using driver $$driver..."; \
	goose -dir migration/postgres postgres "$(DATABASE_URL)" up

migrate-postgres-status:
	@if [ -z "$(DATABASE_URL)" ]; then \
	  echo "Error: DATABASE_URL is not set. Please set it in .env or your environment."; \
	  exit 1; \
	fi; \
	echo "Running migration status with goose using driver $$driver..."; \
	goose -dir migration/postgres postgres "$(DATABASE_URL)" status

%:
	@: