Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/cortex-cli/src/agent_cmd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 9 additions & 0 deletions src/cortex-cli/src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,15 @@ mod tests {
assert!(cli.verbose);
}

#[test]
fn test_cli_global_verbose_with_plugin_enable() {
let cli = Cli::try_parse_from(["cortex", "--verbose", "plugin", "enable", "sample-one"])
.expect("should parse global --verbose with plugin enable");

assert!(cli.verbose);
assert!(matches!(cli.command, Some(Commands::Plugin(_))));
}

#[test]
fn test_cli_verbose_short_flag() {
let cli = Cli::try_parse_from(["cortex", "-v"]).expect("should parse -v");
Expand Down
2 changes: 1 addition & 1 deletion src/cortex-cli/src/cli/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub async fn dispatch_command(cli: Cli) -> Result<()> {
Some(Commands::Debug(debug_cli)) => debug_cli.run().await,
Some(Commands::Servers(servers_cli)) => run_servers(servers_cli).await,
Some(Commands::History(history_cli)) => run_history(history_cli).await,
Some(Commands::Plugin(plugin_cli)) => plugin_cli.run().await,
Some(Commands::Plugin(plugin_cli)) => plugin_cli.run(cli.verbose).await,
Some(Commands::Feedback(feedback_cli)) => feedback_cli.run().await,
Some(Commands::Lock(lock_cli)) => lock_cli.run().await,
Some(Commands::Alias(alias_cli)) => alias_cli.run().await,
Expand Down
56 changes: 53 additions & 3 deletions src/cortex-cli/src/plugin_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,12 +939,12 @@ cp plugin.toml ~/.cortex/plugins/{}/

impl PluginCli {
/// Run the plugin command.
pub async fn run(self) -> Result<()> {
pub async fn run(self, verbose: bool) -> Result<()> {
match self.subcommand {
PluginSubcommand::List(args) => run_list(args).await,
PluginSubcommand::Install(args) => run_install(args).await,
PluginSubcommand::Remove(args) => run_remove(args).await,
PluginSubcommand::Enable(args) => run_enable(args).await,
PluginSubcommand::Enable(args) => run_enable(args, verbose).await,
PluginSubcommand::Disable(args) => run_disable(args).await,
PluginSubcommand::Show(args) => run_show(args).await,
PluginSubcommand::New(args) => run_new(args).await,
Expand Down Expand Up @@ -1169,7 +1169,30 @@ async fn run_remove(args: PluginRemoveArgs) -> Result<()> {
Ok(())
}

async fn run_enable(args: PluginEnableArgs) -> Result<()> {
fn plugin_enabled_value(manifest: &toml::Value) -> Option<bool> {
manifest.get("enabled").and_then(|value| value.as_bool())
}

fn format_enable_verbose_output(
plugin_path: &Path,
manifest_path: &Path,
previous_enabled: Option<bool>,
current_enabled: bool,
) -> String {
let previous = previous_enabled
.map(|value| value.to_string())
.unwrap_or_else(|| "not set".to_string());

format!(
" Plugin directory: {}\n Manifest path: {}\n Previous enabled: {}\n Current enabled: {}",
plugin_path.display(),
manifest_path.display(),
previous,
current_enabled
)
}

async fn run_enable(args: PluginEnableArgs, verbose: bool) -> Result<()> {
let plugins_dir = get_plugins_dir();
let plugin_path = plugins_dir.join(&args.name);
let manifest_path = plugin_path.join("plugin.toml");
Expand All @@ -1180,13 +1203,20 @@ async fn run_enable(args: PluginEnableArgs) -> Result<()> {

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

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

std::fs::write(&manifest_path, toml::to_string_pretty(&manifest)?)?;
println!("Plugin '{}' enabled.", args.name);
if verbose {
println!(
"{}",
format_enable_verbose_output(&plugin_path, &manifest_path, previous_enabled, true)
);
}
Ok(())
}

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

#[test]
fn test_plugin_enable_verbose_output_adds_diagnostics() {
let plugin_path = PathBuf::from("/home/user/.cortex/plugins/sample-one");
let manifest_path = plugin_path.join("plugin.toml");

let default_output = "Plugin 'sample-one' enabled.".to_string();
let verbose_details =
format_enable_verbose_output(&plugin_path, &manifest_path, Some(false), true);
let verbose_output = format!("{default_output}\n{verbose_details}");

assert_ne!(
default_output, verbose_output,
"verbose enable output should differ from default output"
);
assert!(verbose_output.contains("Plugin directory:"));
assert!(verbose_output.contains("Manifest path:"));
assert!(verbose_output.contains("Previous enabled: false"));
assert!(verbose_output.contains("Current enabled: true"));
}

#[test]
fn test_plugin_disable_args() {
let args = PluginDisableArgs {
Expand Down