Skip to content

Commit df8c5aa

Browse files
committed
fix(cli): add verbose plugin enable diagnostics
1 parent 7954d02 commit df8c5aa

4 files changed

Lines changed: 65 additions & 7 deletions

File tree

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() {

src/cortex-cli/src/cli/args.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,15 @@ mod tests {
988988
assert!(cli.verbose);
989989
}
990990

991+
#[test]
992+
fn test_cli_global_verbose_with_plugin_enable() {
993+
let cli = Cli::try_parse_from(["cortex", "--verbose", "plugin", "enable", "sample-one"])
994+
.expect("should parse global --verbose with plugin enable");
995+
996+
assert!(cli.verbose);
997+
assert!(matches!(cli.command, Some(Commands::Plugin(_))));
998+
}
999+
9911000
#[test]
9921001
fn test_cli_verbose_short_flag() {
9931002
let cli = Cli::try_parse_from(["cortex", "-v"]).expect("should parse -v");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub async fn dispatch_command(cli: Cli) -> Result<()> {
5858
Some(Commands::Debug(debug_cli)) => debug_cli.run().await,
5959
Some(Commands::Servers(servers_cli)) => run_servers(servers_cli).await,
6060
Some(Commands::History(history_cli)) => run_history(history_cli).await,
61-
Some(Commands::Plugin(plugin_cli)) => plugin_cli.run().await,
61+
Some(Commands::Plugin(plugin_cli)) => plugin_cli.run(cli.verbose).await,
6262
Some(Commands::Feedback(feedback_cli)) => feedback_cli.run().await,
6363
Some(Commands::Lock(lock_cli)) => lock_cli.run().await,
6464
Some(Commands::Alias(alias_cli)) => alias_cli.run().await,

src/cortex-cli/src/plugin_cmd.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,12 +939,12 @@ cp plugin.toml ~/.cortex/plugins/{}/
939939

940940
impl PluginCli {
941941
/// Run the plugin command.
942-
pub async fn run(self) -> Result<()> {
942+
pub async fn run(self, verbose: bool) -> Result<()> {
943943
match self.subcommand {
944944
PluginSubcommand::List(args) => run_list(args).await,
945945
PluginSubcommand::Install(args) => run_install(args).await,
946946
PluginSubcommand::Remove(args) => run_remove(args).await,
947-
PluginSubcommand::Enable(args) => run_enable(args).await,
947+
PluginSubcommand::Enable(args) => run_enable(args, verbose).await,
948948
PluginSubcommand::Disable(args) => run_disable(args).await,
949949
PluginSubcommand::Show(args) => run_show(args).await,
950950
PluginSubcommand::New(args) => run_new(args).await,
@@ -1169,7 +1169,30 @@ async fn run_remove(args: PluginRemoveArgs) -> Result<()> {
11691169
Ok(())
11701170
}
11711171

1172-
async fn run_enable(args: PluginEnableArgs) -> Result<()> {
1172+
fn plugin_enabled_value(manifest: &toml::Value) -> Option<bool> {
1173+
manifest.get("enabled").and_then(|value| value.as_bool())
1174+
}
1175+
1176+
fn format_enable_verbose_output(
1177+
plugin_path: &Path,
1178+
manifest_path: &Path,
1179+
previous_enabled: Option<bool>,
1180+
current_enabled: bool,
1181+
) -> String {
1182+
let previous = previous_enabled
1183+
.map(|value| value.to_string())
1184+
.unwrap_or_else(|| "not set".to_string());
1185+
1186+
format!(
1187+
" Plugin directory: {}\n Manifest path: {}\n Previous enabled: {}\n Current enabled: {}",
1188+
plugin_path.display(),
1189+
manifest_path.display(),
1190+
previous,
1191+
current_enabled
1192+
)
1193+
}
1194+
1195+
async fn run_enable(args: PluginEnableArgs, verbose: bool) -> Result<()> {
11731196
let plugins_dir = get_plugins_dir();
11741197
let plugin_path = plugins_dir.join(&args.name);
11751198
let manifest_path = plugin_path.join("plugin.toml");
@@ -1180,13 +1203,20 @@ async fn run_enable(args: PluginEnableArgs) -> Result<()> {
11801203

11811204
let content = std::fs::read_to_string(&manifest_path)?;
11821205
let mut manifest: toml::Value = toml::from_str(&content)?;
1206+
let previous_enabled = plugin_enabled_value(&manifest);
11831207

11841208
if let Some(table) = manifest.as_table_mut() {
11851209
table.insert("enabled".to_string(), toml::Value::Boolean(true));
11861210
}
11871211

11881212
std::fs::write(&manifest_path, toml::to_string_pretty(&manifest)?)?;
11891213
println!("Plugin '{}' enabled.", args.name);
1214+
if verbose {
1215+
println!(
1216+
"{}",
1217+
format_enable_verbose_output(&plugin_path, &manifest_path, previous_enabled, true)
1218+
);
1219+
}
11901220
Ok(())
11911221
}
11921222

@@ -2471,6 +2501,26 @@ mod tests {
24712501
assert_eq!(args.name, "enable-me", "name should match");
24722502
}
24732503

2504+
#[test]
2505+
fn test_plugin_enable_verbose_output_adds_diagnostics() {
2506+
let plugin_path = PathBuf::from("/home/user/.cortex/plugins/sample-one");
2507+
let manifest_path = plugin_path.join("plugin.toml");
2508+
2509+
let default_output = "Plugin 'sample-one' enabled.".to_string();
2510+
let verbose_details =
2511+
format_enable_verbose_output(&plugin_path, &manifest_path, Some(false), true);
2512+
let verbose_output = format!("{default_output}\n{verbose_details}");
2513+
2514+
assert_ne!(
2515+
default_output, verbose_output,
2516+
"verbose enable output should differ from default output"
2517+
);
2518+
assert!(verbose_output.contains("Plugin directory:"));
2519+
assert!(verbose_output.contains("Manifest path:"));
2520+
assert!(verbose_output.contains("Previous enabled: false"));
2521+
assert!(verbose_output.contains("Current enabled: true"));
2522+
}
2523+
24742524
#[test]
24752525
fn test_plugin_disable_args() {
24762526
let args = PluginDisableArgs {

0 commit comments

Comments
 (0)