use std::collections::BTreeSet;
use std::sync::Arc;
use color_eyre::eyre::Result;
use crate::config::Config;
use crate::output::Output;
use crate::plugins::{ExternalPlugin, PluginType};
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "list", after_long_help = AFTER_LONG_HELP, verbatim_doc_comment)]
pub struct PluginsLs {
#[clap(short, long, hide = true, verbatim_doc_comment)]
pub all: bool,
#[clap(short, long, verbatim_doc_comment, conflicts_with = "all")]
pub core: bool,
#[clap(long, verbatim_doc_comment, conflicts_with = "all")]
pub user: bool,
#[clap(short, long, verbatim_doc_comment)]
pub urls: bool,
#[clap(long, verbatim_doc_comment)]
pub refs: bool,
}
impl PluginsLs {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let mut tools = config.plugins.values().cloned().collect::<BTreeSet<_>>();
if self.all {
for (plugin, url) in config.get_shorthands() {
let mut ep = ExternalPlugin::new(plugin.clone());
ep.repo_url = Some(url.to_string());
tools.insert(Arc::new(ep));
}
} else if self.user && self.core {
} else if self.core {
tools.retain(|p| matches!(p.get_type(), PluginType::Core));
} else {
tools.retain(|p| matches!(p.get_type(), PluginType::External));
}
if self.urls || self.refs {
for tool in tools {
rtxprint!(out, "{:29}", tool.name());
if self.urls {
if let Some(url) = tool.get_remote_url() {
rtxprint!(out, " {}", url);
}
}
if self.refs {
if let Ok(aref) = tool.current_abbrev_ref() {
rtxprint!(out, " {}", aref);
}
if let Ok(sha) = tool.current_sha_short() {
rtxprint!(out, " {}", sha);
}
}
rtxprint!(out, "\n");
}
} else {
for tool in tools {
rtxprintln!(out, "{tool}");
}
}
Ok(())
}
}
static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>rtx plugins ls</bold>
node
ruby
$ <bold>rtx plugins ls --urls</bold>
node https://github.com/asdf-vm/asdf-node.git
ruby https://github.com/asdf-vm/asdf-ruby.git
"#
);
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use crate::cli::tests::grep;
use crate::{assert_cli, assert_cli_snapshot};
#[test]
fn test_plugin_list() {
assert_cli_snapshot!("plugin", "list");
}
#[test]
fn test_plugin_list_urls() {
let stdout = assert_cli!("plugin", "list", "--urls");
assert!(stdout.contains("dummy"))
}
#[test]
fn test_plugin_list_all() {
let stdout = assert_cli!("plugin", "list", "--all", "--urls");
assert_str_eq!(
grep(stdout, "zephyr"),
"zephyr https://github.com/nsaunders/asdf-zephyr.git"
);
}
#[test]
fn test_plugin_refs() {
let stdout = assert_cli!("plugin", "list", "--refs");
assert!(stdout.contains("dummy"))
}
}