Skip to content

Commit

Permalink
Make runner make more sense
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Mar 25, 2024
1 parent 66769ca commit 950f54f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
38 changes: 24 additions & 14 deletions src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>) -> 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?;

Expand All @@ -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(),
}
}
}

0 comments on commit 950f54f

Please sign in to comment.