refactor(cli): simplify plugin CLI helpers

This commit is contained in:
Casey Chow
2026-05-06 16:23:08 -04:00
parent 664a6ccdb0
commit 3ce26358ab
2 changed files with 27 additions and 55 deletions

View File

@@ -204,19 +204,23 @@ struct PluginSelection {
plugin_key: String,
}
impl PluginSelection {
fn from_plugin_id(plugin_id: PluginId) -> Self {
let plugin_key = plugin_id.as_key();
Self {
plugin_name: plugin_id.plugin_name,
marketplace_name: plugin_id.marketplace_name,
plugin_key,
}
}
}
fn parse_plugin_selection(
plugin: String,
marketplace_name: Option<String>,
) -> Result<PluginSelection> {
match (PluginId::parse(&plugin), marketplace_name) {
(Ok(plugin_id), None) => {
let plugin_key = plugin_id.as_key();
Ok(PluginSelection {
plugin_name: plugin_id.plugin_name,
marketplace_name: plugin_id.marketplace_name,
plugin_key,
})
}
(Ok(plugin_id), None) => Ok(PluginSelection::from_plugin_id(plugin_id)),
(Ok(plugin_id), Some(marketplace_name)) => {
if plugin_id.marketplace_name != marketplace_name {
bail!(
@@ -226,22 +230,12 @@ fn parse_plugin_selection(
marketplace_name
);
}
let plugin_key = plugin_id.as_key();
Ok(PluginSelection {
plugin_name: plugin_id.plugin_name,
marketplace_name: plugin_id.marketplace_name,
plugin_key,
})
}
(Err(_), Some(marketplace_name)) => {
let plugin_id = PluginId::new(plugin, marketplace_name)?;
let plugin_key = plugin_id.as_key();
Ok(PluginSelection {
plugin_name: plugin_id.plugin_name,
marketplace_name: plugin_id.marketplace_name,
plugin_key,
})
Ok(PluginSelection::from_plugin_id(plugin_id))
}
(Err(_), Some(marketplace_name)) => Ok(PluginSelection::from_plugin_id(PluginId::new(
plugin,
marketplace_name,
)?)),
(Err(_), None) => {
bail!("plugin requires --marketplace unless passed as <plugin>@<marketplace>")
}

View File

@@ -58,8 +58,7 @@ fn write_marketplace_source(source: &Path) -> Result<()> {
Ok(())
}
#[tokio::test]
async fn marketplace_list_shows_configured_marketplace_names() -> Result<()> {
fn setup_local_marketplace() -> Result<(TempDir, TempDir)> {
let codex_home = TempDir::new()?;
let source = TempDir::new()?;
write_plugins_enabled_config(codex_home.path())?;
@@ -70,6 +69,12 @@ async fn marketplace_list_shows_configured_marketplace_names() -> Result<()> {
"debug",
&configured_local_marketplace(&source_path),
)?;
Ok((codex_home, source))
}
#[tokio::test]
async fn marketplace_list_shows_configured_marketplace_names() -> Result<()> {
let (codex_home, source) = setup_local_marketplace()?;
codex_command(codex_home.path())?
.args(["plugin", "marketplace", "list"])
@@ -83,16 +88,7 @@ async fn marketplace_list_shows_configured_marketplace_names() -> Result<()> {
#[tokio::test]
async fn plugin_list_shows_plugins_grouped_by_marketplace() -> Result<()> {
let codex_home = TempDir::new()?;
let source = TempDir::new()?;
write_plugins_enabled_config(codex_home.path())?;
write_marketplace_source(source.path())?;
let source_path = source.path().to_string_lossy().into_owned();
record_user_marketplace(
codex_home.path(),
"debug",
&configured_local_marketplace(&source_path),
)?;
let (codex_home, _source) = setup_local_marketplace()?;
codex_command(codex_home.path())?
.args(["plugin", "list"])
@@ -106,16 +102,7 @@ async fn plugin_list_shows_plugins_grouped_by_marketplace() -> Result<()> {
#[tokio::test]
async fn plugin_add_and_remove_updates_installed_plugin_config() -> Result<()> {
let codex_home = TempDir::new()?;
let source = TempDir::new()?;
write_plugins_enabled_config(codex_home.path())?;
write_marketplace_source(source.path())?;
let source_path = source.path().to_string_lossy().into_owned();
record_user_marketplace(
codex_home.path(),
"debug",
&configured_local_marketplace(&source_path),
)?;
let (codex_home, _source) = setup_local_marketplace()?;
codex_command(codex_home.path())?
.args(["plugin", "add", "sample@debug"])
@@ -142,16 +129,7 @@ async fn plugin_add_and_remove_updates_installed_plugin_config() -> Result<()> {
#[tokio::test]
async fn plugin_remove_works_after_marketplace_is_removed() -> Result<()> {
let codex_home = TempDir::new()?;
let source = TempDir::new()?;
write_plugins_enabled_config(codex_home.path())?;
write_marketplace_source(source.path())?;
let source_path = source.path().to_string_lossy().into_owned();
record_user_marketplace(
codex_home.path(),
"debug",
&configured_local_marketplace(&source_path),
)?;
let (codex_home, _source) = setup_local_marketplace()?;
codex_command(codex_home.path())?
.args(["plugin", "add", "sample", "--marketplace", "debug"])