attach.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { cmd } from "../cmd"
  2. import { tui } from "./app"
  3. export const AttachCommand = cmd({
  4. command: "attach <url>",
  5. describe: "attach to a running opencode server",
  6. builder: (yargs) =>
  7. yargs
  8. .positional("url", {
  9. type: "string",
  10. describe: "http://localhost:4096",
  11. demandOption: true,
  12. })
  13. .option("dir", {
  14. type: "string",
  15. description: "directory to run in",
  16. })
  17. .option("session", {
  18. alias: ["s"],
  19. type: "string",
  20. describe: "session id to continue",
  21. })
  22. .option("password", {
  23. alias: ["p"],
  24. type: "string",
  25. describe: "basic auth password (defaults to OPENCODE_SERVER_PASSWORD)",
  26. }),
  27. handler: async (args) => {
  28. const directory = (() => {
  29. if (!args.dir) return undefined
  30. try {
  31. process.chdir(args.dir)
  32. return process.cwd()
  33. } catch {
  34. // If the directory doesn't exist locally (remote attach), pass it through.
  35. return args.dir
  36. }
  37. })()
  38. const headers = (() => {
  39. const password = args.password ?? process.env.OPENCODE_SERVER_PASSWORD
  40. if (!password) return undefined
  41. const auth = `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}`
  42. return { Authorization: auth }
  43. })()
  44. await tui({
  45. url: args.url,
  46. args: { sessionID: args.session },
  47. directory,
  48. headers,
  49. })
  50. },
  51. })