The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 2 | import * as Config from "effect/Config"; |
| 3 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 4 | import * as Console from "effect/Console"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import { Command, Flag, GlobalFlag, Prompt } from "effect/cli"; |
| 9 | import * as CliError from "effect/cli/CliError"; |
| 10 | |
| 11 | import packageJson from "../../package.json" with { type: "json" }; |
| 12 | import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 13 | import * as BootService from "../cloud/bootService.ts"; |
| 14 | import * as CliState from "../cloud/CliState.ts"; |
| 15 | import { CLOUD_ENDPOINT_RUNTIME_CONFIG, CLOUD_LINK_REVOKED } from "../cloud/config.ts"; |
| 16 | import { hasCloudPublicConfig } from "../cloud/publicConfig.ts"; |
| 17 | import { compareExactServiceVersions } from "../cloud/serviceProtocol.ts"; |
| 18 | import * as ServerConfig from "../config.ts"; |
| 19 | import * as ManagedLimits from "../policy/managedLimits.ts"; |
| 20 | import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; |
| 21 | import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts"; |
| 22 | import { |
| 23 | connectSubcommands, |
| 24 | disconnectCloud, |
| 25 | headlessFlag, |
| 26 | linkEnvironmentForConnect, |
| 27 | requireConnectAllowed, |
| 28 | runCloudCommand, |
| 29 | waitForReachability, |
| 30 | } from "./connect.ts"; |
| 31 | import { resolveHostCliCommand } from "./invocation.ts"; |
| 32 | import * as CliService from "./service.ts"; |
| 33 | |
| 34 | type Route = "connect" | "lan"; |
| 35 | type Routes = Readonly<Record<Route, boolean>>; |
| 36 | |
| 37 | /** How other devices reach this machine now. */ |
| 38 | interface Setup { |
| 39 | readonly service: BootService.BootServiceStatus; |
| 40 | /** Null in builds without T3 Connect. Off when T3 Connect revoked the link. */ |
| 41 | readonly connect: boolean | null; |
| 42 | /** T3 Connect revoked the link; `t3 connect` links again. */ |
| 43 | readonly connectRevoked: boolean; |
| 44 | readonly lan: boolean; |
| 45 | /** An installed service with no T3CODE_LAN or T3CODE_HOST keeps the old default: on. */ |
| 46 | readonly lanFromOldDefault: boolean; |
| 47 | readonly host: string | undefined; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * T3 Connect is on when this CLI asked for a managed link, or when the desktop or |
| 52 | * web app made one: that saves the tunnel config but no CLI marker. Revoked is off. |
| 53 | */ |
| 54 | export const connectEnabled = (link: { |
| 55 | readonly cliDesired: boolean; |
| 56 | readonly cliMode: CliState.CliDesiredLinkMode; |
| 57 | readonly managedTunnel: boolean; |
| 58 | readonly revoked: boolean; |
| 59 | }) => |
| 60 | // The server starts no tunnel for a CLI publish-only link, even with an old tunnel config. |
| 61 | !link.revoked && (link.cliDesired ? link.cliMode === "managed" : link.managedTunnel); |
| 62 | |
| 63 | const readSetup = Effect.fn("cli.up.read_setup")(function* () { |
| 64 | const { baseDir } = yield* ServerConfig.ServerConfig; |
| 65 | const service = yield* (yield* BootService.BootService).status; |
| 66 | const { entries } = yield* CliService.readServiceEnv(baseDir); |
| 67 | // Parsed as the server parses it. |
| 68 | const lan = yield* Config.Boolean("T3CODE_LAN") |
| 69 | .pipe(Config.option) |
| 70 | .parse(ConfigProvider.fromEnv({ env: entries })); |
| 71 | const secrets = yield* ServerSecretStore.ServerSecretStore; |
| 72 | const connectRevoked = |
| 73 | hasCloudPublicConfig && Option.isSome(yield* secrets.get(CLOUD_LINK_REVOKED)); |
| 74 | const connect = hasCloudPublicConfig |
| 75 | ? connectEnabled({ |
| 76 | cliDesired: yield* CliState.readCliDesiredCloudLink, |
| 77 | cliMode: yield* CliState.readCliDesiredLinkMode, |
| 78 | managedTunnel: Option.isSome(yield* secrets.get(CLOUD_ENDPOINT_RUNTIME_CONFIG)), |
| 79 | revoked: connectRevoked, |
| 80 | }) |
| 81 | : null; |
| 82 | // Another home's service doesn't read this home's service.env. |
| 83 | const servesThisHome = service.installed && service.otherBaseDir === undefined; |
| 84 | return { |
| 85 | service, |
| 86 | connect, |
| 87 | connectRevoked, |
| 88 | lan: Option.getOrElse(lan, () => servesThisHome), |
| 89 | lanFromOldDefault: Option.isNone(lan) && servesThisHome && entries.T3CODE_HOST === undefined, |
| 90 | host: entries.T3CODE_HOST, |
| 91 | } satisfies Setup; |
| 92 | }); |
| 93 | |
| 94 | /** |
| 95 | * The routes `t3 up` starts from. With no route flag it asks about every |
| 96 | * route, starting a machine with no service from T3 Connect. Any route flag |
| 97 | * means it asks nothing and keeps the routes not named as they are. |
| 98 | */ |
| 99 | export const planRoutes = ( |
| 100 | setup: { readonly installed: boolean; readonly connect: boolean | null; readonly lan: boolean }, |
| 101 | flags: { readonly connect: Option.Option<boolean>; readonly lan: Option.Option<boolean> }, |
| 102 | ) => { |
| 103 | const ask = Option.isNone(flags.connect) && Option.isNone(flags.lan); |
| 104 | const start = { |
| 105 | connect: setup.connect === true || (ask && !setup.installed && setup.connect !== null), |
| 106 | lan: setup.lan, |
| 107 | }; |
| 108 | return { |
| 109 | routes: { |
| 110 | connect: Option.getOrElse(flags.connect, () => start.connect), |
| 111 | lan: Option.getOrElse(flags.lan, () => start.lan), |
| 112 | } satisfies Routes, |
| 113 | ask: ask |
| 114 | ? (["connect", "lan"] as const).filter((r) => r !== "connect" || setup.connect !== null) |
| 115 | : [], |
| 116 | }; |
| 117 | }; |
| 118 | |
| 119 | /** A running service reads T3CODE_LAN and its T3 Connect link only when it starts. */ |
| 120 | const needsRestart = (before: Routes, after: Routes) => |
| 121 | after.lan !== before.lan || (after.connect && !before.connect); |
| 122 | |
| 123 | const CHOICES = { |
| 124 | connect: { title: "T3 Connect", description: "Creates a tunnel to control your PC." }, |
| 125 | lan: { title: "Local network", description: "LAN and tailnet. Pairing code required." }, |
| 126 | } satisfies Record<Route, { readonly title: string; readonly description: string }>; |
| 127 | |
| 128 | /** The commands hints name, run with the caller's --base-dir and launcher. */ |
| 129 | const resolveHints = Effect.fn("cli.up.resolve_hints")(function* (baseDir: Option.Option<string>) { |
| 130 | const baseDirArgs = Option.match(baseDir, { |
| 131 | onNone: () => [], |
| 132 | onSome: (dir) => ["--base-dir", dir], |
| 133 | }); |
| 134 | const command = (subcommand: string, args: ReadonlyArray<string> = []) => |
| 135 | Effect.map(resolveHostCliCommand(subcommand, [...baseDirArgs, ...args]), (r) => r.command); |
| 136 | return { |
| 137 | run: yield* command(""), |
| 138 | pair: yield* command("pair"), |
| 139 | up: yield* command("up"), |
| 140 | upNoLan: yield* command("up", ["--no-lan"]), |
| 141 | connect: yield* command("connect"), |
| 142 | }; |
| 143 | }); |
| 144 | type Hints = Effect.Success<ReturnType<typeof resolveHints>>; |
| 145 | |
| 146 | const formatSetup = (setup: Setup, hints: Hints) => { |
| 147 | const { service } = setup; |
| 148 | // This home's access settings reach no service, so only the service is shown. |
| 149 | if (service.otherBaseDir !== undefined) { |
| 150 | return [ |
| 151 | ` ${"Service".padEnd(15)}installed for another T3 home · ${service.otherBaseDir}`, |
| 152 | "", |
| 153 | `Pass \`${BootService.formatBootServiceBaseDirOption(service.otherBaseDir)}\` to manage it.`, |
| 154 | ].join("\n"); |
| 155 | } |
| 156 | const rows: ReadonlyArray<readonly [string, string]> = [ |
| 157 | [ |
| 158 | "Service", |
| 159 | !service.supported |
| 160 | ? "unavailable" |
| 161 | : service.installed |
| 162 | ? `installed · t3@${service.installedVersion ?? packageJson.version}` |
| 163 | : "not installed", |
| 164 | ], |
| 165 | ...(setup.connect === null |
| 166 | ? [] |
| 167 | : [ |
| 168 | [ |
| 169 | "T3 Connect", |
| 170 | setup.connect |
| 171 | ? "on · your T3 account" |
| 172 | : setup.connectRevoked |
| 173 | ? `off · link revoked. Run \`${hints.connect}\`.` |
| 174 | : "off", |
| 175 | ] as const, |
| 176 | ]), |
| 177 | [ |
| 178 | "Local network", |
| 179 | setup.host !== undefined |
| 180 | ? `overridden · T3CODE_HOST=${setup.host}` |
| 181 | : setup.lanFromOldDefault |
| 182 | ? "on · kept from an older install" |
| 183 | : setup.lan |
| 184 | ? "on · pairing code only" |
| 185 | : "off", |
| 186 | ], |
| 187 | ]; |
| 188 | const notes = [ |
| 189 | ...(setup.host === undefined && setup.lan |
| 190 | ? ["Anyone on your LAN or tailnet can reach the port. A pairing code is the only lock."] |
| 191 | : []), |
| 192 | ...(setup.lanFromOldDefault |
| 193 | ? [`Older installs keep LAN access on. Turn it off: \`${hints.upNoLan}\`.`] |
| 194 | : []), |
| 195 | ]; |
| 196 | return [ |
| 197 | ...rows.map(([label, value]) => ` ${label.padEnd(15)}${value}`), |
| 198 | ...(notes.length === 0 ? [] : ["", ...notes]), |
| 199 | ].join("\n"); |
| 200 | }; |
| 201 | |
| 202 | const formatNextSteps = (routes: Routes, serviceInstalled: boolean, hints: Hints) => |
| 203 | [ |
| 204 | ...(routes.connect |
| 205 | ? ["On your other device, sign in to T3 Connect with the same account."] |
| 206 | : []), |
| 207 | ...(routes.lan ? [`Run \`${hints.pair}\`. Use the code on your other device.`] : []), |
| 208 | ...(routes.connect || routes.lan |
| 209 | ? [] |
| 210 | : [ |
| 211 | serviceInstalled |
| 212 | ? "Remote access is off. T3 Code still runs here in the background." |
| 213 | : `Remote access is off. Run \`${hints.run}\` to use T3 Code here.`, |
| 214 | ]), |
| 215 | ].join("\n"); |
| 216 | |
| 217 | const interactiveTerminal = () => Boolean(process.stdin.isTTY && process.stdout.isTTY); |
| 218 | |
| 219 | const setUp = Effect.fn("cli.up")(function* (flags: { |
| 220 | readonly baseDir: Option.Option<string>; |
| 221 | readonly connect: Option.Option<boolean>; |
| 222 | readonly lan: Option.Option<boolean>; |
| 223 | readonly yes: boolean; |
| 224 | readonly headless: boolean; |
| 225 | }) { |
| 226 | const { baseDir } = yield* ServerConfig.ServerConfig; |
| 227 | const service = yield* BootService.BootService; |
| 228 | const baseDirArgs = Option.match(flags.baseDir, { |
| 229 | onNone: () => [], |
| 230 | onSome: (dir) => ["--base-dir", dir], |
| 231 | }); |
| 232 | const hints = yield* resolveHints(flags.baseDir); |
| 233 | const before = yield* readSetup(); |
| 234 | // Before any question or write: install would refuse only after both. |
| 235 | if (before.service.otherBaseDir !== undefined) { |
| 236 | return yield* new BootService.BootServiceOtherBaseDirError({ |
| 237 | installedBaseDir: before.service.otherBaseDir, |
| 238 | baseDir, |
| 239 | }); |
| 240 | } |
| 241 | if (before.connect === null && Option.getOrElse(flags.connect, () => false)) { |
| 242 | return yield* new CliError.UserError({ |
| 243 | cause: "connect", |
| 244 | userMessage: "T3 Connect is unavailable in this build.", |
| 245 | }); |
| 246 | } |
| 247 | |
| 248 | const current = { connect: before.connect === true, lan: before.lan }; |
| 249 | // A publish-only link is not "on", but a rollback must put it back. |
| 250 | const priorPublishOnly = |
| 251 | hasCloudPublicConfig && |
| 252 | (yield* CliState.readCliDesiredCloudLink) && |
| 253 | (yield* CliState.readCliDesiredLinkMode) === "publish_only"; |
| 254 | const plan = planRoutes({ ...before, installed: before.service.installed }, flags); |
| 255 | const interactive = !flags.yes && interactiveTerminal(); |
| 256 | let routes = plan.routes; |
| 257 | if (plan.ask.length > 0 && interactive) { |
| 258 | if (before.service.installed) |
| 259 | yield* Console.log(`Current setup\n${formatSetup(before, hints)}\n`); |
| 260 | const picked = yield* Prompt.run( |
| 261 | Prompt.MultiSelect({ |
| 262 | message: "Allow connections through:", |
| 263 | selectAll: "Select all", |
| 264 | selectNone: "Clear all", |
| 265 | inverseSelection: "Invert selection", |
| 266 | choices: plan.ask.map((route) => ({ |
| 267 | ...CHOICES[route], |
| 268 | value: route, |
| 269 | selected: plan.routes[route], |
| 270 | })), |
| 271 | }), |
| 272 | ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(null) })); |
| 273 | if (picked === null) return yield* Console.log("Nothing changed."); |
| 274 | routes = { connect: picked.includes("connect"), lan: picked.includes("lan") }; |
| 275 | } else if (plan.ask.length > 0 && !flags.yes && !before.service.installed) { |
| 276 | return yield* new CliError.UserError({ |
| 277 | cause: "routes", |
| 278 | userMessage: `No terminal. Choose access with ${ |
| 279 | before.connect === null ? "--lan" : "--connect or --lan" |
| 280 | }, or use --yes for the default. See \`t3 up --help\`.`, |
| 281 | }); |
| 282 | } |
| 283 | if (routes.connect && !current.connect) yield* requireConnectAllowed; |
| 284 | if (routes.lan && !current.lan) { |
| 285 | const policy = yield* ManagedPolicy.ManagedPolicy; |
| 286 | if (!ManagedLimits.allowsRemoteClients(policy)) { |
| 287 | return yield* ManagedLimits.refuseRemoteClients(policy); |
| 288 | } |
| 289 | } |
| 290 | // Nothing to run in the background, but a route left on still gets turned off. |
| 291 | if (!before.service.installed && !routes.connect && !routes.lan) { |
| 292 | if (current.connect || before.connectRevoked) { |
| 293 | yield* disconnectCloud({ clearAuthorization: false }); |
| 294 | } |
| 295 | if (current.lan) yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: "false" }); |
| 296 | return yield* Console.log(formatNextSteps(routes, false, hints)); |
| 297 | } |
| 298 | const { installedVersion } = before.service; |
| 299 | const newer = |
| 300 | installedVersion !== undefined && |
| 301 | compareExactServiceVersions(installedVersion, packageJson.version) > 0; |
| 302 | // Asked before anything changes, so a declined restart leaves service.env |
| 303 | // matching what the running service read when it started. |
| 304 | // A stopped service of this version is not current, so reconcile starts it; |
| 305 | // a newer one is left alone and needs this restart to start. |
| 306 | const stopped = before.service.problems?.includes("service-stopped") === true; |
| 307 | const restart = |
| 308 | before.service.installed && |
| 309 | (before.service.current || newer) && |
| 310 | (needsRestart(current, routes) || stopped); |
| 311 | // An older or broken service is reinstalled, which restarts it too. |
| 312 | const update = before.service.installed && !before.service.current && !newer && !stopped; |
| 313 | if ((restart || update) && interactive && !stopped) { |
| 314 | const confirmed = yield* Prompt.run( |
| 315 | Prompt.Confirm({ |
| 316 | message: update |
| 317 | ? "Update and restart T3 Code? Running agent turns will stop." |
| 318 | : "Apply changes and restart? Running agent turns will stop.", |
| 319 | initial: true, |
| 320 | }), |
| 321 | ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(false) })); |
| 322 | if (!confirmed) return yield* Console.log("Nothing changed."); |
| 323 | } |
| 324 | |
| 325 | if (routes.connect && !current.connect) { |
| 326 | const linked = yield* linkEnvironmentForConnect({ |
| 327 | headless: flags.headless, |
| 328 | installRelayClient: true, |
| 329 | }); |
| 330 | if (linked === null) return; |
| 331 | yield* Console.log( |
| 332 | `✓ Signed in to T3 Connect${linked.identity ? ` as ${linked.identity}` : ""}`, |
| 333 | ); |
| 334 | } |
| 335 | // A running server unlinks live; when that fails, only a restart stops its tunnel. |
| 336 | const unlinkFailed = |
| 337 | !routes.connect && (current.connect || before.connectRevoked) |
| 338 | ? (yield* disconnectCloud({ clearAuthorization: false })).status === "failed" |
| 339 | : false; |
| 340 | const previousLan = (yield* CliService.readServiceEnv(baseDir)).entries.T3CODE_LAN; |
| 341 | // Also with no service manager: the file records the choice that `t3 status` shows. |
| 342 | yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: String(routes.lan) }); |
| 343 | // When the service step fails, an installed service still runs with the old |
| 344 | // value, so the file goes back to match it. |
| 345 | // A new T3 Connect link is undone too, with any tunnel the failed start saved, so a |
| 346 | // rerun sees it as a change and restarts. The sign-in stays. |
| 347 | const restoreServiceEnv = before.service.installed |
| 348 | ? CliService.writeServiceEnv(baseDir, { T3CODE_LAN: previousLan }).pipe( |
| 349 | Effect.andThen( |
| 350 | !routes.connect || current.connect |
| 351 | ? Effect.void |
| 352 | : priorPublishOnly |
| 353 | ? CliState.setCliDesiredCloudLink(true, "publish_only") |
| 354 | : disconnectCloud({ clearAuthorization: false }).pipe(Effect.asVoid), |
| 355 | ), |
| 356 | ) |
| 357 | : Effect.void; |
| 358 | // A failed update or restart may still have started the service with the new |
| 359 | // settings, so restart it once more on the old ones. |
| 360 | const rollBack = before.service.installed |
| 361 | ? restoreServiceEnv.pipe(Effect.andThen(service.restart.pipe(Effect.ignore))) |
| 362 | : Effect.void; |
| 363 | if (before.host !== undefined) { |
| 364 | yield* Console.warn( |
| 365 | `T3CODE_HOST=${before.host} in service.env overrides the network choice. Remove it to apply it.`, |
| 366 | ); |
| 367 | } |
| 368 | |
| 369 | const printServeCommand = Effect.fn(function* (reason: string) { |
| 370 | const { command } = yield* resolveHostCliCommand("serve", [ |
| 371 | ...baseDirArgs, |
| 372 | // Explicit either way, so a T3CODE_LAN in the shell cannot override the choice. |
| 373 | routes.lan ? "--lan" : "--no-lan", |
| 374 | ]); |
| 375 | yield* Console.log(`${reason} Keep this command running:\n ${command}`); |
| 376 | // A host wins over --lan and --no-lan, as it does in service.env. |
| 377 | const shellHost = (yield* HostProcess.Environment).T3CODE_HOST; |
| 378 | if (shellHost !== undefined) { |
| 379 | yield* Console.warn( |
| 380 | `T3CODE_HOST=${shellHost} in this shell overrides that choice. Unset it before you run the command.`, |
| 381 | ); |
| 382 | } |
| 383 | }); |
| 384 | let started = false; |
| 385 | if (!before.service.supported) { |
| 386 | yield* printServeCommand("Background service unavailable."); |
| 387 | } else { |
| 388 | if (newer) { |
| 389 | yield* Console.log(`Keeping installed service version t3@${installedVersion}.`); |
| 390 | } |
| 391 | if (update && !interactive) { |
| 392 | yield* Console.log("Updating T3 Code. Running agent turns will stop."); |
| 393 | } |
| 394 | const result = newer |
| 395 | ? ({ changed: false } as const) |
| 396 | : yield* CliService.reconcileService().pipe( |
| 397 | Effect.catchTags({ |
| 398 | BootServicePrerequisiteError: (error) => Effect.succeed({ failed: error.message }), |
| 399 | BootServiceUnsupportedError: (error) => Effect.succeed({ failed: error.message }), |
| 400 | }), |
| 401 | Effect.tapError(() => rollBack), |
| 402 | ); |
| 403 | if ("failed" in result && before.service.installed) { |
| 404 | // The old service still runs, so a foreground server would clash with it. |
| 405 | yield* restoreServiceEnv; |
| 406 | yield* Console.log( |
| 407 | `Service setup failed: ${result.failed} Fix that, then run \`${hints.up}\` again.`, |
| 408 | ); |
| 409 | } else if ("failed" in result) { |
| 410 | yield* printServeCommand(`Service setup failed: ${result.failed}`); |
| 411 | } else if (result.changed) { |
| 412 | yield* Console.log(CliService.formatServiceChange(result)); |
| 413 | started = true; |
| 414 | } else if (restart || (unlinkFailed && before.service.installed)) { |
| 415 | const confirmed = |
| 416 | restart || |
| 417 | !interactive || |
| 418 | (yield* Prompt.run( |
| 419 | Prompt.Confirm({ |
| 420 | message: |
| 421 | "The running service still has T3 Connect on. Restart it? Running agent turns will stop.", |
| 422 | initial: true, |
| 423 | }), |
| 424 | ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(false) }))); |
| 425 | if (!confirmed) { |
| 426 | const { command } = yield* resolveHostCliCommand("service restart", baseDirArgs); |
| 427 | yield* Console.log(`Run \`${command}\` to turn T3 Connect off.`); |
| 428 | } else { |
| 429 | if (!interactive && !stopped) { |
| 430 | yield* Console.log("Restarting T3 Code. Running agent turns will stop."); |
| 431 | } |
| 432 | const outcome = yield* service.restart.pipe(Effect.tapError(() => rollBack)); |
| 433 | if (outcome === "queued") { |
| 434 | yield* Console.log("Restart queued. This session will disconnect."); |
| 435 | } |
| 436 | if (outcome === "skipped") { |
| 437 | yield* Console.log( |
| 438 | "The installed service was not restarted. Run `t3 service status` to see why.", |
| 439 | ); |
| 440 | } |
| 441 | started = outcome === "restarted"; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | if (started && routes.connect && !current.connect) yield* waitForReachability(); |
| 446 | |
| 447 | const after = yield* readSetup(); |
| 448 | yield* Console.log( |
| 449 | // From what took effect: a failed service step can undo a choice. |
| 450 | `\nThis machine\n${formatSetup(after, hints)}\n\n${formatNextSteps( |
| 451 | { connect: after.connect === true, lan: after.lan }, |
| 452 | after.service.installed, |
| 453 | hints, |
| 454 | )}`, |
| 455 | ); |
| 456 | }); |
| 457 | |
| 458 | export const upCommand = Command.make("up", { |
| 459 | ...projectLocationFlags, |
| 460 | connect: Flag.Boolean("connect").pipe( |
| 461 | Flag.withDescription( |
| 462 | "Allow access through T3 Connect with your T3 account. Turn off with --no-connect.", |
| 463 | ), |
| 464 | Flag.optional, |
| 465 | ), |
| 466 | lan: Flag.Boolean("lan").pipe( |
| 467 | Flag.withDescription( |
| 468 | "Allow LAN and tailnet connections. Anyone there can reach the port; a pairing code is the only lock. Turn off with --no-lan.", |
| 469 | ), |
| 470 | Flag.optional, |
| 471 | ), |
| 472 | yes: Flag.Boolean("yes").pipe( |
| 473 | Flag.withDescription( |
| 474 | "Ask nothing. Keep the access settings; a new machine gets T3 Connect. A restart stops running agent turns.", |
| 475 | ), |
| 476 | Flag.withDefault(false), |
| 477 | ), |
| 478 | headless: headlessFlag, |
| 479 | }).pipe( |
| 480 | Command.withShortDescription("Set up remote access and the T3 Code background service."), |
| 481 | Command.withDescription( |
| 482 | "Set up remote access and the T3 Code background service. Access flags skip the question, and routes not named keep their settings. Without a terminal, a needed restart runs without asking and stops running agent turns.", |
| 483 | ), |
| 484 | Command.withExamples([ |
| 485 | { command: "t3 up", description: "Choose remote access." }, |
| 486 | { command: "t3 up --connect", description: "Turn on T3 Connect. Keep other settings." }, |
| 487 | { command: "t3 up --lan", description: "Allow LAN access with pairing codes." }, |
| 488 | { command: "t3 up --no-lan", description: "Stop LAN access." }, |
| 489 | { command: "t3 up --yes", description: "Take the defaults with no questions." }, |
| 490 | { command: "t3 status --json", description: "Read the settings from a script." }, |
| 491 | ]), |
| 492 | Command.withHandler((flags) => runCloudCommand(flags, setUp(flags), { quietLogs: true })), |
| 493 | ); |
| 494 | |
| 495 | export const connectCommand = Command.make("connect", { |
| 496 | ...projectLocationFlags, |
| 497 | headless: headlessFlag, |
| 498 | }).pipe( |
| 499 | Command.withDescription("Set up T3 Connect for this machine. Same as `t3 up --connect`."), |
| 500 | Command.withHandler((flags) => |
| 501 | runCloudCommand( |
| 502 | flags, |
| 503 | setUp({ ...flags, connect: Option.some(true), lan: Option.none(), yes: false }), |
| 504 | { quietLogs: true }, |
| 505 | ), |
| 506 | ), |
| 507 | Command.withSubcommands(connectSubcommands), |
| 508 | ); |
| 509 | |
| 510 | /** `t3 status --json`, for scripts and agents. */ |
| 511 | const setupJson = (setup: Setup) => |
| 512 | JSON.stringify( |
| 513 | { |
| 514 | service: { |
| 515 | supported: setup.service.supported, |
| 516 | installed: setup.service.installed, |
| 517 | installedVersion: setup.service.installedVersion ?? null, |
| 518 | otherBaseDir: setup.service.otherBaseDir ?? null, |
| 519 | }, |
| 520 | connect: |
| 521 | setup.connect === null ? null : { enabled: setup.connect, revoked: setup.connectRevoked }, |
| 522 | lan: { |
| 523 | enabled: setup.lan, |
| 524 | inheritedDefault: setup.lanFromOldDefault, |
| 525 | hostOverride: setup.host ?? null, |
| 526 | }, |
| 527 | }, |
| 528 | null, |
| 529 | 2, |
| 530 | ); |
| 531 | |
| 532 | export const statusCommand = Command.make("status", { |
| 533 | ...projectLocationFlags, |
| 534 | json: Flag.Boolean("json").pipe( |
| 535 | Flag.withDescription("Print the settings as JSON. Does not test connections."), |
| 536 | Flag.withDefault(false), |
| 537 | ), |
| 538 | }).pipe( |
| 539 | Command.withDescription("Show service setup and remote access settings."), |
| 540 | Command.withExamples([ |
| 541 | { command: "t3 status", description: "Show the settings." }, |
| 542 | { command: "t3 status --json", description: "Print them for scripts and agents." }, |
| 543 | ]), |
| 544 | Command.withHandler( |
| 545 | Effect.fn("cli.status")(function* (flags) { |
| 546 | const config = yield* resolveCliAuthConfig(flags, yield* GlobalFlag.LogLevel, { |
| 547 | quietLogs: flags.json, |
| 548 | }); |
| 549 | // Only what readSetup reads: no database, unlike the T3 Connect commands. |
| 550 | const setup = yield* readSetup().pipe( |
| 551 | Effect.provide( |
| 552 | Layer.merge(CliService.layer(config), ServerSecretStore.layer).pipe( |
| 553 | Layer.provideMerge(ServerConfig.layer(config)), |
| 554 | ), |
| 555 | ), |
| 556 | ); |
| 557 | if (flags.json) return yield* Console.log(setupJson(setup)); |
| 558 | const hints = yield* resolveHints(flags.baseDir); |
| 559 | yield* Console.log( |
| 560 | setup.service.otherBaseDir !== undefined |
| 561 | ? `This machine\n${formatSetup(setup, hints)}` |
| 562 | : `This machine\n${formatSetup(setup, hints)}\n\nChange access: \`${hints.up}\`.`, |
| 563 | ); |
| 564 | }), |
| 565 | ), |
| 566 | ); |