Skip to content

Commit

Permalink
feat: Add moonup which command
Browse files Browse the repository at this point in the history
Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
  • Loading branch information
chawyehsu committed Jul 17, 2024
1 parent bf40e23 commit 724d177
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/bin/moonup-shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use std::process::Command;

use moonup::constant::RECURSION_LIMIT;
use moonup::utils::detect_toolchain_version;
use moonup::utils::detect_active_toolchain;

pub fn main() {
if let Err(err) = run() {
Expand Down Expand Up @@ -44,7 +44,7 @@ fn run() -> Result<()> {
return Err(anyhow::anyhow!("cannot run moonup-shim directly"));
}

let toolchain_root = detect_toolchain_version();
let toolchain_root = detect_active_toolchain();
let actual_exe = toolchain_root.join("bin").join(&shim_name);

let mut cmd = Command::new(actual_exe);
Expand Down
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilte
mod install;
mod pin;
mod show;
mod which;

#[derive(Debug, Parser)]
#[clap(arg_required_else_help = true)]
Expand All @@ -25,6 +26,7 @@ pub enum Command {
Install(install::Args),
Pin(pin::Args),
Show(show::Args),
Which(which::Args),
}

/// CLI entry point
Expand All @@ -37,6 +39,7 @@ pub async fn start() -> miette::Result<()> {
Command::Install(args) => install::execute(args).await?,
Command::Pin(args) => pin::execute(args).await?,
Command::Show(args) => show::execute(args).await?,
Command::Which(args) => which::execute(args).await?,
}
Ok(())
}
Expand Down
41 changes: 41 additions & 0 deletions src/cli/which.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use clap::Parser;

use crate::utils::detect_active_toolchain;

/// Show the actual binary that will be run for a given command
#[derive(Parser, Debug)]
#[clap(arg_required_else_help = true)]
pub struct Args {
/// The command to inspect
command: String,
}

pub async fn execute(args: Args) -> miette::Result<()> {
let command_name = {
let name = args.command.as_str();

#[cfg(target_os = "windows")]
{
if !name.ends_with(".exe") {
format!("{}.exe", name)
} else {
name.to_string()
}
}

#[cfg(not(target_os = "windows"))]
name.to_string()
};
let mut active_toolchain = detect_active_toolchain();

active_toolchain.push("bin");
active_toolchain.push(command_name);

if active_toolchain.exists() {
println!("{}", active_toolchain.display());
} else {
eprintln!("No command for '{}'", active_toolchain.display());
}

Ok(())
}
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn detect_toolchain_file() -> Option<PathBuf> {
/// # Returns
///
/// The path to actual versioned toolchain
pub fn detect_toolchain_version() -> PathBuf {
pub fn detect_active_toolchain() -> PathBuf {
detect_toolchain_file().map_or_else(
|| moonup_home().join("toolchains").join("latest"),
|path| {
Expand Down

0 comments on commit 724d177

Please sign in to comment.