Repository navigation
Expand file tree
/
Copy pathload_env.sh
More file actions
49 lines (47 loc) · 2.24 KB
/
Copy pathload_env.sh
File metadata and controls
49 lines (47 loc) · 2.24 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
# Load the env files into the calling shell by parsing them, never by running
# them (#170). Source me from the repo root, in the same command as whatever
# needs the variables - an agent's tool starts a fresh shell per call:
#
# . scripts/load_env.sh; . scripts/rcli.sh; rcli PING
#
# Sourcing an env file runs it as shell code, so a `$(...)` or a backtick in a
# value executes, and the repo .env holds org-wide PATs. load_env reads plain
# KEY=value lines (an optional `export `, matched quotes) and exports each one.
# It still exports, so whatever either file holds reaches every child process:
# keep secrets a shell has no use for out of both.
#
# load_env is lifted in place from the vendored recipe in the header of
# skills/shipping-work/scripts/pre-ship.sh, which stays the only
# copy; init-project-fastapi's template lifts it with the same sed line.
#
# Order: /etc/replicator/.env, then the checkout's .env, so the developer's file
# wins. A linked worktree has no .env (it is git-ignored), so it falls back to
# the primary checkout's, as the recipe does. A missing file loads nothing.
#
# Everything runs inside one function so the caller's shell keeps no name of
# ours but load_env and what the files set. Bash only: it reads BASH_SOURCE.
_load_env_main() {
local _le_root _le_recipe _le_env _le_common PRE_SHIP_ENV_NAMES # the recipe appends key names to the last
_le_root=$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd) || return 1 # CDPATH would move it
_le_recipe="$_le_root/skills/shipping-work/scripts/pre-ship.sh"
unset -f load_env
[ -r "$_le_recipe" ] &&
eval "$(sed -n 's/^# //; /^load_env() [{]/,/^[}]/p' "$_le_recipe")"
if ! declare -F load_env >/dev/null; then
echo "load_env.sh: no load_env recipe in $_le_recipe - run: git submodule update --init" >&2
return 1
fi
_le_env="$_le_root/.env"
# Outside a repository there is no primary: never let `dirname ""` make it the cwd.
if [ ! -r "$_le_env" ] &&
_le_common=$(git -C "$_le_root" rev-parse --path-format=absolute --git-common-dir 2>/dev/null); then
_le_env="$(dirname "$_le_common")/.env"
fi
load_env /etc/replicator/.env && load_env "$_le_env"
}
if _load_env_main; then
unset -f _load_env_main
else
unset -f _load_env_main
return 1
fi