Files
herdr-issue/skills/herdr-issue/scripts/git-credential.sh
T

25 lines
1.2 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.
#!/usr/bin/env bash
# git credential helper:用 tea 預設 login 的帳號與 token 回應 HTTPS 認證,避免 git 要互動輸入帳密
# ("could not read Username ... No such device or address")。只處理 get,且只回應該 login 的主機。
[ "${1:-}" = get ] || exit 0
exec python3 -c '
import os, sys
req = dict(l.split("=", 1) for l in sys.stdin.read().splitlines() if "=" in l)
cfg = os.path.join(os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser("~/.config"), "tea", "config.yml")
logins, cur = [], None
for line in open(cfg):
s = line.strip()
if s.startswith("- name:"):
cur = {"name": s.split(":", 1)[1].strip()}; logins.append(cur)
elif cur is not None and ":" in s:
k, v = s.split(":", 1)
if k in ("url", "token", "user", "default"): cur[k] = v.strip().strip("\"\x27")
want = os.environ.get("HERDR_ISSUE_LOGIN")
pick = next((l for l in logins if l["name"] == want), None) if want else None
pick = pick or next((l for l in logins if l.get("default") == "true"), None)
if not pick or not pick.get("token"): sys.exit(0)
host = pick["url"].split("://", 1)[-1].rstrip("/")
if req.get("host") != host: sys.exit(0)
print("username=" + pick["user"]); print("password=" + pick["token"])
'