Files
ISEP/scripts/lib/gitea-token.sh
T

45 lines
1.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# gitea-token.sh — 解出 Gitea 憑證(GITEA_TOKEN_CLAUDE_CODE)。source 它、呼叫 gitea_token()。
#
# 為什麼存在(inkstone/ISEP#47):舊法用 `git remote get-url gitea | sed` 從 remote URL
# 抽 token ⇒ token 必須明文嵌在 URL 裡 ⇒ 任何印出 remote URL 的訊息(git-lfs 的
# locksverify 那行)都會把它洩進 transcript/log。來源統一到這裡後,remote URL 才能改乾淨。
#
# 解析順序(先環境、後檔案——雲端只有 env、本機有 .env,兩邊都涵蓋):
# 1) 環境變數 $GITEA_TOKEN_CLAUDE_CODE(雲端 make-cloud-env 注入;本機 source .env 後也在)
# 2) .env 的 GITEA_TOKEN_CLAUDE_CODE=(先找 $CLAUDE_PROJECT_DIR/.env,再找 git 根的 .env
# —— 與既有 gitea_arm_token()scripts/lib/gitea-arm-common.sh)同一條路,照既有寫法走。
# 讀不到 → 印空字串、回非 0(呼叫者必須自己判空,不能把空字串當成功)。
#
# 🔴 只回值、不印別的——絕不 echo 出 token 以外的東西(避免洩進 log)。
# 內部:從一個 .env 檔抓 GITEA_TOKEN_CLAUDE_CODE=,去掉外圍引號。
_gitea_token_from_file() {
[ -f "$1" ] || return 1
_gt_val=$(grep '^GITEA_TOKEN_CLAUDE_CODE=' "$1" 2>/dev/null | head -1 | cut -d= -f2-)
_gt_val=${_gt_val%\"}; _gt_val=${_gt_val#\"}
_gt_val=${_gt_val%\'}; _gt_val=${_gt_val#\'}
[ -n "$_gt_val" ] || return 1
printf '%s' "$_gt_val"
return 0
}
gitea_token() {
# 1) 環境變數優先
if [ -n "${GITEA_TOKEN_CLAUDE_CODE:-}" ]; then
printf '%s' "$GITEA_TOKEN_CLAUDE_CODE"
return 0
fi
# 2) $CLAUDE_PROJECT_DIR/.env
if [ -n "${CLAUDE_PROJECT_DIR:-}" ] && _gitea_token_from_file "$CLAUDE_PROJECT_DIR/.env"; then
return 0
fi
# 3) git 根的 .env
_gt_root=$(git rev-parse --show-toplevel 2>/dev/null || true)
if [ -n "${_gt_root:-}" ] && _gitea_token_from_file "$_gt_root/.env"; then
return 0
fi
printf ''
return 1
}