2021-02-09 17:05:00 +01:00
|
|
|
##
|
2021-06-02 00:54:33 +02:00
|
|
|
# ENV
|
|
|
|
|
2022-06-28 13:30:38 +02:00
|
|
|
# target .env: Update file .env
|
|
|
|
## it updates file .env when file .env.dist is newer
|
2021-02-09 17:05:00 +01:00
|
|
|
.env: .env.dist
|
2021-06-09 00:44:36 +02:00
|
|
|
$(call .env,,,$(wildcard $(CONFIG)/$(ENV)/$(APP)/.env .env.$(ENV)))
|
2021-02-09 17:05:00 +01:00
|
|
|
|
2022-06-28 13:30:38 +02:00
|
|
|
# target .env-clean: Remove file .env
|
|
|
|
## it removes file .env
|
2021-05-30 23:45:30 +02:00
|
|
|
.PHONY: .env-clean
|
|
|
|
.env-clean:
|
2022-12-05 20:29:00 +01:00
|
|
|
$(RUN) rm -$(if $(FORCE),f,i) .env || true
|
2021-05-30 23:45:30 +02:00
|
|
|
|
2022-06-28 13:30:38 +02:00
|
|
|
# target .env-update: Update file ENV_FILE
|
|
|
|
## it updates file ENV_FILE with missing values from file ENV_DIST
|
|
|
|
## it can override ENV_DIST with values from file ENV_OVER
|
|
|
|
.PHONY: .env-update
|
|
|
|
.env-update:
|
|
|
|
$(call INFO,.env-update,$(ENV_FILE)$(comma) $(ENV_DIST)$(comma) $(ENV_OVER))
|
|
|
|
$(foreach env_file,$(ENV_FILE),$(call .env,$(env_file),$(or $(ENV_DIST),$(env_file).dist),$(ENV_OVER)))
|
|
|
|
|
2021-02-09 17:05:00 +01:00
|
|
|
# include .env file
|
|
|
|
-include .env
|
|
|
|
|
2021-05-18 09:37:34 +02:00
|
|
|
ifneq (,$(filter true,$(ENV_RESET)))
|
|
|
|
env_reset := -i
|
|
|
|
endif
|
|
|
|
|
2021-07-12 21:04:06 +02:00
|
|
|
docker.env.args = $(foreach var,$(ENV_VARS),$(if $($(var)),-e $(var)='$($(var))'))
|
|
|
|
docker.env.dist ?= $(shell printenv |awk -F '=' 'NR == FNR { if($$1 !~ /^(\#|$$)/) { A[$$1]; next } } ($$1 in A) {print "-e "$$0}' .env.dist - 2>/dev/null)
|
|
|
|
docker.env.file ?= $(patsubst %,--env-file %,$(wildcard $(ENV_FILE)))
|
|
|
|
docker_env_args = $(docker.env.args) $(docker.env.dist) $(docker.env.file)
|
|
|
|
env.args = $(foreach var,$(ENV_VARS),$(if $($(var)),$(var)='$($(var))'))
|
|
|
|
env.dist ?= $(shell printenv |awk -F '=' 'NR == FNR { if($$1 !~ /^(\#|$$)/) { A[$$1]; next } } ($$1 in A)' .env.dist - 2>/dev/null)
|
|
|
|
env.file ?= $(shell cat $(or $(ENV_FILE),/dev/null) 2>/dev/null |sed '/^[ \t]*$$/d;/^[ \t]*\#/d;s/='\''/=/;s/'\''$$//;s/='\"'/=/;s/'\"'$$//;' |awk -F '=' '{print $$1"='\''"$$2"'\''"}')
|
|
|
|
env_args = $(env.args) $(env.dist) $(env.file)
|
2021-02-09 17:05:00 +01:00
|
|
|
|
|
|
|
SHELL:=/bin/bash
|
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# function .env: Call .env_update function
|
|
|
|
## it sets .env, .env.dist and .env.ENV files paths
|
|
|
|
## it calls .env_update function if .env.dist file exists
|
2021-02-09 17:05:00 +01:00
|
|
|
# 1st arg: path to .env file to update, default to .env
|
|
|
|
# 2nd arg: path to .env.dist file, default to .env.dist
|
|
|
|
# 3rd arg: path to .env override files, default to .env.$(ENV)
|
|
|
|
define .env
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,.env,$(1)$(comma) $(2)$(comma) $(3))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(eval env_file:=$(or $(1),.env))
|
|
|
|
$(eval env_dist:=$(or $(2),$(env_file).dist))
|
|
|
|
$(eval env_over:=$(or $(wildcard $(3)),$(wildcard $(env_file).$(ENV))))
|
|
|
|
$(if $(wildcard $(env_dist)), $(call .env_update))
|
|
|
|
endef
|
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# function .env_update: Update .env file with values from .env.dist
|
2021-02-09 17:05:00 +01:00
|
|
|
## this function adds variables from the .env.dist to the .env file
|
|
|
|
## and does substitution to replace variables with their value when
|
|
|
|
## adding it to the .env. It reads variables first from environment,
|
|
|
|
## make command line, .env override files and finish with .env.dist
|
|
|
|
## to do the substitution. It does not write to .env file variables
|
|
|
|
## that already exist in .env file or comes from system environment.
|
|
|
|
# create the .env file
|
|
|
|
# read environment variables
|
|
|
|
# keep variables from .env.dist that does not exist in environment
|
2021-06-02 00:54:33 +02:00
|
|
|
# add variables definition from .env override files at the beginning
|
|
|
|
# add variables definition from make command line at the beginning
|
2021-02-09 17:05:00 +01:00
|
|
|
# remove duplicate variables
|
|
|
|
# keep variables that exists in .env.dist
|
|
|
|
# keep variables that does not exist in .env
|
|
|
|
# read variables definition in a subshell with multiline support
|
2021-06-02 00:54:33 +02:00
|
|
|
# create a new (empty if ENV_RESET is true) environment with env.args
|
2021-02-09 17:05:00 +01:00
|
|
|
# read environment variables and keep only those existing in .env.dist
|
|
|
|
# add .env overrides variables definition
|
|
|
|
# add .env.dist variables definition
|
|
|
|
# remove empty lines or comments
|
|
|
|
# remove duplicate variables
|
2021-05-30 23:45:30 +02:00
|
|
|
# replace variables in stdin with their value from the new environment
|
2021-02-09 17:05:00 +01:00
|
|
|
# remove residual empty lines or comments
|
|
|
|
# sort alphabetically
|
|
|
|
# add variables definition to the .env file
|
|
|
|
define .env_update
|
2022-06-28 13:30:38 +02:00
|
|
|
$(call INFO,.env_update,$(env_file)$(comma) $(env_dist)$(comma) $(env_over))
|
2022-06-04 02:24:35 +02:00
|
|
|
touch $(env_file) $(if $(VERBOSE)$(DEBUG),,2> /dev/null)
|
2021-02-09 17:05:00 +01:00
|
|
|
printenv \
|
|
|
|
|awk -F '=' 'NR == FNR { if($$1 !~ /^(#|$$)/) { A[$$1]; next } } !($$1 in A)' - $(env_dist) \
|
|
|
|
|cat $(env_over) - \
|
2022-06-30 23:37:10 +02:00
|
|
|
|awk 'BEGIN {split("$(MAKE_CMD_VARS)",vars," "); for (var in vars) {print vars[var]"="ENVIRON[vars[var]]};} {print}' \
|
2021-02-09 17:05:00 +01:00
|
|
|
|awk -F '=' '!seen[$$1]++' \
|
|
|
|
|awk -F '=' 'ARGV[1] == FILENAME { A[$$1]; next } ($$1 in A)' $(env_dist) - 2>/dev/null \
|
|
|
|
|awk -F '=' 'ARGV[1] == FILENAME { A[$$1]; next } !($$1 in A)' $(env_file) - 2>/dev/null \
|
|
|
|
|(IFS=$$'\n'; \
|
2021-05-30 23:45:30 +02:00
|
|
|
env $(env_reset) $(env.args) \
|
2021-02-09 17:05:00 +01:00
|
|
|
$$(env |awk -F '=' 'NR == FNR { if($$1 !~ /^(#|$$)/) { A[$$1]; next } } ($$1 in A)' $(env_dist) - \
|
|
|
|
|cat - $(env_over) \
|
|
|
|
|cat - $(env_dist) \
|
|
|
|
|sed -e /^$$/d -e /^#/d \
|
|
|
|
|awk -F '=' '!seen[$$1]++') \
|
|
|
|
awk '{while(match($$0,"[$$]{[^}]*}")) {var=substr($$0,RSTART+2,RLENGTH-3);gsub("[$$]{"var"}",ENVIRON[var])} print}') \
|
|
|
|
|sed -e /^$$/d -e /^#/d \
|
|
|
|
|sort \
|
2021-06-16 13:19:52 +02:00
|
|
|
>> $(env_file);
|
2021-02-09 17:05:00 +01:00
|
|
|
endef
|