| 39 | import type { ClientCommandHandler } from './router-types.ts'; |
| 40 | |
| 41 | export const connectCommand: ClientCommandHandler = async ({ positionals, flags, client }) => { |
| 42 | const stateDir = resolveDaemonPaths(flags.stateDir).baseDir; |
| 43 | const provider = readConnectProvider(positionals); |
| 44 | assertConnectProviderUsage(provider, flags); |
| 45 | const resolved = await resolveConnectProfile({ provider, flags, stateDir }); |
| 46 | const connectFlags = resolved.flags; |
| 47 | const connectionMetadata = readRemoteConfigConnectionMetadata(resolved.remoteConfigPath); |
| 48 | const scope = readRequiredConnectScope(connectFlags, connectionMetadata); |
| 49 | const context = resolveConnectContext({ |
| 50 | stateDir, |
| 51 | flags: connectFlags, |
| 52 | remoteConfigPath: resolved.remoteConfigPath, |
| 53 | }); |
| 54 | assertCompatibleConnectionOrForce(context.previous, { |
| 55 | flags: connectFlags, |
| 56 | session: context.session, |
| 57 | remoteConfigPath: resolved.remoteConfigPath, |
| 58 | remoteConfigHash: context.remoteConfigHash, |
| 59 | desiredLeaseBackend: resolveRequestedLeaseBackend(connectFlags), |
| 60 | connection: connectionMetadata, |
| 61 | daemon: context.daemon, |
| 62 | }); |
| 63 | const state = buildConnectedState({ |
| 64 | flags: connectFlags, |
| 65 | scope, |
| 66 | connectionMetadata, |
| 67 | context, |
| 68 | remoteConfigPath: resolved.remoteConfigPath, |
| 69 | }); |
| 70 | writeRemoteConnectionState({ stateDir, state }); |
| 71 | await cleanupForcedPreviousConnection(client, stateDir, connectFlags, context.previous); |
| 72 | const leasePreparation = buildLeasePreparationNotice(state); |
| 73 | const runtimePreparation = buildRuntimePreparationNotice(connectFlags, state); |
| 74 | |
| 75 | writeCommandOutput(connectFlags, serializeConnectionState(state, runtimePreparation), () => |
| 76 | [ |
| 77 | `Connected remote session "${context.session}" tenant "${scope.tenant}" run "${scope.runId}" ${ |
| 78 | state.leaseId ? `lease ${state.leaseId}` : 'lease pending' |
| 79 | }`, |
| 80 | leasePreparation?.message, |
| 81 | runtimePreparation?.message, |
| 82 | ] |
| 83 | .filter((line): line is string => Boolean(line)) |
| 84 | .join('\n'), |
| 85 | ); |
| 86 | return true; |
| 87 | }; |
| 88 | |
| 89 | async function resolveConnectProfile(options: { |
| 90 | provider?: ConnectProvider; |