#!/usr/bin/env bash set -euo pipefail readonly lore_url="${LORE_REPOSITORY_URL:-lore://lore.eirexe.moe/shinespark}" readonly default_branch="${LORE_DEFAULT_BRANCH:-main}" readonly assets_dir="${LORE_ASSETS_DIR:-assets}" branch="${1:-${LORE_BRANCH:-$default_branch}}" case "$branch" in ""|*[!A-Za-z0-9._/-]*) echo "error: invalid Lore branch name: $branch" >&2 exit 2 ;; esac if [[ -n "${LORE_IDENTITY_TOKEN:-}" ]]; then lore_args=(--identity-token "$LORE_IDENTITY_TOKEN") elif [[ -n "${LORE_CI_USERNAME:-}" || -n "${LORE_CI_PASSWORD:-}" ]]; then if [[ -z "${LORE_CI_USERNAME:-}" || -z "${LORE_CI_PASSWORD:-}" ]]; then echo "error: LORE_CI_USERNAME and LORE_CI_PASSWORD must be provided together" >&2 exit 2 fi : "${LORE_AUTH_URL:=https://auth.eirexe.moe}" export LORE_AUTH_URL token="$(python3 - <<'PY' import json import os import sys import urllib.error import urllib.request request = urllib.request.Request( os.environ["LORE_AUTH_URL"].rstrip("/") + "/auth/login", data=json.dumps({ "username": os.environ["LORE_CI_USERNAME"], "password": os.environ["LORE_CI_PASSWORD"], }).encode(), headers={"Content-Type": "application/json"}, method="POST", ) try: with urllib.request.urlopen(request) as response: payload = json.load(response) except urllib.error.HTTPError as error: print(f"Lore authentication failed with HTTP {error.code}", file=sys.stderr) sys.exit(1) token = payload.get("token") if not isinstance(token, str) or not token: print("Lore authentication response did not contain a token", file=sys.stderr) sys.exit(1) print(token) PY )" if [[ -n "${GITHUB_ENV:-}" ]]; then echo "::add-mask::$token" fi lore_args=(--identity-token "$token") else lore_args=() fi if [[ -e "$assets_dir" && ! -d "$assets_dir" ]]; then echo "error: $assets_dir exists but is not a directory" >&2 exit 1 fi if [[ -d "$assets_dir/.lore" ]]; then if ! lore "${lore_args[@]}" --repository "$assets_dir" repository info >/dev/null; then echo "error: $assets_dir is not a usable Lore checkout" >&2 exit 1 fi if ! lore "${lore_args[@]}" --repository "$assets_dir" branch list --no-pager \ | grep -Fqx " $branch"; then if [[ "$branch" != "$default_branch" ]]; then echo "warning: Lore branch does not exist: $branch; using $default_branch" >&2 branch="$default_branch" else echo "error: Lore branch does not exist: $branch" >&2 exit 1 fi fi lore "${lore_args[@]}" --repository "$assets_dir" sync "$branch@LATEST" exit 0 fi if [[ -d "$assets_dir" && -n "$(find "$assets_dir" -mindepth 1 -maxdepth 1 -print -quit)" ]]; then echo "error: refusing to overwrite non-Lore directory: $assets_dir" >&2 exit 1 fi metadata_dir="$(mktemp -d)" trap 'rm -rf "$metadata_dir"' EXIT lore "${lore_args[@]}" --non-interactive clone --bare "$lore_url" "$metadata_dir" if ! lore "${lore_args[@]}" --repository "$metadata_dir" branch list --no-pager \ | grep -Fqx " $branch"; then if [[ "$branch" != "$default_branch" ]]; then echo "warning: Lore branch does not exist: $branch; using $default_branch" >&2 branch="$default_branch" else echo "error: Lore branch does not exist: $branch" >&2 echo "available branches:" >&2 lore "${lore_args[@]}" --repository "$metadata_dir" branch list --no-pager >&2 exit 1 fi fi rm -rf "$assets_dir" lore "${lore_args[@]}" --non-interactive clone --branch "$branch" "$lore_url" "$assets_dir" lore "${lore_args[@]}" --repository "$assets_dir" status --revision-only --no-pager