2021-02-09 17:05:00 +01:00
|
|
|
##
|
2021-06-02 00:54:33 +02:00
|
|
|
# ENV
|
|
|
|
|
|
|
|
# target .env: Update .env
|
|
|
|
## it updates .env file when .env.dist file is newer
|
2021-02-09 17:05:00 +01:00
|
|
|
.env: .env.dist
|
2021-06-01 01:50:11 +02:00
|
|
|
$(call .env,,,$(wildcard $(PARAMETERS)/$(ENV)/$(APP)/.env .env.$(ENV)))
|
2021-02-09 17:05:00 +01:00
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# target .env-clean: Remove .env
|
2021-05-30 23:45:30 +02:00
|
|
|
.PHONY: .env-clean
|
|
|
|
.env-clean:
|
|
|
|
rm -f .env || true
|
|
|
|
|
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
|
|
|
|
|
|
|
|
env = $(env.args) $(env.dist) $(env.file)
|
|
|
|
env.docker = $(env.docker.args) $(env.docker.dist) $(env.docker.file)
|
2021-05-17 17:20:31 +02:00
|
|
|
|
2021-05-18 09:09:28 +02:00
|
|
|
env.args = $(foreach var,$(ENV_VARS),$(if $($(var)),$(var)='$($(var))'))
|
2021-05-17 17:20:31 +02:00
|
|
|
env.dist = $(shell printenv |awk -F '=' 'NR == FNR { if($$1 !~ /^(\#|$$)/) { A[$$1]; next } } ($$1 in A)' .env.dist - 2>/dev/null)
|
2021-06-02 00:54:33 +02:00
|
|
|
env.file = $(shell cat $(ENV_FILE) 2>/dev/null |sed '/^[ \t]*$$/d;/^[ \t]*\#/d;')
|
2021-05-18 09:09:28 +02:00
|
|
|
env.docker.args = $(foreach var,$(ENV_VARS),$(if $($(var)),-e $(var)='$($(var))'))
|
|
|
|
env.docker.dist = $(shell printenv |awk -F '=' 'NR == FNR { if($$1 !~ /^(\#|$$)/) { A[$$1]; next } } ($$1 in A) {print "-e "$$0}' .env.dist - 2>/dev/null)
|
2021-05-31 22:29:12 +02:00
|
|
|
env.docker.file = $(patsubst %,--env-file %,$(wildcard $(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
|
|
|
|
$(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
|
|
|
|
touch $(env_file)
|
|
|
|
printenv \
|
|
|
|
|awk -F '=' 'NR == FNR { if($$1 !~ /^(#|$$)/) { A[$$1]; next } } !($$1 in A)' - $(env_dist) \
|
|
|
|
|cat $(env_over) - \
|
|
|
|
|awk 'BEGIN {split("$(MAKECMDVARS)",vars," "); for (var in vars) {print vars[var]"="ENVIRON[vars[var]]};} {print}' \
|
|
|
|
|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 \
|
|
|
|
>> $(env_file)
|
|
|
|
endef
|