#!/usr/bin/env bash # Simulated Crossplane composition: turns a learner-authored XR into an # EngineeringTask that declares the construction work required to # satisfy the request. Emits YAML only — writes no code and does not # touch any repository. That is the whole point: Crossplane *requests* # engineering work; it does not do it. # # Usage: compose.sh # Writes /task/engineering-task.yaml set -euo pipefail XR="${1:?xr request path required}" WORK_ROOT="${2:?work-root path required}" if [ ! -f "$XR" ]; then printf 'compose: xr request not found: %s\n' "$XR" >&2 exit 2 fi TASK_DIR="$WORK_ROOT/task" TASK_OUT="$TASK_DIR/engineering-task.yaml" mkdir -p "$TASK_DIR" # Extract the three fields the composition promotes from the XR: # metadata.name, spec.value, and (implicitly) the artifact kind # selected by the XR's apiVersion/kind pair. FIELDS="$(XR_PATH="$XR" python3 <<'PY' import os, re, sys path = os.environ.get("XR_PATH", "") api_version = kind = name = value = None for line in open(path): m_api = re.match(r"^\s*apiVersion\s*:\s*(.+?)\s*$", line) m_kind = re.match(r"^\s*kind\s*:\s*(.+?)\s*$", line) m_name = re.match(r"^\s{2}name\s*:\s*(.+?)\s*$", line) m_val = re.match(r"^\s{2}value\s*:\s*(.+?)\s*$", line) if m_api and api_version is None: api_version = m_api.group(1) if m_kind and kind is None: kind = m_kind.group(1) if m_name and name is None: name = m_name.group(1) if m_val and value is None: value = m_val.group(1) def unquote(v): if v is None: return v v = v.strip() if len(v) >= 2 and v[0] == v[-1] and v[0] in ("'", '"'): v = v[1:-1] return v missing = [k for k, v in { "apiVersion": api_version, "kind": kind, "metadata.name": name, "spec.value": value, }.items() if not v] if missing: sys.stderr.write(f"compose: xr missing required fields: {missing}\n") sys.exit(3) print(unquote(api_version)) print(unquote(kind)) print(unquote(name)) print(unquote(value)) PY )" API_VERSION="$(printf '%s\n' "$FIELDS" | sed -n '1p')" KIND="$(printf '%s\n' "$FIELDS" | sed -n '2p')" NAME="$(printf '%s\n' "$FIELDS" | sed -n '3p')" VALUE="$(printf '%s\n' "$FIELDS" | sed -n '4p')" if [ "$KIND" != "XHelloWorldPico" ]; then printf 'compose: this composition only satisfies XHelloWorldPico requests (got %q)\n' "$KIND" >&2 exit 2 fi BRANCH="sandcastle/hello-world-pico-${NAME}" REPO_URL="file://./work/compose-sandcastle-request/target-repo" cat > "$TASK_OUT" <