feat: substitute environment into a file using envsubst
feat build_file no added file comment by default use -f to enablemaster
parent
4318f9ca89
commit
2aa4fa89a7
|
@ -14,10 +14,58 @@ isFile() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# env_file_pretty () {
|
||||||
|
|
||||||
|
# }
|
||||||
|
|
||||||
|
# https://www.baeldung.com/linux/envsubst-command
|
||||||
|
env_subs_file () {
|
||||||
|
module_load helpers
|
||||||
|
local input; local fenv; local all
|
||||||
|
[[ $1 == "-a" ]] && all=true && shift 1
|
||||||
|
input=$1
|
||||||
|
fenv=$2
|
||||||
|
[[ ! $input ]] && iecho "nothing was passed to merge with environment" && return 1
|
||||||
|
[[ -f "$input" ]] && input=$(cat "$input")
|
||||||
|
[[ ! $(echo $input | grep -F "$") ]] && iecho "nothing to merge in $1" && return 2
|
||||||
|
if [[ ($2 && ! -f $2) ]]; then
|
||||||
|
iecho "no enviroment file at $2" && return 3
|
||||||
|
else
|
||||||
|
# [[ $all ]] && export_file $fenv && unset fenv
|
||||||
|
if [[ $all ]]; then
|
||||||
|
fenv="/tmp/$USER/_tenvf"
|
||||||
|
# put quotes around values to avoid issues
|
||||||
|
env | sed 's/\(=[[:blank:]]*\)\(.*\)/\1"\2"/' > $fenv
|
||||||
|
cat $2 | sed 's/\(=[[:blank:]]*\)\(.*\)/\1"\2"/' | sed 's/\s*=\s*/=/g'
|
||||||
|
return
|
||||||
|
cat "$2" | sed 's/\(=[[:blank:]]*\)\(.*\)/\1"\2"/' >> $fenv
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
if [[ $fenv ]]; then
|
||||||
|
(set -a;source "$fenv" ;set +a; echo -e "$input" | envsubst "$(printf '${%s} ' $(cat "$fenv" | cut -d'=' -f1) )")
|
||||||
|
else
|
||||||
|
echo -e "$input" | envsubst "$(printf '${%s} ' $(env | cut -d'=' -f1) )"
|
||||||
|
fi
|
||||||
|
[[ $all ]] && rm -f $fenv
|
||||||
|
# [[ $all ]] && unset_file $2
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# shift 1
|
||||||
|
# input=$1
|
||||||
|
# set -a;. $2 ;set +a;
|
||||||
|
# echo env file is now $2
|
||||||
|
# fi
|
||||||
|
|
||||||
build_file () {
|
build_file () {
|
||||||
|
# local file
|
||||||
|
# echo build file $1 $2
|
||||||
|
[[ $1 == "-f" ]] && file=true && shift 1
|
||||||
[[ -f "$2" ]] || (iecho "output file $2 does not exist";return 1)
|
[[ -f "$2" ]] || (iecho "output file $2 does not exist";return 1)
|
||||||
if [[ -f "$1" ]]; then
|
if [[ -f "$1" ]]; then
|
||||||
echo -e "\n####### ADDED $1 ########" >> $2
|
# echo $1
|
||||||
|
[[ $file ]] && echo -e "\n####### ADDED $1 ########" >> $2
|
||||||
|
# remove comment lines, remove blank last line
|
||||||
cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2
|
cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2
|
||||||
else iecho "no such file $1 to append"
|
else iecho "no such file $1 to append"
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
iecho () {
|
iecho () {
|
||||||
[[ ! -z "$PS1" ]] && echo $1
|
[[ ! -z "$PS1" ]] && echo $1
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# no way to tell if sourced vs subshell
|
# no way to tell if sourced vs subshell
|
||||||
|
|
Loading…
Reference in New Issue