Skip to content

Commit f137254

Browse files
committed
fix(cli): emit json for missing agent show
1 parent 7954d02 commit f137254

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/cortex-cli/src/agent_cmd/handlers/show.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
//! Handler for the `agent show` command.
22
3-
use anyhow::Result;
3+
use anyhow::{Result, bail};
44

55
use crate::agent_cmd::cli::ShowArgs;
66
use crate::agent_cmd::loader::load_all_agents;
77
use crate::agent_cmd::utils::format_color_preview;
88

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+
919
/// Show agent details command.
1020
pub async fn run_show(args: ShowArgs) -> Result<()> {
1121
let agents = load_all_agents()?;
1222

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+
};
1736

1837
// Warn if the agent is hidden
1938
if agent.hidden {
@@ -153,3 +172,16 @@ pub async fn run_show(args: ShowArgs) -> Result<()> {
153172

154173
Ok(())
155174
}
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+
}

src/cortex-cli/src/agent_cmd/tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
#[cfg(test)]
44
mod tests {
55
use crate::agent_cmd::cli::{CopyArgs, ExportArgs};
6-
use crate::agent_cmd::loader::{
7-
load_builtin_agents, parse_frontmatter, read_file_with_encoding,
8-
};
6+
use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter};
97
use crate::agent_cmd::types::AgentMode;
8+
use crate::utils::file::read_file_with_encoding;
109

1110
#[test]
1211
fn test_read_file_with_utf8() {

0 commit comments

Comments
 (0)