#!/usr/bin/env bash # #region Tooling.SourceRepositoryIdentity.OptionalEnv [C:2] [TYPE Module] # @BRIEF Attach optional origin metadata for publication without blocking local startup or image builds. # @POST An unavailable origin leaves the relevant environment variable unset and returns success. # #region Tooling.SourceRepositoryIdentity.OptionalEnv.Build [C:2] [TYPE Function] source_repository_identity_for_build() { local checkout="$1" writer="$2" identity_json unset SOURCE_REPOSITORY_IDENTITY_FILE SOURCE_REPOSITORY_IDENTITY_JSON if identity_json="$(python3 "$writer" --checkout "$checkout" 2>/dev/null)"; then SOURCE_REPOSITORY_IDENTITY_JSON="$identity_json" export SOURCE_REPOSITORY_IDENTITY_JSON fi return 0 } # #endregion Tooling.SourceRepositoryIdentity.OptionalEnv.Build # #region Tooling.SourceRepositoryIdentity.OptionalEnv.Run [C:2] [TYPE Function] source_repository_identity_for_run() { local checkout="$1" writer="$2" runtime_dir="$3" identity_json identity_file unset SOURCE_REPOSITORY_IDENTITY_FILE SOURCE_REPOSITORY_IDENTITY_JSON if ! identity_json="$(python3 "$writer" --checkout "$checkout" 2>/dev/null)"; then return 0 fi if ! identity_file="$(SOURCE_REPOSITORY_IDENTITY_JSON="$identity_json" python3 - "$runtime_dir" <<'PY' import os from pathlib import Path import tempfile import sys root = Path(sys.argv[1]) root.mkdir(parents=True, exist_ok=True, mode=0o700) with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", prefix="source-repository-", suffix=".json", dir=root, delete=False) as stream: stream.write(os.environ["SOURCE_REPOSITORY_IDENTITY_JSON"]) path = Path(stream.name) path.chmod(0o400) print(path) PY )"; then unset SOURCE_REPOSITORY_IDENTITY_FILE SOURCE_REPOSITORY_IDENTITY_JSON return 0 fi SOURCE_REPOSITORY_IDENTITY_FILE="$identity_file" export SOURCE_REPOSITORY_IDENTITY_FILE return 0 } # #endregion Tooling.SourceRepositoryIdentity.OptionalEnv.Run # #endregion Tooling.SourceRepositoryIdentity.OptionalEnv