From 950f54fea6c48e5bc79a04b9e2f36243ce8f5258 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Mon, 25 Mar 2024 02:57:49 +0100 Subject: [PATCH] Make runner make more sense --- src/cli/mod.rs | 3 ++- src/main.rs | 5 ++--- src/runner/mod.rs | 38 ++++++++++++++++++++++++-------------- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 1c289ab..c67756c 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,8 +1,9 @@ -use aftman::storage::Home; use anyhow::{Context, Result}; use clap::Parser; use tokio::time::Instant; +use aftman::storage::Home; + mod add; mod install; mod list; diff --git a/src/main.rs b/src/main.rs index 0608b78..542d1f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,9 +16,8 @@ async fn main() { init_tracing(); let runner = Runner::new(); - let exe_name = runner.arg0_file_name(); - let result = if exe_name != "aftman" { - runner.run(exe_name).await + let result = if runner.should_run() { + runner.run().await } else { Cli::parse().run().await }; diff --git a/src/runner/mod.rs b/src/runner/mod.rs index 354741a..cb6afee 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -12,27 +12,21 @@ use aftman::{storage::Home, system::run_interruptible, tool::ToolAlias}; use crate::util::discover_closest_tool_spec; #[derive(Debug, Clone)] -pub struct Runner; +pub struct Runner { + exe_name: String, +} impl Runner { pub fn new() -> Self { - Self + Self::default() } - pub fn arg0_file_name(&self) -> String { - let arg0 = args().next().unwrap(); - let exe_path = PathBuf::from(arg0); - let exe_name = exe_path - .file_name() - .expect("Invalid file name passed as arg0") - .to_str() - .expect("Non-UTF8 file name passed as arg0") - .trim_end_matches(EXE_EXTENSION); - exe_name.to_string() + pub fn should_run(&self) -> bool { + self.exe_name != "aftman" } - pub async fn run(&self, alias: impl AsRef) -> Result<()> { - let alias = ToolAlias::from_str(alias.as_ref())?; + pub async fn run(&self) -> Result<()> { + let alias = ToolAlias::from_str(&self.exe_name)?; let home = Home::load_from_env().await?; @@ -51,3 +45,19 @@ impl Runner { exit(result?); } } + +impl Default for Runner { + fn default() -> Self { + let arg0 = args().next().unwrap(); + let exe_path = PathBuf::from(arg0); + let exe_name = exe_path + .file_name() + .expect("Invalid file name passed as arg0") + .to_str() + .expect("Non-UTF8 file name passed as arg0") + .trim_end_matches(EXE_EXTENSION); + Self { + exe_name: exe_name.to_string(), + } + } +}