diff --git a/modules/utility/file.lib b/modules/utility/file.lib index de0b625..ecf8576 100644 --- a/modules/utility/file.lib +++ b/modules/utility/file.lib @@ -14,10 +14,58 @@ isFile() { 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 () { + # local file + # echo build file $1 $2 + [[ $1 == "-f" ]] && file=true && shift 1 [[ -f "$2" ]] || (iecho "output file $2 does not exist";return 1) 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 else iecho "no such file $1 to append" fi diff --git a/modules/utility/iecho.func b/modules/utility/iecho.func index 9bc10ee..70df16b 100644 --- a/modules/utility/iecho.func +++ b/modules/utility/iecho.func @@ -1,6 +1,7 @@ #!/bin/bash iecho () { [[ ! -z "$PS1" ]] && echo $1 +return 0 } # no way to tell if sourced vs subshell