refactor(cli): rename ps to ls and add top-level agent commands
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import type { Command } from 'commander'
|
||||
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
|
||||
import type { CommandOptions, SingleResult, OutputSchema, CommandError } from '../../output/index.js'
|
||||
|
||||
/** Result type for worktree archive command */
|
||||
export interface WorktreeArchiveResult {
|
||||
name: string
|
||||
status: 'archived'
|
||||
removedAgents: string[]
|
||||
}
|
||||
|
||||
/** Schema for archive command output */
|
||||
export const archiveSchema: OutputSchema<WorktreeArchiveResult> = {
|
||||
idField: 'name',
|
||||
columns: [
|
||||
{ header: 'NAME', field: 'name' },
|
||||
{ header: 'STATUS', field: 'status' },
|
||||
{
|
||||
header: 'REMOVED AGENTS',
|
||||
field: (item) => item.removedAgents.length > 0 ? item.removedAgents.join(', ') : '-',
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
export interface WorktreeArchiveOptions extends CommandOptions {
|
||||
host?: string
|
||||
}
|
||||
|
||||
export type WorktreeArchiveCommandResult = SingleResult<WorktreeArchiveResult>
|
||||
|
||||
export async function runArchiveCommand(
|
||||
nameArg: string,
|
||||
options: WorktreeArchiveOptions,
|
||||
_command: Command
|
||||
): Promise<WorktreeArchiveCommandResult> {
|
||||
const host = getDaemonHost({ host: options.host })
|
||||
|
||||
// Validate arguments
|
||||
if (!nameArg || nameArg.trim().length === 0) {
|
||||
const error: CommandError = {
|
||||
code: 'MISSING_WORKTREE_NAME',
|
||||
message: 'Worktree name is required',
|
||||
details: 'Usage: paseo worktree archive <name>',
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
let client
|
||||
try {
|
||||
client = await connectToDaemon({ host: options.host })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const error: CommandError = {
|
||||
code: 'DAEMON_NOT_RUNNING',
|
||||
message: `Cannot connect to daemon at ${host}: ${message}`,
|
||||
details: 'Start the daemon with: paseo daemon start',
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
try {
|
||||
// Get the list of worktrees first to resolve the name
|
||||
const listResponse = await client.getPaseoWorktreeList({})
|
||||
|
||||
if (listResponse.error) {
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_LIST_FAILED',
|
||||
message: `Failed to list worktrees: ${listResponse.error.message}`,
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
// Find the worktree by name or branch
|
||||
const worktree = listResponse.worktrees.find((wt) => {
|
||||
const name = wt.worktreePath.split('/').pop()
|
||||
return name === nameArg || wt.branchName === nameArg
|
||||
})
|
||||
|
||||
if (!worktree) {
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_NOT_FOUND',
|
||||
message: `Worktree not found: ${nameArg}`,
|
||||
details: 'Use "paseo worktree ls" to list available worktrees',
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
// Archive the worktree
|
||||
const response = await client.archivePaseoWorktree({
|
||||
worktreePath: worktree.worktreePath,
|
||||
})
|
||||
|
||||
await client.close()
|
||||
|
||||
if (response.error) {
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_ARCHIVE_FAILED',
|
||||
message: `Failed to archive worktree: ${response.error.message}`,
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
const worktreeName = worktree.worktreePath.split('/').pop() ?? nameArg
|
||||
|
||||
return {
|
||||
type: 'single',
|
||||
data: {
|
||||
name: worktreeName,
|
||||
status: 'archived',
|
||||
removedAgents: response.removedAgents ?? [],
|
||||
},
|
||||
schema: archiveSchema,
|
||||
}
|
||||
} catch (err) {
|
||||
await client.close().catch(() => {})
|
||||
|
||||
// Re-throw CommandError as-is
|
||||
if (err && typeof err === 'object' && 'code' in err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_ARCHIVE_FAILED',
|
||||
message: `Failed to archive worktree: ${message}`,
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Command } from 'commander'
|
||||
import { runLsCommand } from './ls.js'
|
||||
import { runArchiveCommand } from './archive.js'
|
||||
import { withOutput } from '../../output/index.js'
|
||||
|
||||
export function createWorktreeCommand(): Command {
|
||||
const worktree = new Command('worktree').description('Manage Paseo-managed git worktrees')
|
||||
|
||||
worktree
|
||||
.command('ls')
|
||||
.description('List Paseo-managed git worktrees')
|
||||
.option('--json', 'Output in JSON format')
|
||||
.option('--host <host>', 'Daemon host:port (default: localhost:6767)')
|
||||
.action((options, command) => {
|
||||
if (options.json) {
|
||||
command.parent.parent.opts().format = 'json'
|
||||
}
|
||||
return withOutput(runLsCommand)(options, command)
|
||||
})
|
||||
|
||||
worktree
|
||||
.command('archive')
|
||||
.description('Archive a worktree (removes worktree and associated branch)')
|
||||
.argument('<name>', 'Worktree name or branch name')
|
||||
.option('--host <host>', 'Daemon host:port (default: localhost:6767)')
|
||||
.action(withOutput(runArchiveCommand))
|
||||
|
||||
return worktree
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import type { Command } from 'commander'
|
||||
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
|
||||
import type { CommandOptions, ListResult, OutputSchema, CommandError } from '../../output/index.js'
|
||||
|
||||
/** Worktree list item for display */
|
||||
export interface WorktreeListItem {
|
||||
name: string
|
||||
branch: string
|
||||
cwd: string
|
||||
agent: string
|
||||
}
|
||||
|
||||
/** Shorten home directory in path */
|
||||
function shortenPath(path: string): string {
|
||||
const home = process.env.HOME
|
||||
if (home && path.startsWith(home)) {
|
||||
return '~' + path.slice(home.length)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
/** Extract worktree name from path */
|
||||
function extractWorktreeName(path: string): string {
|
||||
// ~/.paseo/worktrees/<repo>/<name> -> name
|
||||
const parts = path.split('/')
|
||||
return parts[parts.length - 1] ?? path
|
||||
}
|
||||
|
||||
/** Schema for worktree ls output */
|
||||
export const worktreeLsSchema: OutputSchema<WorktreeListItem> = {
|
||||
idField: 'name',
|
||||
columns: [
|
||||
{ header: 'NAME', field: 'name', width: 20 },
|
||||
{ header: 'BRANCH', field: 'branch', width: 25 },
|
||||
{ header: 'CWD', field: 'cwd', width: 45 },
|
||||
{ header: 'AGENT', field: 'agent', width: 10 },
|
||||
],
|
||||
}
|
||||
|
||||
export type WorktreeLsResult = ListResult<WorktreeListItem>
|
||||
|
||||
export interface WorktreeLsOptions extends CommandOptions {
|
||||
host?: string
|
||||
}
|
||||
|
||||
export async function runLsCommand(
|
||||
options: WorktreeLsOptions,
|
||||
_command: Command
|
||||
): Promise<WorktreeLsResult> {
|
||||
const host = getDaemonHost({ host: options.host })
|
||||
|
||||
let client
|
||||
try {
|
||||
client = await connectToDaemon({ host: options.host })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const error: CommandError = {
|
||||
code: 'DAEMON_NOT_RUNNING',
|
||||
message: `Cannot connect to daemon at ${host}: ${message}`,
|
||||
details: 'Start the daemon with: paseo daemon start',
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
try {
|
||||
// Request session state to get agent information
|
||||
client.requestSessionState()
|
||||
|
||||
// Wait a moment for the session state to be populated
|
||||
await new Promise((resolve) => setTimeout(resolve, 500))
|
||||
|
||||
const agents = client.listAgents()
|
||||
|
||||
// Get worktree list from daemon
|
||||
const response = await client.getPaseoWorktreeList({})
|
||||
|
||||
await client.close()
|
||||
|
||||
if (response.error) {
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_LIST_FAILED',
|
||||
message: `Failed to list worktrees: ${response.error.message}`,
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
// Build a map of worktree paths to agent IDs
|
||||
const worktreeAgentMap = new Map<string, string>()
|
||||
for (const agent of agents) {
|
||||
// Check if agent cwd is under ~/.paseo/worktrees/
|
||||
const paseoHome = process.env.PASEO_HOME ?? (process.env.HOME + '/.paseo')
|
||||
const worktreesDir = paseoHome + '/worktrees/'
|
||||
if (agent.cwd.startsWith(worktreesDir)) {
|
||||
worktreeAgentMap.set(agent.cwd, agent.id.slice(0, 7))
|
||||
}
|
||||
}
|
||||
|
||||
const items: WorktreeListItem[] = response.worktrees.map((wt) => ({
|
||||
name: extractWorktreeName(wt.worktreePath),
|
||||
branch: wt.branchName ?? '-',
|
||||
cwd: shortenPath(wt.worktreePath),
|
||||
agent: worktreeAgentMap.get(wt.worktreePath) ?? '-',
|
||||
}))
|
||||
|
||||
return {
|
||||
type: 'list',
|
||||
data: items,
|
||||
schema: worktreeLsSchema,
|
||||
}
|
||||
} catch (err) {
|
||||
await client.close().catch(() => {})
|
||||
|
||||
// Re-throw CommandError as-is
|
||||
if (err && typeof err === 'object' && 'code' in err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const error: CommandError = {
|
||||
code: 'WORKTREE_LIST_FAILED',
|
||||
message: `Failed to list worktrees: ${message}`,
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user