keep the dynamism of make in pure shell
Two mechanisms, matching what the make engine actually did:
Lazy defaults. A stack setting is a function myos_default_<VAR>, called only
when the variable has no value, and called again at every reference. That is
exactly a recursive ?=: an explicit value wins, and the default follows a
DOMAIN that a .env changes later. The prefix is what makes it safe; the first
version used a bare function named after the variable, and the test suite
caught it running /usr/bin/host for a stack group called host.
Templates. myos env-update fills a .env from the .env.dist files, expanding
${VAR} against the current values and running $(command), forward references
included.
Also fixed: the project .env now wins over /etc/conf.d/myos, which is what the
documentation claimed and the code did not.
share/make/shim.mk lets a project keep make as a front end: every myos command
becomes a target that shells out to bin/myos, and the project keeps its own
targets and its stack .mk files. It sits outside make/ because the legacy
engine globs every .mk in there.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
#shellcheck shell=sh
|
||||
# myos env-update fill the .env of the workdir from the .env.dist it finds
|
||||
#
|
||||
# Templates are read from the workdir and from every requested stack, so a
|
||||
# stack can ship the variables it expects and their defaults.
|
||||
myos_cmd_env_update() {
|
||||
_target=${ENV_FILE:-$WORKDIR/.env}
|
||||
_dists=$MYOS_ARGS
|
||||
if [ -z "$_dists" ]; then
|
||||
for _ref in $MYOS_STACKS; do
|
||||
_d=$(myos_stack_resolve "$_ref" 2>/dev/null) || continue
|
||||
for _c in "$_d/.env.dist" "$_d/$(myos_stack_name "$_ref").env.dist"; do
|
||||
[ -f "$_c" ] && _dists="${_dists:+$_dists }$_c"
|
||||
done
|
||||
done
|
||||
for _c in "$WORKDIR/.env.dist" "$WORKDIR/.env.example" "$WORKDIR/.env.sample"; do
|
||||
[ -f "$_c" ] && _dists="${_dists:+$_dists }$_c"
|
||||
done
|
||||
fi
|
||||
[ -n "$_dists" ] || { myos_warning "no .env.dist found for $MYOS_STACKS"; return 0; }
|
||||
for _dist in $_dists; do
|
||||
myos_info "env-update $_target from $_dist"
|
||||
myos_env_update "$_target" "$_dist" "$WORKDIR/.env.$ENV"
|
||||
done
|
||||
printf '%s\n' "$_target"
|
||||
}
|
||||
@@ -80,3 +80,94 @@ myos_env_export() {
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
# myos_expand STRING substitute ${VAR} and $(command) in STRING.
|
||||
# shellcheck disable=SC2016 # the single quotes are deliberate: these patterns
|
||||
# match the literal characters ${ and $( in the input, they are not expansions
|
||||
# This is what the make engine did when it generated a .env out of a .env.dist:
|
||||
# ${VAR} takes the current value, $(cmd) runs the command. Nothing else is
|
||||
# interpreted, so the rest of the line can hold anything.
|
||||
myos_expand() {
|
||||
_in=$1
|
||||
_guard=0
|
||||
while [ "$_guard" -lt 16 ]; do
|
||||
_guard=$((_guard + 1))
|
||||
case $_in in
|
||||
*'${'*'}'*)
|
||||
_pre=${_in%%'${'*}
|
||||
_rest=${_in#*'${'}
|
||||
_name=${_rest%%\}*}
|
||||
_post=${_rest#*\}}
|
||||
case $_name in
|
||||
''|*[!A-Za-z0-9_]*) _in="$_pre\${$_name}$_post"; break ;;
|
||||
esac
|
||||
_in="$_pre$(myos_var "$_name")$_post" ;;
|
||||
*) break ;;
|
||||
esac
|
||||
done
|
||||
_guard=0
|
||||
while [ "$_guard" -lt 16 ]; do
|
||||
_guard=$((_guard + 1))
|
||||
case $_in in
|
||||
*'$('*')'*)
|
||||
_pre=${_in%%'$('*}
|
||||
_rest=${_in#*'$('}
|
||||
_cmd=${_rest%%)*}
|
||||
_post=${_rest#*)}
|
||||
_in="$_pre$(eval "$_cmd" 2>/dev/null)$_post" ;;
|
||||
*) break ;;
|
||||
esac
|
||||
done
|
||||
printf '%s' "$_in"
|
||||
}
|
||||
|
||||
# myos_env_update FILE DIST [OVER...]
|
||||
# Add to FILE every variable of DIST that is missing from it, expanded.
|
||||
# A variable that already has a value keeps it, whether it comes from the
|
||||
# environment, from FILE, or from one of the OVER files: the .env is a record
|
||||
# of the choices already made, never something that overwrites them.
|
||||
myos_env_update() {
|
||||
_file=$1; _dist=$2; shift 2
|
||||
[ -f "$_dist" ] || return 0
|
||||
[ -e "$_file" ] || : > "$_file"
|
||||
|
||||
# what the overrides pin, read before anything else
|
||||
for _over in "$@"; do
|
||||
[ -f "$_over" ] || continue
|
||||
myos_dotenv_load "$_over"
|
||||
done
|
||||
|
||||
# Which keys already hold a choice, made in the environment, in the .env or
|
||||
# in an override. Those are kept verbatim; everything else is a template.
|
||||
_preset=" "
|
||||
while IFS= read -r _kv; do
|
||||
[ -n "$_kv" ] || continue
|
||||
_k=${_kv%%=*}
|
||||
[ -n "$_k" ] || continue
|
||||
[ -n "$(myos_var "$_k")" ] && _preset="$_preset$_k "
|
||||
done <<EOF
|
||||
$(myos_dotenv_parse "$_dist")
|
||||
EOF
|
||||
|
||||
# Make the templates themselves visible, so a line may refer to a variable
|
||||
# defined further down the file, as the make engine allowed.
|
||||
myos_dotenv_load "$_dist"
|
||||
|
||||
_added=0
|
||||
while IFS= read -r _kv; do
|
||||
[ -n "$_kv" ] || continue
|
||||
_k=${_kv%%=*}
|
||||
[ -n "$_k" ] || continue
|
||||
myos_dotenv_has "$_file" "$_k" && continue
|
||||
case $_preset in
|
||||
*" $_k "*) _v=$(myos_var "$_k") ;;
|
||||
*) _v=$(myos_expand "${_kv#*=}") ;;
|
||||
esac
|
||||
printf '%s=%s\n' "$_k" "$_v" >> "$_file"
|
||||
_added=$((_added + 1))
|
||||
done <<EOF
|
||||
$(myos_dotenv_parse "$_dist")
|
||||
EOF
|
||||
[ "$_added" -gt 0 ] && myos_info "added $_added variable(s) to $_file"
|
||||
return 0
|
||||
}
|
||||
|
||||
+3
-6
@@ -4,12 +4,9 @@
|
||||
# Ported from make/apps/def.mk (uri, url, urlprefix, urlprefixs, tagprefix,
|
||||
# envprefix, servicenvs). Registrator publishes the SERVICE_<port>_TAGS label
|
||||
# to consul, fabio routes on the urlprefix- tags it finds there.
|
||||
|
||||
# myos_var NAME value of the variable named NAME, empty when unset
|
||||
myos_var() {
|
||||
[ -n "${1:-}" ] || return 0
|
||||
eval "printf '%s' \"\${$1:-}\""
|
||||
}
|
||||
#
|
||||
# Every lookup goes through myos_var (lib/var.sh), so a stack setting may be a
|
||||
# plain value or a lazy default, and the two behave the same here.
|
||||
|
||||
# myos_uri SERVICE PORT [BASE_URI]
|
||||
# <service>.<base uri>, unless <SERVICE>_SERVICE[_<port>]_NAME overrides the prefix
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
#shellcheck shell=sh
|
||||
# var: variable resolution, with the lazy defaults the make engine had.
|
||||
#
|
||||
# make gives every `VAR ?= $(call ...)` two properties at once: an explicit
|
||||
# value wins, and the default is re-evaluated at each reference, so it sees
|
||||
# whatever a .env loaded later has changed.
|
||||
#
|
||||
# The shell gets both by keeping defaults in functions: `myos_var NAME` reads
|
||||
# the variable when it has a value, and otherwise calls the function
|
||||
# `myos_default_NAME`. The prefix matters: a bare function named after the
|
||||
# variable would collide with commands on PATH, and a stack setting called
|
||||
# `host` or `test` would then run a program instead of returning a value.
|
||||
#
|
||||
# myos_default_HOST_FABIO_SERVICE_9998_TAGS() { myos_tagprefix HOST_FABIO 9998; }
|
||||
#
|
||||
# is the exact equivalent of
|
||||
#
|
||||
# HOST_FABIO_SERVICE_9998_TAGS ?= $(call tagprefix,HOST_FABIO,9998)
|
||||
|
||||
MYOS_VAR_MAX_DEPTH=${MYOS_VAR_MAX_DEPTH:-32}
|
||||
|
||||
# myos_var NAME the value of NAME: the variable if it has one, else the lazy
|
||||
# default, else empty.
|
||||
myos_var() {
|
||||
[ -n "${1:-}" ] || return 0
|
||||
eval "_myos_set=\${$1+yes}"
|
||||
if [ "${_myos_set:-}" = yes ]; then
|
||||
eval "printf '%s' \"\$$1\""
|
||||
return 0
|
||||
fi
|
||||
myos_var_is_lazy "$1" || return 0
|
||||
|
||||
# a default written in terms of itself would loop for ever
|
||||
_myos_depth=$((${MYOS_VAR_DEPTH:-0} + 1))
|
||||
if [ "$_myos_depth" -gt "$MYOS_VAR_MAX_DEPTH" ]; then
|
||||
myos_error "variable $1 is defined in terms of itself"
|
||||
return 1
|
||||
fi
|
||||
MYOS_VAR_DEPTH=$_myos_depth "myos_default_$1"
|
||||
}
|
||||
|
||||
# myos_default NAME BODY declare a lazy default from a string, for callers
|
||||
# that build the variable name at run time
|
||||
myos_default() {
|
||||
eval "myos_default_$1() { $2; }"
|
||||
}
|
||||
|
||||
# myos_var_is_lazy NAME true when NAME has no value but has a lazy default
|
||||
myos_var_is_lazy() {
|
||||
eval "_myos_set=\${$1+yes}"
|
||||
[ "${_myos_set:-}" = yes ] && return 1
|
||||
# a shell function, never a command on PATH: command -v prints the name back
|
||||
# for a function and a path for a program
|
||||
_myos_fn=$(command -v "myos_default_$1" 2>/dev/null) || return 1
|
||||
[ "$_myos_fn" = "myos_default_$1" ]
|
||||
}
|
||||
Reference in New Issue
Block a user