Skip to content

Instantly share code, notes, and snippets.

@LeKovr
Last active June 20, 2023 21:54
Show Gist options
  • Save LeKovr/2697fe02504d7c081b0bf79427c93db6 to your computer and use it in GitHub Desktop.
Save LeKovr/2697fe02504d7c081b0bf79427c93db6 to your computer and use it in GitHub Desktop.
.env.sample generation from app Makefile vars with notes prefixed by '#- ...' comment
# app custom Makefile
SHELL = /bin/sh
CFG = .env
# ------------------------------------------------------------------------------
# app custom config
# comments prefixed with '#- ' will be copied to $(CFG).sample
#- Postgresql container name (access via docker)
PG_CONTAINER ?= dcape_db_1
#- PowerDNS DB user name
PGUSER ?= pdns
#- PowerDNS DB name
PGDATABASE ?= pdns
# ------------------------------------------------------------------------------
-include $(CFG)
export
# This code generates $(CFG).sample from Makefile vars with previous comment line(s)
# Internal: generate config sample data
.env.temp.mk:
@echo "define CFVAR" > $@
@grep -A 1 -h "^#- " $(MAKEFILE_LIST) | grep -vE "^--" \
| sed -E 's/^([^\n ]+)\ *\??=([^\n]*)$$/\1=$$(\1)\n/ ; s/^(#)-/\1/' >> $@
@echo "endef" >> $@
ifneq ($(findstring $(MAKECMDGOALS),config $(CFG).sample),)
include .env.temp.mk
endif
# Internal: generate config sample
$(CFG).sample: .env.temp.mk
@echo -e "# app config file, generated by 'make config'\n" > $@
@echo "$$CFVAR" >> $@
@rm -f $<
## generate sample config
config: $(CFG).sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment