# shellcheck shell=bash # The job's config and the database (docs/AGENT-1.0-PLAN.md §5). # # CROSSCODE_CONFIG is a JSON file with the .env values by layer ({ server, defaults, install }), # given with --config by the agent (it opens the sealed config from the Hub, writes it mode 600 # and removes it afterwards). By hand there's usually none, and the script's own defaults apply. # # Database: the server layer can name a PostgreSQL server (DB_HOST, DB_PORT) and an admin # connection that may create databases and roles (DB_ADMIN_URL). Without DB_ADMIN_URL the local # PostgreSQL is used through `sudo -u postgres`, as before. CROSSCODE_CONFIG="${CROSSCODE_CONFIG:-}" # cfg [product]: a value from the config, "" when there's none. cfg() { [ -n "$CROSSCODE_CONFIG" ] && [ -f "$CROSSCODE_CONFIG" ] || return 0 node -e ' const c = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8")); const [layer, key, product] = process.argv.slice(2); const v = layer === "install" ? ((c.install || {})[product] || {})[key] : (c[layer] || {})[key]; if (v !== undefined && v !== null) process.stdout.write(String(v));' "$CROSSCODE_CONFIG" "$1" "$2" "${3:-}" } # env_template : the release's template, or the copy shipped with the # script (for releases from before templates, e.g. events@2.2.0). env_template() { if [ -f "$1/env.template.json" ]; then echo "$1/env.template.json"; else echo "$CROSSCODE_HOME/products/$2.env.template.json"; fi } # env_apply [envtpl options…]: writes or appends the .env files. # Generated values come in as CROSSCODE_GEN_ environment variables. env_apply() { local app="$1" product="$2" release="$3" shift 3 local -a conf=() [ -n "$CROSSCODE_CONFIG" ] && conf=(--config "$CROSSCODE_CONFIG") node "$CROSSCODE_HOME/lib/envtpl.cjs" --template "$(env_template "$app" "$product")" --dir "$app" \ --product "$product" --release "$release" "${conf[@]+"${conf[@]}"}" "$@" } # env_apply_template