//// gitproxy — Git remote helper + appview for AT Protocol PDS snapshots. import argv import gitproxy/appview import gitproxy/helper import gitproxy/oauth import gitproxy/pds import gitproxy/remote_spec import gitproxy/sys import gleam/int import gleam/result pub fn main() -> Nil { let args = argv.load().arguments case args { ["appview", ..rest] -> { let port = case rest { [p, ..] -> int.parse(p) |> result.unwrap(8787) [] -> sys.env_or("PORT", "8787") |> int.parse |> result.unwrap(8787) } appview.start(port) } ["login"] -> run_login("") ["login", handle] -> run_login(handle) ["logout"] -> run_logout("") ["logout", handle] -> run_logout(handle) ["whoami"] -> run_whoami("") ["whoami", handle] -> run_whoami(handle) ["remote-helper", url] -> run_remote_helper(url) ["remote-helper", _name, url] -> run_remote_helper(url) [url] -> run_remote_helper(url) [_name, url] -> run_remote_helper(url) _ -> { sys.write_stderr(usage()) sys.halt(2) } } } fn run_login(handle_arg: String) -> Nil { let handle = case handle_arg { "" -> sys.env_or("APROTO_HANDLE", "") h -> h } case handle { "" -> { sys.eprint("usage: gitproxy login ") sys.eprint(" or set APROTO_HANDLE") sys.halt(2) } handle -> case pds.resolve(handle) { Error(e) -> { sys.eprint(pds.error_message(e)) sys.halt(1) } Ok(identity) -> case oauth.login(handle, identity.pds, oauth.default_scope) { Error(e) -> { sys.eprint(oauth.error_message(e)) sys.halt(1) } Ok(session) -> { sys.write_stdout( "Logged in as " <> session.handle <> " (" <> session.did <> ")\n", ) sys.write_stdout( "Session saved under " <> sys.state_dir() <> "\n", ) sys.halt(0) } } } } } fn run_logout(handle_arg: String) -> Nil { let handle = case handle_arg { "" -> sys.env_or("APROTO_HANDLE", "") h -> h } case handle { "" -> { sys.eprint("usage: gitproxy logout ") sys.halt(2) } handle -> case oauth.delete_session(handle) { Error(e) -> { sys.eprint(oauth.error_message(e)) sys.halt(1) } Ok(_) -> { sys.write_stdout("Logged out " <> handle <> "\n") sys.halt(0) } } } } fn run_whoami(handle_arg: String) -> Nil { let handle = case handle_arg { "" -> sys.env_or("APROTO_HANDLE", "") h -> h } case handle { "" -> { sys.eprint("usage: gitproxy whoami ") sys.halt(2) } handle -> case oauth.load_session(handle) { Error(e) -> { sys.eprint(oauth.error_message(e)) sys.halt(1) } Ok(session) -> { sys.write_stdout( session.handle <> "\n" <> "did: " <> session.did <> "\n" <> "pds: " <> session.pds <> "\n" <> "scope: " <> session.scope <> "\n", ) sys.halt(0) } } } } fn run_remote_helper(url: String) -> Nil { let fallback = sys.env_or("APROTO_HANDLE", "") case remote_spec.parse(url, fallback) { Error(e) -> { sys.eprint(remote_spec.error_message(e)) sys.halt(1) } Ok(spec) -> case pds.live_from_env(spec.handle, pds.default_prefix()) { Error(e) -> { sys.eprint(pds.error_message(e)) sys.halt(1) } Ok(client) -> case helper.run(helper.stdio(), url, client) { Ok(_) -> sys.halt(0) Error(e) -> { sys.eprint(helper.error_message(e)) sys.halt(1) } } } } } fn usage() -> String { "gitproxy — store git repos as AT Protocol PDS snapshots Usage: gitproxy login [handle] Browser OAuth login (saves session) gitproxy logout [handle] Delete saved OAuth session gitproxy whoami [handle] Show saved session identity gitproxy remote-helper Run as git remote helper gitproxy appview [port] Browse snapshots over HTTP (default 8787) Git remotes (install bin/ on PATH): git remote add origin atgit://handle/repo git remote add origin aproto://handle/repo Auth (first match wins): 1. APROTO_APP_PASSWORD / BLUESKY_APP_PASSWORD (app password) 2. Saved OAuth session from `gitproxy login` 3. Interactive browser OAuth on first push/fetch Optional: APROTO_COLLECTION_PREFIX default org.latha.gitproxy APROTO_HANDLE fallback handle when URL is incomplete PORT appview port " }