|
1 | 1 | //! Handler for the `agent show` command. |
2 | 2 |
|
3 | | -use anyhow::Result; |
| 3 | +use anyhow::{Result, bail}; |
4 | 4 |
|
5 | 5 | use crate::agent_cmd::cli::ShowArgs; |
6 | 6 | use crate::agent_cmd::loader::load_all_agents; |
7 | 7 | use crate::agent_cmd::utils::format_color_preview; |
8 | 8 |
|
| 9 | +fn agent_not_found_message(name: &str) -> String { |
| 10 | + format!("Agent '{}' not found", name) |
| 11 | +} |
| 12 | + |
| 13 | +fn agent_not_found_json(name: &str) -> serde_json::Value { |
| 14 | + serde_json::json!({ |
| 15 | + "error": agent_not_found_message(name) |
| 16 | + }) |
| 17 | +} |
| 18 | + |
9 | 19 | /// Show agent details command. |
10 | 20 | pub async fn run_show(args: ShowArgs) -> Result<()> { |
11 | 21 | let agents = load_all_agents()?; |
12 | 22 |
|
13 | | - let agent = agents |
14 | | - .iter() |
15 | | - .find(|a| a.name == args.name) |
16 | | - .ok_or_else(|| anyhow::anyhow!("Agent '{}' not found", args.name))?; |
| 23 | + let agent = match agents.iter().find(|a| a.name == args.name) { |
| 24 | + Some(agent) => agent, |
| 25 | + None => { |
| 26 | + let error_message = agent_not_found_message(&args.name); |
| 27 | + if args.json { |
| 28 | + println!( |
| 29 | + "{}", |
| 30 | + serde_json::to_string_pretty(&agent_not_found_json(&args.name))? |
| 31 | + ); |
| 32 | + } |
| 33 | + bail!("{}", error_message); |
| 34 | + } |
| 35 | + }; |
17 | 36 |
|
18 | 37 | // Warn if the agent is hidden |
19 | 38 | if agent.hidden { |
@@ -153,3 +172,16 @@ pub async fn run_show(args: ShowArgs) -> Result<()> { |
153 | 172 |
|
154 | 173 | Ok(()) |
155 | 174 | } |
| 175 | + |
| 176 | +#[cfg(test)] |
| 177 | +mod tests { |
| 178 | + use super::*; |
| 179 | + |
| 180 | + #[test] |
| 181 | + fn test_agent_not_found_json_error() { |
| 182 | + let error = agent_not_found_json("nonexistent"); |
| 183 | + |
| 184 | + assert_eq!(error["error"], "Agent 'nonexistent' not found"); |
| 185 | + serde_json::to_string(&error).expect("error should serialize as JSON"); |
| 186 | + } |
| 187 | +} |
0 commit comments