shell-base/modules/utility/env.lib

105 lines
3.5 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# environment and environment file functions
module_load iecho
clean_env_file () {
local compact
module_load helpers
# -c will also remove comments and all empty lines
[[ $1 == "-c" ]] && compact=true && shift 1
#
# remove trailing spaces | remove double quotes
# | remove blanks after equals and quote value | remove any spaces before afer =
# remove blank lines | remove comment lines if requested
cat $1 | sed_ignore_comments s/\\s*$//g | sed_ignore_comments s/\"//g \
| sed_ignore_comments s/\(=[[:blank:]]*\)\(.*\)/\\1\"\\2\"/ \
| sed_ignore_comments s/\\s*=\\s*/=/g \
| sed -rz 's/^\n+//; s/\n+$/\n/g' | if [[ $compact ]]; then grep -v '^#' | grep -v "^$" ; else cat; fi
}
# https://stackoverflow.com/questions/19331497/set-environment-variables-from-file-of-key-value-pairs
env_file () {
env=${1:-.env}
[[ -f "${env}" ]] && { echo $env; return 0; } # || echo not $env
[[ -f "${env}.env" ]] && { echo "${env}.env"; return 0; } # || echo not ${env}.env
[[ -f "${env}/.env" ]] && { echo "${env}/.env"; return 0; } # || echo not ${env}/.env
return 1
}
# ignore non assignment lines
list_env_file_vars () {
env=$(env_file $1)
cat $env
[[ ! $env ]] && { iecho "Env file ${1} doesn't exist"; return 1; }
clean_env_file $env | grep -v '^#'| sed -n -E 's/(.*)=[^ =].*/\1/p' | xargs
}
function load_env_file() {
# second argument, "-x" will export within function (or anything called), "-gx" is equivalent of export and is global
env=$(env_file $1)
[[ ! $env ]] && { iecho "Env file ${1} doesn't exist"; return 1; }
while read line; do
# echo var to add $line
declare $2 "$(echo "${line}" | sed 's/\"//g' )"
done < <(clean_env_file -c $env)
}
# list_env_file_vals() {
# local vars
# vars=$(list_env_file_vars $1)
# echo $vars
# [[ $? -gt 0 ]] && return 1
# export_env_file $1
# for var in $vars ;
# do
# "$var=${!var}"
# done
# }
export_env_file () {
load_env_file $1 -gx
}
unset_env_file () {
env=$(env_file $1)
[[ ! $env ]] && { iecho "Env file ${1} doesn't exist"; return 1; }
unset $(clean_env_file -c ${env} | sed -E 's/(.*)=.*/\1/' | xargs)
}
testenv () {
echo in testenv
echo TEST Variable $TEST
env | grep TEST
echo done testenv
}
# https://www.baeldung.com/linux/envsubst-command
# todo allow input file -o overwrite or to another file -f <name>
env_subs_file () {
local fin; local fenv; local useenv; local tout; local fout; local fenv; local tout2
[[ $1 == "-e" ]] && useenv=true && shift 1
[[ $1 == "-o" ]] && { shift 1; fout=$1; }
[[ $1 == "-f" ]] && { fout=$2; shift 2; }
fin=$1; fenv=$2
# echo useenv $useenv, fout $fout, fin $fin, fenv $fenv
[[ ! $fin ]] && iecho "nothing was passed to merge with environment" && return 1
[[ ! -f "$fin" ]] && iecho "file $fin does not exit nothing to merge with environment" && return 1
[[ ! $(cat $fin | grep -F "$") ]] && iecho "no variables in $fin to merge" && return 2
tout=/tmp/${USER}-mergedenvfile
if [[ $useenv ]] && [[ -f $fenv ]]; then
tout2=/tmp/${USER}-mergedenvfile2
cat $fin | envsubst "$(printf '${%s} ' $(env | cut -d'=' -f1) )" > $tout2
fin=$tout2
fi
if [[ -f $fenv ]]; then
(export_env_file $fenv; cat $fin | envsubst "$(printf '${%s} ' $(clean_env_file -c $fenv | cut -d'=' -f1) )")
else
cat $fin | envsubst "$(printf '${%s} ' $(env | cut -d'=' -f1) )"
fi > $tout
[[ -f $tout2 ]] && rm -f $tout2
[[ $fout ]] && mv -f $tout $fout || { echo merged; cat "$tout"; }
# env | grep BASE
# # unset_env_file $2
}