({ positionals, flags })
| 5 | import type { ClientCommandHandler } from './router-types.ts'; |
| 6 | |
| 7 | export const authCommand: ClientCommandHandler = async ({ positionals, flags }) => { |
| 8 | const subcommand = positionals[0] ?? 'status'; |
| 9 | const stateDir = resolveDaemonPaths(flags.stateDir).baseDir; |
| 10 | if (subcommand === 'status') { |
| 11 | const status = summarizeCliSession({ stateDir }); |
| 12 | writeCommandOutput(flags, status, () => renderAuthStatus(status)); |
| 13 | return true; |
| 14 | } |
| 15 | if (subcommand === 'login') { |
| 16 | const login = await loginWithDeviceAuth({ |
| 17 | stateDir, |
| 18 | flags, |
| 19 | commandLabel: 'agent-device auth login', |
| 20 | }); |
| 21 | const data = { |
| 22 | authenticated: true, |
| 23 | source: 'cli-session', |
| 24 | sessionId: login.session.id, |
| 25 | cloudBaseUrl: login.session.cloudBaseUrl, |
| 26 | workspaceId: login.session.workspaceId, |
| 27 | accountId: login.session.accountId, |
| 28 | expiresAt: login.session.expiresAt, |
| 29 | agentTokenExpiresAt: login.expiresAt, |
| 30 | }; |
| 31 | writeCommandOutput(flags, data, () => 'Authenticated with cloud CLI session.'); |
| 32 | return true; |
| 33 | } |
| 34 | if (subcommand === 'logout') { |
| 35 | const removed = removeCliSession({ stateDir }); |
| 36 | writeCommandOutput(flags, { authenticated: false, removed }, () => |
| 37 | removed ? 'Removed stored cloud CLI session.' : 'No stored cloud CLI session.', |
| 38 | ); |
| 39 | return true; |
| 40 | } |
| 41 | throw new AppError('INVALID_ARGS', 'auth accepts only: status, login, logout'); |
| 42 | }; |
| 43 | |
| 44 | function renderAuthStatus(status: ReturnType<typeof summarizeCliSession>): string { |
| 45 | if (!status.authenticated) return 'Not authenticated.'; |
nothing calls this directly
no test coverage detected