-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-file
More file actions
executable file
·57 lines (46 loc) · 1.46 KB
/
export-file
File metadata and controls
executable file
·57 lines (46 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env sh
# Usage: doc/api.md#export-file
# Create an alias
. "${SHELL_API_PATH}"/common/expand-aliases
alias export-file="export_file"
export_file(){
if [ -z "${1}" ]; then
printf '%s\n' "export-file: arguments are missing"
return 127
fi
target_env_file=$(mktemp)
shell_env_file=$(mktemp)
# Use the '-f' or '--force' flag to
# overwrite previously exported variables
if [ "$1" = "-f" ] || [ "$1" = "--force" ] ; then
shift $(( $# > 0 ? 1 : 0 ))
eval "rm -rf ${shell_env_file}"
unset shell_env_file
fi
# Loop over input files
while [ -n "${1:-}" ]; do
input_env_file="${1}"
shift $(( $# > 0 ? 1 : 0 ))
if [ ! -f "${input_env_file}" ]; then
printf '%s\n' "Required resource not found: '${input_env_file}'"
else
# Cache existing exported vars in a file so they can be preserved
# Calling 'set' in POSIX mode returns a list of shell variables
if [ -n "${shell_env_file}" ]; then set > "${shell_env_file}"; fi
# Remove leading whitespace from unquoted values
# shellcheck disable=SC2002
cat "${input_env_file}" | \
sed 's/\(=\s\+\)[^A-Za-z0-9_]\+/=/g' | \
tee "${target_env_file}" >/dev/null 2>&1
# Export the target variables
set -a
. "${target_env_file}"
# Restore previously exported variables that belong to the current process
if [ -n "${shell_env_file}" ]; then . "${shell_env_file}"; fi
set +a
fi
done
# Cleanup
rm -rf "${target_env_file}"
rm -rf "${shell_env_file}"
}