Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

1.2 KB
Raw Blame
import { Command } from 'commander';
import { authFileDisplayPath, clearTokenFromAllBackends } from '../../auth/token-store.ts';
import { resolveSignoutHost } from './validate-host.ts';

interface SignoutOptions {
  host: string;
}

async function runSignout(opts: SignoutOptions): Promise<void> {
  const { host } = opts;
  const { touched } = await clearTokenFromAllBackends(host);

  if (touched.length === 0) {
    process.stderr.write(`Not signed in to ${host}\n`);
    return;
  }

  process.stderr.write(`✓ Signed out from ${host}\n`);
  if (touched.includes('keychain')) {
    process.stderr.write(`  cleared from OS keychain\n`);
  }
  if (touched.includes('file')) {
    process.stderr.write(`  cleared from ${authFileDisplayPath()}\n`);
  }
}

export function signoutCommand(): Command {
  return new Command('signout')
    .description('Remove stored credentials')
    .option(
      '--host <host>',
      'GitHub or GitHub Enterprise hostname (default: the GitHub origin host, or github.com with no origin; required otherwise)',
    )
    .action(async (opts: Omit<SignoutOptions, 'host'> & { host?: string }) => {
      const host = resolveSignoutHost(opts.host);
      await runSignout({ ...opts, host });
    });
}