Skip to content

Commit

Permalink
Add json output.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 15, 2023
1 parent dbfcc53 commit 895e7d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#### 🚀 Updates

- Added a `proto tool info` command for viewing information about a tool and its plugin.
- Added a `detect-strategy` setting to `~/.proto/config.toml` to configure which strategy to use when detecting a version. Accepts:
- `first-available` (default) - Will use the first available version that is found. Either from `.prototools` or a tool specific file (`.nvmrc`, etc).
- `prefer-prototools` - Prefer a `.prototools` version, even if found in a parent directory. If none found, falls back to tool specific file.
Expand Down
40 changes: 36 additions & 4 deletions crates/cli/src/commands/tool/info.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
use crate::printer::Printer;
use clap::Args;
use proto_core::{detect_version, load_tool, Id, PluginLocator};
use miette::IntoDiagnostic;
use proto_core::{detect_version, load_tool, ExecutableLocation, Id, PluginLocator};
use proto_pdk_api::ToolMetadataOutput;
use serde::Serialize;
use starbase::system;
use starbase_styles::color;
use starbase_utils::json;
use std::path::PathBuf;

#[derive(Serialize)]
pub struct PluginItem {
pub struct ToolInfo {
bins: Vec<ExecutableLocation>,
exe_path: PathBuf,
globals_dir: Option<PathBuf>,
globals_prefix: Option<String>,
id: Id,
locator: PluginLocator,
inventory_dir: PathBuf,
metadata: ToolMetadataOutput,
name: String,
version: Option<String>,
plugin: PluginLocator,
shims: Vec<ExecutableLocation>,
}

#[derive(Args, Clone, Debug)]
pub struct ToolInfoArgs {
#[arg(required = true, help = "ID of tool")]
id: Id,

#[arg(long, help = "Print the info in JSON format")]
json: bool,
}

#[system]
Expand All @@ -28,6 +41,25 @@ pub async fn tool_info(args: ArgsRef<ToolInfoArgs>) {
tool.create_executables(false, false).await?;
tool.locate_globals_dir().await?;

if args.json {
let info = ToolInfo {
bins: tool.get_bin_locations()?,
exe_path: tool.get_exe_path()?.to_path_buf(),
globals_dir: tool.get_globals_bin_dir().map(|p| p.to_path_buf()),
globals_prefix: tool.get_globals_prefix().map(|p| p.to_owned()),
inventory_dir: tool.get_inventory_dir(),
shims: tool.get_shim_locations()?,
id: tool.id,
name: tool.metadata.name.clone(),
metadata: tool.metadata,
plugin: tool.locator.unwrap(),
};

println!("{}", json::to_string_pretty(&info).into_diagnostic()?);

return Ok(());
}

let mut printer = Printer::new();

printer.header(&tool.id, &tool.metadata.name);
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::time::{Duration, SystemTime};
use tracing::{debug, trace, warn};
use warpgate::{download_from_url_to_file, Id, PluginContainer, PluginLocator, VirtualPath};

#[derive(Debug, Default)]
#[derive(Debug, Default, Serialize)]
pub struct ExecutableLocation {
pub config: ExecutableConfig,
pub name: String,
Expand Down

0 comments on commit 895e7d0

Please sign in to comment.