Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859// The CLI's signed-in identities: ~/.config/ziran/accounts.json (mode 600,// ZIRAN_CONFIG_DIR overrides the directory — tests point it at a temp// dir). Several accounts may be logged in at once; one is the default,// `--as <handle>` picks any other for a single command.
import { join } from 'jsr:@std/path@1';
export interface Account { handle: string; did: string; pds: string; password: string;}
export interface AccountsFile { default?: string; accounts: Account[];}
export function configDir(): string { const override = Deno.env.get('ZIRAN_CONFIG_DIR'); if (override) return override; const xdg = Deno.env.get('XDG_CONFIG_HOME'); if (xdg) return join(xdg, 'ziran'); const home = Deno.env.get('HOME') ?? Deno.env.get('USERPROFILE') ?? '.'; return join(home, '.config', 'ziran');}
const file = () => join(configDir(), 'accounts.json');
export async function loadAccounts(): Promise<AccountsFile> { try { const parsed = JSON.parse(await Deno.readTextFile(file())) as AccountsFile; return { default: parsed.default, accounts: parsed.accounts ?? [] }; } catch { return { accounts: [] }; }}
export async function saveAccounts(data: AccountsFile): Promise<void> { await Deno.mkdir(configDir(), { recursive: true }); await Deno.writeTextFile(file(), JSON.stringify(data, null, 2) + '\n'); // Passwords live here: owner-only, like .netrc. await Deno.chmod(file(), 0o600).catch(() => { // chmod may be unsupported (Windows); the file is still user-profile scoped });}
const norm = (handle: string) => handle.replace(/^@/, '').toLowerCase();
export function findAccount(data: AccountsFile, handle: string): Account | undefined { return data.accounts.find((a) => norm(a.handle) === norm(handle));}
export function defaultAccount(data: AccountsFile): Account | undefined { if (data.default) return findAccount(data, data.default) ?? data.accounts[0]; return data.accounts[0];}