Skip to content

Commit

Permalink
Fix: makefiles and standardize the commands across all of them (#566)
Browse files Browse the repository at this point in the history
* Fix and standardize Makefiles across all components

* Restore .env.example files from main branch

* Fix: Replace deprecated tenv linter with usetesting

* Fix: Replace process substitution with temp files in Makefile for better shell compatibility

* Add TODO comments for test coverage in Makefile

* Fix: Add platform specification for Trillian images to support ARM64 hosts

* Fix: Update Makefile to handle components without docker-compose.yml

* Fix: Set ENV_NAME=development in Makefile to avoid Docker Compose warnings

* Fix: Set ENV_NAME=development in all .env.example files

* Fix: Add ENV_NAME=development to infra and auth .env.example files

* Fix: Mark networks as external in Docker Compose files to avoid warnings

* Remove external flag from network configurations in audit docker-compose

* Fix audit docker-compose.yml network configuration

* refactor: update Makefile to remove clean-docker and add start, stop, restart commands 🔨

* refactor: centralize Makefile colors and utilities in pkg/shell 🔨

* patch: removes auth_network from transaction and onboarding's compose files
  • Loading branch information
fredcamaral authored Mar 5, 2025
1 parent 8f40928 commit 4d02cbe
Show file tree
Hide file tree
Showing 18 changed files with 680 additions and 445 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ linters:
- reassign
- revive # Metalinter; drop-in replacement for golint.
- stylecheck # Replacement for golint
- tenv # Detects using os.Setenv instead of t.Setenv.
- usetesting # Detects using os.Setenv instead of t.Setenv (replacement for tenv).
- thelper # Detects test helpers without t.Helper().
- tparallel # Detects inappropriate usage of t.Parallel().
- unconvert # Detects unnecessary type conversions.
Expand Down
462 changes: 359 additions & 103 deletions Makefile

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions components/infra/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# INFRA
ENV_NAME=development

# PG BOUNCER
DB_PGBOUNCER_PORT=5706
Expand Down
79 changes: 42 additions & 37 deletions components/infra/Makefile
Original file line number Diff line number Diff line change
@@ -1,80 +1,85 @@
service_name := infra-service
bin_dir := ./.bin
artifacts_dir := ./artifacts
SERVICE_NAME := infra-service
BIN_DIR := ./.bin
ARTIFACTS_DIR := ./artifacts

$(shell mkdir -p $(artifacts_dir))
$(shell mkdir -p $(ARTIFACTS_DIR))

DOCKER_VERSION := $(shell docker version --format '{{.Server.Version}}')
DOCKER_MIN_VERSION := 20.10.13
# Define the root directory of the project (if not already defined)
MIDAZ_ROOT ?= $(shell cd ../.. && pwd)

DOCKER_CMD := $(shell \
if [ "$(shell printf '%s\n' "$(DOCKER_MIN_VERSION)" "$(DOCKER_VERSION)" | sort -V | head -n1)" = "$(DOCKER_MIN_VERSION)" ]; then \
echo "docker compose"; \
else \
echo "docker-compose"; \
fi \
)
# Include shared color definitions and utility functions
include $(MIDAZ_ROOT)/pkg/shell/makefile_colors.mk
include $(MIDAZ_ROOT)/pkg/shell/makefile_utils.mk

# Display available commands
.PHONY: info
info:
@echo " "
@echo " "
@echo "To run a specific command inside the infra container using make, you can execute: "
@echo " "
@echo "make infra COMMAND=\"any\" "
@echo " "
@echo "This command will run the specified command inside the infra container. Replace \"any\" with the desired command you want to execute. "
@echo " "
@echo " "
@echo "## Docker commands:"
@echo " "
@echo " COMMAND=\"build\" Builds all Docker images defined in docker-compose.yml."
@echo " COMMAND=\"up\" Starts and runs all services defined in docker-compose.yml."
@echo " COMMAND=\"start\" Starts existing containers defined in docker-compose.yml without creating them."
@echo " COMMAND=\"stop\" Stops running containers defined in docker-compose.yml without removing them."
@echo " COMMAND=\"down\" Stops and removes containers, networks, and volumes defined in docker-compose.yml."
@echo " COMMAND=\"destroy\" Stops and removes containers, networks, and volumes (including named volumes) defined in docker-compose.yml."
@echo " COMMAND=\"restart\" Stops and removes containers, networks, and volumes, then starts all services in detached mode."
@echo " COMMAND=\"logs\" Shows the last 100 lines of logs and follows live log output for services defined in docker-compose.yml."
@echo " COMMAND=\"ps\" Lists the status of containers defined in docker-compose.yml."
@echo " "
@echo " "
@echo ""
@echo "$(BOLD)Infra Service Commands$(NC)"
@echo ""
@echo "To run a specific command inside the infra container using make, you can execute:"
@echo ""
@echo "$(CYAN)make infra COMMAND=\"any\"$(NC)"
@echo ""
@echo "This command will run the specified command inside the infra container."
@echo "Replace \"any\" with the desired command you want to execute."
@echo ""
@echo "$(BOLD)## Docker commands:$(NC)"
@echo ""
@echo " $(CYAN)COMMAND=\"build\"$(NC) Build Docker images"
@echo " $(CYAN)COMMAND=\"up\"$(NC) Start all services in detached mode"
@echo " $(CYAN)COMMAND=\"start\"$(NC) Start existing containers"
@echo " $(CYAN)COMMAND=\"stop\"$(NC) Stop running containers"
@echo " $(CYAN)COMMAND=\"down\"$(NC) Stop and remove containers, networks, and volumes"
@echo " $(CYAN)COMMAND=\"destroy\"$(NC) Stop and remove containers, networks, and all volumes"
@echo " $(CYAN)COMMAND=\"restart\"$(NC) Restart all services"
@echo " $(CYAN)COMMAND=\"logs\"$(NC) Show and follow logs for all services"
@echo " $(CYAN)COMMAND=\"ps\"$(NC) List container status"
@echo ""

# Docker Compose Commands
.PHONY: build
build:
@echo "$(BLUE)Building Docker images...$(NC)"
@$(DOCKER_CMD) -f docker-compose.yml build $(c)

.PHONY: up
up:
@echo "$(BLUE)Starting all services in detached mode...$(NC)"
@$(DOCKER_CMD) -f docker-compose.yml up $(c) -d

.PHONY: start
start:
@docker compose -f docker-compose.yml start $(c)
@echo "$(BLUE)Starting existing containers...$(NC)"
@$(DOCKER_CMD) -f docker-compose.yml start $(c)

.PHONY: down
down:
@echo "$(BLUE)Stopping and removing containers, networks, and volumes...$(NC)"
@$(DOCKER_CMD) -f docker-compose.yml down $(c)

.PHONY: destroy
destroy:
@echo "$(BLUE)Stopping and removing containers, networks, and all volumes...$(NC)"
@$(DOCKER_CMD) -f docker-compose.yml down -v $(c)

.PHONY: stop
stop:
@echo "$(BLUE)Stopping running containers...$(NC)"
@$(DOCKER_CMD) -f docker-compose.yml stop $(c)

.PHONY: restart
restart:
make stop && \
@echo "$(BLUE)Restarting all services...$(NC)"
@make stop && \
make up

.PHONY: logs
logs:
@echo "$(BLUE)Showing logs for all services...$(NC)"
@$(DOCKER_CMD) -f docker-compose.yml logs --tail=100 -f $(c)

.PHONY: ps
ps:
@echo "$(BLUE)Listing container status...$(NC)"
@$(DOCKER_CMD) -f docker-compose.yml ps
3 changes: 3 additions & 0 deletions components/mdz/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# DEFAULT local
ENV_NAME=development

CLIENT_ID=9670e0ca55a29a466d31
CLIENT_SECRET=dd03f916cacf4a98c6a413d9c38ba102dce436a9
URL_API_AUTH=http://127.0.0.1:8080
Expand Down
2 changes: 1 addition & 1 deletion components/mdz/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ linters:
- reassign
- revive # Metalinter; drop-in replacement for golint.
- stylecheck # Replacement for golint
- tenv # Detects using os.Setenv instead of t.Setenv.
- usetesting # Detects using os.Setenv instead of t.Setenv (replacement for tenv).
- thelper # Detects test helpers without t.Helper().
- tparallel # Detects inappropriate usage of t.Parallel().
- unconvert # Detects unnecessary type conversions.
Expand Down
98 changes: 82 additions & 16 deletions components/mdz/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
GO := $(shell which go)
SERVICE_NAME := mdz-service
BIN_DIR := ./bin
ARTIFACTS_DIR := ./artifacts
NAME := mdz

$(shell mkdir -p $(ARTIFACTS_DIR))

# Define the root directory of the project (if not already defined)
MIDAZ_ROOT ?= $(shell cd ../.. && pwd)

# Include shared color definitions and utility functions
include $(MIDAZ_ROOT)/pkg/shell/makefile_colors.mk
include $(MIDAZ_ROOT)/pkg/shell/makefile_utils.mk

GO := $(shell which go)

ifeq (, $(GO))
$(error "No go binary found in your system, please install go version go1.23.2 before continuing")
endif
Expand All @@ -9,58 +22,111 @@ ifneq (,$(wildcard .env))
include .env
endif


LDFLAGS=-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.ClientID=$(CLIENT_ID)'\
-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.ClientSecret=$(CLIENT_SECRET)' \
-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.URLAPIAuth=$(URL_API_AUTH)' \
-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.URLAPILedger=$(URL_API_LEDGER)' \
-X 'github.com/LerianStudio/midaz/components/mdz/pkg/environment.Version=$(VERSION)'

# Display available commands
.PHONY: info
info:
@echo ""
@echo "$(BOLD)MDZ Service Commands$(NC)"
@echo ""
@echo "To run a specific command inside the mdz container using make, you can execute:"
@echo ""
@echo "$(CYAN)make mdz COMMAND=\"any\"$(NC)"
@echo ""
@echo "This command will run the specified command inside the mdz container."
@echo "Replace \"any\" with the desired command you want to execute."
@echo ""
@echo "$(BOLD)## Linting and Security commands:$(NC)"
@echo ""
@echo " $(CYAN)COMMAND=\"lint\"$(NC) Run code linting"
@echo " $(CYAN)COMMAND=\"govulncheck\"$(NC) Check for vulnerabilities"
@echo " $(CYAN)COMMAND=\"gosec\"$(NC) Run security analysis"
@echo " $(CYAN)COMMAND=\"perfsprint\"$(NC) Run performance checks"
@echo ""
@echo "$(BOLD)## Testing commands:$(NC)"
@echo ""
@echo " $(CYAN)COMMAND=\"test\"$(NC) Run unit tests"
@echo " $(CYAN)COMMAND=\"test_integration\"$(NC) Run integration tests"
@echo ""
@echo "$(BOLD)## Build commands:$(NC)"
@echo ""
@echo " $(CYAN)COMMAND=\"build\"$(NC) Build mdz binary"
@echo " $(CYAN)COMMAND=\"install-local\"$(NC) Install mdz to /usr/local/bin"
@echo ""

# Linting and Security Commands
.PHONY: get-lint-deps
get-lint-deps:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
@echo "$(BLUE)Installing golangci-lint...$(NC)"
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

.PHONY: lint
lint: get-lint-deps
golangci-lint run --fix ./... --verbose
$(call title1,"Running code linting")
@golangci-lint run --fix ./... --verbose
@echo "$(GREEN)$(BOLD)[ok]$(NC) Linting completed successfully$(GREEN) ✔️$(NC)"

.PHONY: get-govulncheck-deps
get-govulncheck-deps:
go install golang.org/x/vuln/cmd/govulncheck@latest
@echo "$(BLUE)Installing govulncheck...$(NC)"
@go install golang.org/x/vuln/cmd/govulncheck@latest

.PHONY: govulncheck
govulncheck: get-govulncheck-deps
govulncheck ./...
$(call title1,"Checking for vulnerabilities")
@govulncheck ./...
@echo "$(GREEN)$(BOLD)[ok]$(NC) Vulnerability check completed$(GREEN) ✔️$(NC)"

.PHONY: get-gosec-deps
get-gosec-deps:
go install github.com/securego/gosec/v2/cmd/gosec@latest
@echo "$(BLUE)Installing gosec...$(NC)"
@go install github.com/securego/gosec/v2/cmd/gosec@latest

.PHONY: gosec
gosec: get-gosec-deps
gosec ./...
$(call title1,"Running security analysis")
@gosec ./...
@echo "$(GREEN)$(BOLD)[ok]$(NC) Security analysis completed$(GREEN) ✔️$(NC)"

.PHONY: get-perfsprint-deps
get-perfsprint-deps:
go get github.com/catenacyber/perfsprint@latest
@echo "$(BLUE)Installing perfsprint...$(NC)"
@go get github.com/catenacyber/perfsprint@latest

.PHONY : perfsprint
.PHONY: perfsprint
perfsprint: get-perfsprint-deps
perfsprint ./...
$(call title1,"Running performance checks")
@perfsprint ./...
@echo "$(GREEN)$(BOLD)[ok]$(NC) Performance checks completed$(GREEN) ✔️$(NC)"

# Testing Commands
.PHONY: test
test:
go test ./...
@echo "$(BLUE)Running unit tests...$(NC)"
@go test ./...
@echo "$(GREEN)$(BOLD)[ok]$(NC) Tests completed successfully$(GREEN) ✔️$(NC)"

.PHONY: test_integration
test_integration:
go test -v -tags=integration ./test/integration/...
$(call title1,"Running integration tests")
@go test -v -tags=integration ./test/integration/...
@echo "$(GREEN)$(BOLD)[ok]$(NC) Integration tests completed successfully$(GREEN) ✔️$(NC)"

# Build Commands
.PHONY: build
build:
go version
go build -ldflags "$(LDFLAGS)" -o ./bin/$(NAME) ./main.go
$(call title1,"Building mdz binary")
@go version
@go build -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/$(NAME) ./main.go
@echo "$(GREEN)$(BOLD)[ok]$(NC) Binary built successfully at $(BIN_DIR)/$(NAME)$(GREEN) ✔️$(NC)"

.PHONY: install-local
install-local: build
sudo cp -r bin/mdz /usr/local/bin
@echo "$(BLUE)Installing mdz binary to /usr/local/bin...$(NC)"
@sudo cp -r $(BIN_DIR)/mdz /usr/local/bin
@echo "$(GREEN)$(BOLD)[ok]$(NC) Binary installed successfully to /usr/local/bin/mdz$(GREEN) ✔️$(NC)"
1 change: 1 addition & 0 deletions components/onboarding/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# ONBOARDING
ENV_NAME=development

# DEFAULT local
# ENV_NAME=production
Expand Down
Loading

0 comments on commit 4d02cbe

Please sign in to comment.