| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import { cmd } from "../cmd"
- import { UI } from "@/cli/ui"
- import { tui } from "./app"
- import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32"
- export const AttachCommand = cmd({
- command: "attach <url>",
- describe: "attach to a running opencode server",
- builder: (yargs) =>
- yargs
- .positional("url", {
- type: "string",
- describe: "http://localhost:4096",
- demandOption: true,
- })
- .option("dir", {
- type: "string",
- description: "directory to run in",
- })
- .option("continue", {
- alias: ["c"],
- describe: "continue the last session",
- type: "boolean",
- })
- .option("session", {
- alias: ["s"],
- type: "string",
- describe: "session id to continue",
- })
- .option("fork", {
- type: "boolean",
- describe: "fork the session when continuing (use with --continue or --session)",
- })
- .option("password", {
- alias: ["p"],
- type: "string",
- describe: "basic auth password (defaults to OPENCODE_SERVER_PASSWORD)",
- }),
- handler: async (args) => {
- const unguard = win32InstallCtrlCGuard()
- try {
- win32DisableProcessedInput()
- if (args.fork && !args.continue && !args.session) {
- UI.error("--fork requires --continue or --session")
- process.exitCode = 1
- return
- }
- const directory = (() => {
- if (!args.dir) return undefined
- try {
- process.chdir(args.dir)
- return process.cwd()
- } catch {
- // If the directory doesn't exist locally (remote attach), pass it through.
- return args.dir
- }
- })()
- const headers = (() => {
- const password = args.password ?? process.env.OPENCODE_SERVER_PASSWORD
- if (!password) return undefined
- const auth = `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}`
- return { Authorization: auth }
- })()
- await tui({
- url: args.url,
- args: {
- continue: args.continue,
- sessionID: args.session,
- fork: args.fork,
- },
- directory,
- headers,
- })
- } finally {
- unguard?.()
- }
- },
- })
|