From 97ff33a4b012550a5dd75464f8126b802b6ff522 Mon Sep 17 00:00:00 2001 From: onlyyu1996 <1158673577@qq.com> Date: Mon, 18 May 2026 14:52:45 +0800 Subject: [PATCH] fix(plugin): emit json for list filter conflict --- src/cortex-cli/src/agent_cmd/tests.rs | 5 ++--- src/cortex-cli/src/plugin_cmd.rs | 31 ++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/cortex-cli/src/agent_cmd/tests.rs b/src/cortex-cli/src/agent_cmd/tests.rs index e2ff07f9f..18f7ba753 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() { diff --git a/src/cortex-cli/src/plugin_cmd.rs b/src/cortex-cli/src/plugin_cmd.rs index 3ce99f238..0925b3e01 100644 --- a/src/cortex-cli/src/plugin_cmd.rs +++ b/src/cortex-cli/src/plugin_cmd.rs @@ -21,6 +21,8 @@ use std::process::{Command, Stdio}; use std::sync::mpsc; use std::time::Duration; +const PLUGIN_LIST_FILTER_CONFLICT: &str = "Cannot specify both --enabled and --disabled. Choose one filter or use neither for all plugins."; + // ============================================================================= // Plugin SDK Templates (embedded for standalone CLI operation) // ============================================================================= @@ -959,9 +961,14 @@ impl PluginCli { async fn run_list(args: PluginListArgs) -> Result<()> { // Validate mutually exclusive flags if args.enabled && args.disabled { - bail!( - "Cannot specify both --enabled and --disabled. Choose one filter or use neither for all plugins." - ); + if args.json { + println!( + "{}", + serde_json::to_string_pretty(&plugin_list_filter_conflict_error())? + ); + std::process::exit(1); + } + bail!(PLUGIN_LIST_FILTER_CONFLICT); } let plugins_dir = get_plugins_dir(); @@ -1062,6 +1069,12 @@ async fn run_list(args: PluginListArgs) -> Result<()> { Ok(()) } +fn plugin_list_filter_conflict_error() -> serde_json::Value { + serde_json::json!({ + "error": PLUGIN_LIST_FILTER_CONFLICT + }) +} + async fn run_install(args: PluginInstallArgs) -> Result<()> { // Validate plugin name is not empty (Issue #3700) if args.name.trim().is_empty() { @@ -2370,6 +2383,18 @@ mod tests { assert!(args.disabled, "disabled filter should be true when set"); } + #[test] + fn test_plugin_list_filter_conflict_error_serializes_as_json() { + let error = plugin_list_filter_conflict_error(); + let json = serde_json::to_string(&error).expect("should serialize conflict error"); + + assert!(json.contains("error"), "JSON should contain error field"); + assert!( + json.contains(PLUGIN_LIST_FILTER_CONFLICT), + "JSON should contain conflict message" + ); + } + // ========================================================================== // CLI argument parsing tests - PluginInstallArgs // ==========================================================================