use std::path::Path;
use crate::config::spec_dir;
use crate::error::Result;
use crate::output::{print_header, GREEN, RESET};
use crate::self_test::{
cleanup_self_test, create_self_test_spec, print_cleanup_results, print_failure_details,
SELF_TEST_SPEC_FILENAME,
};
use crate::Runner;
use super::{detect_input_type, ensure_project_dir, InputType};
pub fn run_command(
verbose: bool,
spec: &Path,
skip_review: bool,
worktree: bool,
no_worktree: bool,
self_test: bool,
) -> Result<()> {
ensure_project_dir()?;
if self_test {
return run_self_test(verbose, skip_review, worktree, no_worktree);
}
let mut runner = Runner::new()?
.with_verbose(verbose)
.with_skip_review(skip_review);
if worktree {
runner = runner.with_worktree(true);
} else if no_worktree {
runner = runner.with_worktree(false);
}
print_header();
match detect_input_type(spec) {
InputType::Json => runner.run(spec),
InputType::Markdown => runner.run_from_spec(spec),
}
}
fn run_self_test(
verbose: bool,
skip_review: bool,
worktree: bool,
no_worktree: bool,
) -> Result<()> {
let spec = create_self_test_spec();
let spec_path = spec_dir()?.join(SELF_TEST_SPEC_FILENAME);
spec.save(&spec_path)?;
let mut runner = Runner::new()?
.with_verbose(verbose)
.with_skip_review(skip_review)
.with_commit(false)
.with_pull_request(false);
if worktree {
runner = runner.with_worktree(true);
} else if no_worktree {
runner = runner.with_worktree(false);
}
print_header();
let run_result = runner.run(&spec_path);
if let Err(ref e) = run_result {
print_failure_details(e);
}
let cleanup_result = cleanup_self_test();
print_cleanup_results(&cleanup_result);
run_result
}
pub fn run_with_file(runner: &Runner, file: &Path) -> Result<()> {
ensure_project_dir()?;
let move_result = crate::config::move_to_config_dir(file)?;
print_header();
if move_result.was_moved {
println!(
"{GREEN}Moved{RESET} {} → {}",
file.display(),
move_result.dest_path.display()
);
println!();
}
match detect_input_type(&move_result.dest_path) {
InputType::Json => runner.run(&move_result.dest_path),
InputType::Markdown => runner.run_from_spec(&move_result.dest_path),
}
}