Add edit command to open session files in $EDITOR
This commit is contained in:
parent
b46511efe3
commit
ec1f6796b8
|
|
@ -18,6 +18,7 @@ import { action as diffAction } from "./commands/diff.ts"
|
||||||
import { action as showAction } from "./commands/show.ts"
|
import { action as showAction } from "./commands/show.ts"
|
||||||
import { action as logAction } from "./commands/log.ts"
|
import { action as logAction } from "./commands/log.ts"
|
||||||
import { action as dirAction } from "./commands/dir.ts"
|
import { action as dirAction } from "./commands/dir.ts"
|
||||||
|
import { action as editAction } from "./commands/edit.ts"
|
||||||
import { action as cleanupAction } from "./commands/cleanup.ts"
|
import { action as cleanupAction } from "./commands/cleanup.ts"
|
||||||
import { register as registerVmCommands } from "./commands/vm.ts"
|
import { register as registerVmCommands } from "./commands/vm.ts"
|
||||||
import { action as completionsAction } from "./commands/completions.ts"
|
import { action as completionsAction } from "./commands/completions.ts"
|
||||||
|
|
@ -139,6 +140,13 @@ program
|
||||||
.description("Open a shell in the VM")
|
.description("Open a shell in the VM")
|
||||||
.action(shellAction)
|
.action(shellAction)
|
||||||
|
|
||||||
|
program
|
||||||
|
.command("edit")
|
||||||
|
.argument("<branch>", "branch name")
|
||||||
|
.argument("<file>", "file path relative to worktree root")
|
||||||
|
.description("Open a file from a session in $EDITOR")
|
||||||
|
.action(editAction)
|
||||||
|
|
||||||
program
|
program
|
||||||
.command("dir")
|
.command("dir")
|
||||||
.argument("<branch>", "branch name")
|
.argument("<branch>", "branch name")
|
||||||
|
|
|
||||||
35
src/commands/edit.ts
Normal file
35
src/commands/edit.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
import { resolve } from "path"
|
||||||
|
import { existsSync } from "fs"
|
||||||
|
import { die } from "../fmt.ts"
|
||||||
|
import { requireSession } from "./helpers.ts"
|
||||||
|
|
||||||
|
export async function action(branch: string, file: string) {
|
||||||
|
const editor = process.env.EDITOR
|
||||||
|
if (!editor) {
|
||||||
|
die("$EDITOR is not set.")
|
||||||
|
}
|
||||||
|
|
||||||
|
const { session } = await requireSession(branch)
|
||||||
|
const worktree = resolve(session.worktree)
|
||||||
|
const path = resolve(worktree, file)
|
||||||
|
|
||||||
|
if (!path.startsWith(worktree + "/") && path !== worktree) {
|
||||||
|
die("File path escapes the worktree.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!existsSync(path)) {
|
||||||
|
die(`File not found: ${file}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const [cmd, ...args] = editor.split(/\s+/)
|
||||||
|
const proc = Bun.spawn([cmd, ...args, path], {
|
||||||
|
stdin: "inherit",
|
||||||
|
stdout: "inherit",
|
||||||
|
stderr: "inherit",
|
||||||
|
})
|
||||||
|
|
||||||
|
const exitCode = await proc.exited
|
||||||
|
if (exitCode !== 0) {
|
||||||
|
die(`Editor exited with code ${exitCode}.`)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user