use crate::cli::commands;
use crate::cli::error::{CliResult, ErrorFactory};
use crate::commands::{
run_worktree_watch_start, run_worktree_watch_status, run_worktree_watch_stop,
run_worktree_watch_sync, WorktreeWatchStartOptions, WorktreeWatchStatusOptions,
WorktreeWatchStopOptions, WorktreeWatchSyncOptions, WorktreeWatchTargetOptions,
};
pub async fn handle_worktree_watch(cmd: commands::WorktreeWatchCmd, _debug: bool) -> CliResult<()> {
let result = match cmd.command {
commands::WorktreeWatchSubCommand::Start(start_cmd) => {
run_worktree_watch_start(WorktreeWatchStartOptions {
target: WorktreeWatchTargetOptions {
repo: start_cmd.target.repo,
parent: start_cmd.target.parent,
},
detach: start_cmd.detach,
sync_interval_seconds: start_cmd.sync_interval_seconds,
once: start_cmd.once,
})
.await
}
commands::WorktreeWatchSubCommand::Stop(stop_cmd) => {
run_worktree_watch_stop(WorktreeWatchStopOptions {
target: WorktreeWatchTargetOptions {
repo: stop_cmd.target.repo,
parent: stop_cmd.target.parent,
},
force: stop_cmd.force,
})
}
commands::WorktreeWatchSubCommand::Sync(sync_cmd) => {
run_worktree_watch_sync(WorktreeWatchSyncOptions {
target: WorktreeWatchTargetOptions {
repo: sync_cmd.target.repo,
parent: sync_cmd.target.parent,
},
dry_run: sync_cmd.dry_run,
resync: sync_cmd.resync,
})
.await
}
commands::WorktreeWatchSubCommand::Status(status_cmd) => {
run_worktree_watch_status(WorktreeWatchStatusOptions {
target: WorktreeWatchTargetOptions {
repo: status_cmd.target.repo,
parent: status_cmd.target.parent,
},
json: status_cmd.json,
records: status_cmd.records,
record_limit: status_cmd.record_limit,
stats: status_cmd.stats,
stats_gap_minutes: status_cmd.stats_gap_minutes,
})
.await
}
};
result.map_err(|error| {
ErrorFactory::operation(
"worktree-watch",
"process worktree mutations",
error,
Some(
"Run inside a git repository, pass `--repo <path>`, or pass `--parent <folder>`. Use `xbp login` before syncing.",
),
)
})
}