-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(registry): add --json flag to registry command #7290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ use crate::registry::{REGISTRY, RegistryTool, tool_enabled}; | |||||
| use crate::ui::table::MiseTable; | ||||||
| use eyre::{Result, bail}; | ||||||
| use itertools::Itertools; | ||||||
| use serde_derive::Serialize; | ||||||
|
|
||||||
| /// List available tools to install | ||||||
| /// | ||||||
|
|
@@ -27,18 +28,38 @@ pub struct Registry { | |||||
| /// Hide aliased tools | ||||||
| #[clap(long)] | ||||||
| hide_aliased: bool, | ||||||
|
|
||||||
| /// Output in JSON format | ||||||
| #[clap(long, short = 'J')] | ||||||
| json: bool, | ||||||
| } | ||||||
|
|
||||||
| #[derive(Serialize)] | ||||||
| struct RegistryToolOutput { | ||||||
| short: String, | ||||||
| backends: Vec<String>, | ||||||
| #[serde(skip_serializing_if = "Option::is_none")] | ||||||
| description: Option<String>, | ||||||
| #[serde(skip_serializing_if = "Vec::is_empty")] | ||||||
| aliases: Vec<String>, | ||||||
| } | ||||||
|
|
||||||
| impl Registry { | ||||||
| pub async fn run(self) -> Result<()> { | ||||||
| if let Some(name) = &self.name { | ||||||
| if let Some(rt) = REGISTRY.get(name.as_str()) { | ||||||
| miseprintln!("{}", rt.backends().join(" ")); | ||||||
| if self.json { | ||||||
| self.display_single_json(name, rt)?; | ||||||
| } else { | ||||||
| miseprintln!("{}", rt.backends().join(" ")); | ||||||
| } | ||||||
| } else { | ||||||
| bail!("tool not found in registry: {name}"); | ||||||
| } | ||||||
| } else if self.complete { | ||||||
| self.complete()?; | ||||||
| } else if self.json { | ||||||
| self.display_json()?; | ||||||
| } else { | ||||||
| self.display_table()?; | ||||||
| } | ||||||
|
|
@@ -96,6 +117,60 @@ impl Registry { | |||||
| }); | ||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| fn display_json(&self) -> Result<()> { | ||||||
| let filter_backend = |rt: &RegistryTool| { | ||||||
| if let Some(backend) = &self.backend { | ||||||
| rt.backends() | ||||||
| .iter() | ||||||
| .filter(|full| full.starts_with(&format!("{backend}:"))) | ||||||
| .cloned() | ||||||
| .collect() | ||||||
| } else { | ||||||
| rt.backends() | ||||||
| } | ||||||
| }; | ||||||
| let tools: Vec<RegistryToolOutput> = REGISTRY | ||||||
| .iter() | ||||||
| .filter(|(short, _)| filter_enabled(short)) | ||||||
| .filter(|(short, rt)| !self.hide_aliased || **short == rt.short) | ||||||
| .map(|(short, rt)| { | ||||||
| let backends = filter_backend(rt); | ||||||
| RegistryToolOutput { | ||||||
| short: short.to_string(), | ||||||
| backends: backends.iter().map(|s| s.to_string()).collect(), | ||||||
|
||||||
| backends: backends.iter().map(|s| s.to_string()).collect(), | |
| backends, |
Copilot
AI
Dec 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The backends are already strings from filter_backend, so calling .to_string() creates unnecessary allocations. Since filter_backend returns a cloned collection, you can use the backends directly without the extra iteration and string conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
filter_backendclosure is duplicated in bothdisplay_jsonanddisplay_single_jsonmethods. Extract this into a shared helper method to improve maintainability and reduce code duplication.