From 78b28d56ea28cd44631cfbc7866262ede0df9412 Mon Sep 17 00:00:00 2001 From: OnlyYu1996 <1158673577@qq.com> Date: Sun, 17 May 2026 21:07:08 +0800 Subject: [PATCH] fix(cli): add dry run for agent copy --- src/cortex-cli/src/agent_cmd/cli.rs | 4 ++ src/cortex-cli/src/agent_cmd/handlers/copy.rs | 37 +++++++++++++++++-- src/cortex-cli/src/agent_cmd/tests.rs | 19 ++++++++-- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/cortex-cli/src/agent_cmd/cli.rs b/src/cortex-cli/src/agent_cmd/cli.rs index c60b1a3ad..2500a80bc 100644 --- a/src/cortex-cli/src/agent_cmd/cli.rs +++ b/src/cortex-cli/src/agent_cmd/cli.rs @@ -184,6 +184,10 @@ pub struct CopyArgs { /// Force overwrite if destination agent already exists. #[arg(short, long)] pub force: bool, + + /// Show what would be copied without writing the destination file. + #[arg(long = "dry-run")] + pub dry_run: bool, } /// Arguments for export command. diff --git a/src/cortex-cli/src/agent_cmd/handlers/copy.rs b/src/cortex-cli/src/agent_cmd/handlers/copy.rs index 2611db11e..4275580a3 100644 --- a/src/cortex-cli/src/agent_cmd/handlers/copy.rs +++ b/src/cortex-cli/src/agent_cmd/handlers/copy.rs @@ -31,7 +31,7 @@ pub async fn run_copy(args: CopyArgs) -> Result<()> { // Check if destination already exists let dest_exists = agents.iter().any(|a| a.name == args.destination); - if dest_exists && !args.force { + if dest_exists && !args.force && !args.dry_run { bail!( "Agent '{}' already exists. Use --force to overwrite.", args.destination @@ -40,10 +40,41 @@ pub async fn run_copy(args: CopyArgs) -> Result<()> { // Get the agents directory let agents_dir = get_agents_dir()?; - std::fs::create_dir_all(&agents_dir)?; - let dest_file = agents_dir.join(format!("{}.md", args.destination)); + if args.dry_run { + println!( + "Would copy agent '{}' to '{}'", + args.source, args.destination + ); + println!( + " Source: {}", + source_agent + .path + .as_ref() + .map(|path| path.display().to_string()) + .unwrap_or_else(|| "builtin".to_string()) + ); + println!(" Destination: {}", dest_file.display()); + println!( + " Destination exists: {}", + if dest_exists { "yes" } else { "no" } + ); + println!( + " Force required: {}", + if dest_exists && !args.force { + "yes" + } else { + "no" + } + ); + println!(); + println!("No files written (dry run)."); + return Ok(()); + } + + std::fs::create_dir_all(&agents_dir)?; + // Generate the agent content let content = if source_agent.native { // For built-in agents, create a new file from scratch diff --git a/src/cortex-cli/src/agent_cmd/tests.rs b/src/cortex-cli/src/agent_cmd/tests.rs index e2ff07f9f..c613a307e 100644 --- a/src/cortex-cli/src/agent_cmd/tests.rs +++ b/src/cortex-cli/src/agent_cmd/tests.rs @@ -3,10 +3,9 @@ #[cfg(test)] mod tests { use crate::agent_cmd::cli::{CopyArgs, ExportArgs}; - use crate::agent_cmd::loader::{ - load_builtin_agents, parse_frontmatter, read_file_with_encoding, - }; + use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter}; use crate::agent_cmd::types::AgentMode; + use crate::utils::file::read_file_with_encoding; #[test] fn test_read_file_with_utf8() { @@ -89,9 +88,23 @@ mod tests { source: "build".to_string(), destination: "my-build".to_string(), force: false, + dry_run: false, }; assert_eq!(args.source, "build"); assert_eq!(args.destination, "my-build"); + assert!(!args.dry_run); + } + + #[test] + fn test_copy_args_dry_run() { + let args = CopyArgs { + source: "build".to_string(), + destination: "my-build".to_string(), + force: false, + dry_run: true, + }; + + assert!(args.dry_run); } #[test]