Skip to content

Instantly share code, notes, and snippets.

@AlcidesRC
Created October 28, 2024 08:12
Show Gist options
  • Save AlcidesRC/7ff0450e22f2ff90e4954089cc3b48a0 to your computer and use it in GitHub Desktop.
Save AlcidesRC/7ff0450e22f2ff90e4954089cc3b48a0 to your computer and use it in GitHub Desktop.
Plantilla de fichero Makefile
.DEFAULT_GOAL := help
###
# CONSTANTS
###
ifneq (,$(findstring xterm,$(TERM)))
BLACK := $(shell tput -Txterm setaf 0)
RED := $(shell tput -Txterm setaf 1)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
BLUE := $(shell tput -Txterm setaf 4)
MAGENTA := $(shell tput -Txterm setaf 5)
CYAN := $(shell tput -Txterm setaf 6)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
else
BLACK := ""
RED := ""
GREEN := ""
YELLOW := ""
BLUE := ""
MAGENTA := ""
CYAN := ""
WHITE := ""
RESET := ""
endif
#---
SERVICE_APP = app
#---
HOST_USER_ID := $(shell id --user)
HOST_USER_NAME := $(shell id --user --name)
HOST_GROUP_ID := $(shell id --group)
HOST_GROUP_NAME := $(shell id --group --name)
#---
DOCKER_COMPOSE = docker compose --file docker-compose.yml
DOCKER_BUILD_ARGUMENTS = --build-arg="HOST_USER_ID=$(HOST_USER_ID)" --build-arg="HOST_USER_NAME=$(HOST_USER_NAME)" --build-arg="HOST_GROUP_ID=$(HOST_GROUP_ID)" --build-arg="HOST_GROUP_NAME=$(HOST_GROUP_NAME)"
###
# FUNCTIONS
###
define pad
$(shell printf "%-$(1)s" " ")
endef
###
# HELP
###
.PHONY: help
help:
@clear
@echo "${BLACK}"
@echo "╔════════════════════════════════════════════════════════════════════════════════════════════════════════╗"
@echo "║ $(call pad,96) ║"
@echo "║ $(call pad,32) ${YELLOW}.:${RESET} AVAILABLE COMMANDS ${YELLOW}:.${BLACK} $(call pad,32) ║"
@echo "║ $(call pad,96) ║"
@echo "╚════════════════════════════════════════════════════════════════════════════════════════════════════════╝"
@echo "${BLACK}·${RESET} ${MAGENTA}SERVICE(s)${BLACK} ... ${CYAN}$(shell docker ps --format "{{.Names}}")${BLACK}"
@echo "${BLACK}·${RESET} ${MAGENTA}USER${BLACK} ......... ${WHITE}(${CYAN}$(HOST_USER_ID)${WHITE})${BLACK} ${CYAN}$(HOST_USER_NAME)${BLACK}"
@echo "${BLACK}·${RESET} ${MAGENTA}GROUP${BLACK} ........ ${WHITE}(${CYAN}$(HOST_GROUP_ID)${WHITE})${BLACK} ${CYAN}$(HOST_GROUP_NAME)${BLACK}"
@echo "${RESET}"
@grep -E '^[a-zA-Z_0-9%-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "${BLACK}·${RESET} ${YELLOW}%-35s${RESET} %s\n", $$1, $$2}'
@echo ""
###
# DOCKER RELATED
###
.PHONY: build
build: ## Docker: builds the service
@$(DOCKER_COMPOSE) build $(DOCKER_BUILD_ARGUMENTS)
.PHONY: up
up: ## Docker: starts the service
@$(DOCKER_COMPOSE) up --remove-orphans --detach
.PHONY: restart
restart: ## Docker: restarts the service
@$(DOCKER_COMPOSE) restart
.PHONY: down
down: ## Docker: stops the service
@$(DOCKER_COMPOSE) down --remove-orphans
.PHONY: logs
logs: ## Docker: exposes the service logs
@$(DOCKER_COMPOSE) logs -f $(SERVICE_APP)
.PHONY: shell
shell: ## Docker: establish a shell session into main container
@$(DOCKER_COMPOSE) run --rm $(SERVICE_APP) sh
.PHONY: health
health: ## Docker: inspect the health for specific service
@docker inspect --format "{{json .State.Health}}" $(SERVICE_APP) | jq
###
# APPLICATION
###
.PHONY: init
init: build up ## Application: initializes the application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment