Skip to content

Commit

Permalink
Add info command.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 15, 2023
1 parent f068440 commit b2a8af5
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 49 deletions.
12 changes: 9 additions & 3 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::commands::{
AddToolArgs, AliasArgs, BinArgs, CleanArgs, CompletionsArgs, InstallArgs, InstallGlobalArgs,
ListArgs, ListGlobalArgs, ListPluginsArgs, ListRemoteArgs, ListToolPluginsArgs, ListToolsArgs,
MigrateArgs, OutdatedArgs, PinArgs, RemoveToolArgs, RunArgs, SetupArgs, UnaliasArgs,
ListArgs, ListGlobalArgs, ListRemoteArgs, ListToolPluginsArgs, ListToolsArgs, MigrateArgs,
OutdatedArgs, PinArgs, RemoveToolArgs, RunArgs, SetupArgs, ToolInfoArgs, UnaliasArgs,
UninstallArgs, UninstallGlobalArgs,
};
use clap::builder::styling::{Color, Style, Styles};
Expand Down Expand Up @@ -182,7 +182,7 @@ pub enum Commands {
about = "List all active and configured plugins.",
hide = true
)]
Plugins(ListPluginsArgs),
Plugins(ListToolPluginsArgs),

#[command(
alias = "rp",
Expand Down Expand Up @@ -259,6 +259,12 @@ pub enum ToolCommands {
)]
Add(AddToolArgs),

#[command(
name = "info",
about = "Display information about a tool and its plugin."
)]
Info(ToolInfoArgs),

#[command(name = "list", about = "List all installed tools and their versions.")]
List(ListToolsArgs),

Expand Down
124 changes: 112 additions & 12 deletions crates/cli/src/commands/tool/info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use crate::helpers::load_configured_tools;
use super::list_plugins::print_locator;
use clap::Args;
use miette::IntoDiagnostic;
use proto_core::{Id, PluginLocator};
use proto_core::{detect_version, load_tool, Id, PluginLocator};
use serde::Serialize;
use starbase::system;
use starbase_styles::color::{self, OwoStyle};
use starbase_utils::json;
use std::io::{BufWriter, Write};
use tracing::info;

#[derive(Serialize)]
pub struct PluginItem {
Expand All @@ -18,16 +15,119 @@ pub struct PluginItem {
}

#[derive(Args, Clone, Debug)]
pub struct ListPluginsArgs {
#[arg(long, help = "Print the list in JSON format")]
json: bool,
pub struct ToolInfoArgs {
#[arg(required = true, help = "ID of tool")]
id: Id,
}

#[system]
pub async fn info_plugin(args: ArgsRef<ListPluginsArgs>) {
if !args.json {
info!("Loading plugins...");
pub async fn tool_info(args: ArgsRef<ToolInfoArgs>) {
let mut tool = load_tool(&args.id).await?;
let version = detect_version(&tool, None).await?;

tool.resolve_version(&version, false).await?;
tool.create_executables(false, false).await?;
tool.locate_globals_dir().await?;

let stdout = std::io::stdout();
let mut buffer = BufWriter::new(stdout.lock());

writeln!(
buffer,
"{} {} {}",
OwoStyle::new().bold().style(color::id(&tool.id)),
color::muted("-"),
color::muted_light(&tool.metadata.name),
)
.unwrap();

writeln!(
buffer,
"\n{}",
OwoStyle::new()
.bold()
.style(color::muted_light("Inventory"))
)
.unwrap();

writeln!(buffer, " Store: {}", color::path(tool.get_inventory_dir())).unwrap();
writeln!(
buffer,
" Executable: {}",
color::path(tool.get_exe_path()?)
)
.unwrap();

if let Some(dir) = tool.get_globals_bin_dir() {
writeln!(buffer, " Globals directory: {}", color::path(dir)).unwrap();
}

if let Some(prefix) = tool.get_globals_prefix() {
writeln!(buffer, " Globals prefix: {}", color::property(prefix)).unwrap();
}

let tools = load_configured_tools().await?;
let bins = tool.get_bin_locations()?;

if bins.is_empty() {
writeln!(buffer, " Binaries: {}", color::failure("None")).unwrap();
} else {
writeln!(buffer, " Binaries:").unwrap();

for bin in bins {
writeln!(
buffer,
" {} {} {}",
color::muted("-"),
color::path(bin.path),
if bin.primary {
color::muted_light("(primary)")
} else {
"".into()
}
)
.unwrap();
}
}

let shims = tool.get_shim_locations()?;

if shims.is_empty() {
writeln!(buffer, " Shims: {}", color::failure("None")).unwrap();
} else {
writeln!(buffer, " Shims:").unwrap();

for shim in shims {
writeln!(
buffer,
" {} {} {}",
color::muted("-"),
color::path(shim.path),
if shim.primary {
color::muted_light("(primary)")
} else {
"".into()
}
)
.unwrap();
}
}

writeln!(
buffer,
"\n{}",
OwoStyle::new().bold().style(color::muted_light("Plugin"))
)
.unwrap();

if let Some(version) = &tool.metadata.plugin_version {
writeln!(buffer, " Version: {}", color::hash(version)).unwrap();
}

if let Some(locator) = &tool.locator {
print_locator(&mut buffer, locator);
}

writeln!(buffer, "").unwrap();

buffer.flush().unwrap();
}
70 changes: 37 additions & 33 deletions crates/cli/src/commands/tool/list_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,42 +66,46 @@ pub async fn tool_list_plugins(args: ArgsRef<ListToolPluginsArgs>) {
)
.unwrap();

match item.locator {
PluginLocator::SourceFile { path, .. } => {
writeln!(
buffer,
" Source: {}",
color::path(path.canonicalize().unwrap())
)
.unwrap();
}
PluginLocator::SourceUrl { url } => {
writeln!(buffer, " Source: {}", color::url(url)).unwrap();
}
PluginLocator::GitHub(github) => {
writeln!(buffer, " GitHub: {}", color::label(&github.repo_slug)).unwrap();

writeln!(
buffer,
" Tag: {}",
color::hash(github.tag.as_deref().unwrap_or("latest")),
)
.unwrap();
}
PluginLocator::Wapm(wapm) => {
writeln!(buffer, " Package: {}", color::label(&wapm.package_name)).unwrap();

writeln!(
buffer,
" Version: {}",
color::hash(wapm.version.as_deref().unwrap_or("latest")),
)
.unwrap();
}
};
print_locator(&mut buffer, &item.locator);

writeln!(buffer).unwrap();
}

buffer.flush().unwrap();
}

pub fn print_locator<T: Write>(buffer: &mut BufWriter<T>, locator: &PluginLocator) {
match locator {
PluginLocator::SourceFile { path, .. } => {
writeln!(
buffer,
" Source: {}",
color::path(path.canonicalize().unwrap())
)
.unwrap();
}
PluginLocator::SourceUrl { url } => {
writeln!(buffer, " Source: {}", color::url(url)).unwrap();
}
PluginLocator::GitHub(github) => {
writeln!(buffer, " GitHub: {}", color::label(&github.repo_slug)).unwrap();

writeln!(
buffer,
" Tag: {}",
color::hash(github.tag.as_deref().unwrap_or("latest")),
)
.unwrap();
}
PluginLocator::Wapm(wapm) => {
writeln!(buffer, " Package: {}", color::label(&wapm.package_name)).unwrap();

writeln!(
buffer,
" Version: {}",
color::hash(wapm.version.as_deref().unwrap_or("latest")),
)
.unwrap();
}
};
}
1 change: 1 addition & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async fn main() -> MainResult {
Commands::Setup(args) => app.execute_with_args(commands::setup, args),
Commands::Tool { command } => match command {
ToolCommands::Add(args) => app.execute_with_args(commands::tool_add, args),
ToolCommands::Info(args) => app.execute_with_args(commands::tool_info, args),
ToolCommands::List(args) => app.execute_with_args(commands::tool_list, args),
ToolCommands::ListPlugins(args) => {
app.execute_with_args(commands::tool_list_plugins, args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
}
}

mod tool_plugins {
mod plugins {
use super::*;

#[tokio::test]
Expand Down

0 comments on commit b2a8af5

Please sign in to comment.