diff --git a/src/config/mod.rs b/src/config/mod.rs index 10ef702..c6e7845 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,14 +1,15 @@ use std::fs; use std::fs::File; use std::io::Write; -use std::path::{PathBuf, Path}; +use std::path::{Path, PathBuf}; use toml::map::Map; -use toml::Value; +use crate::environment::Environment; use toml::value::Value::Table; +use toml::Value; pub struct Configuration { - config: Value + config: Value, } pub enum Alias { @@ -162,7 +163,7 @@ fn merge_values(v1: &Value, } } -fn create_config_if_needed(config_file_path: &Path) -> Result<(), String> { +fn create_config_if_needed(config_file_path: &Path, environment: &Environment) -> Result<(), String> { if !config_file_path.exists() { let config_file_path_str = match config_file_path.to_str() { @@ -173,8 +174,11 @@ fn create_config_if_needed(config_file_path: &Path) -> Result<(), String> { let mut f = File::create(config_file_path) .map_err(|_| format!("Unable to create {} file", config_file_path_str))?; + let auto_detect_executable = environment.try_detect_executable(); + let sample_config_content = [ - "executable=\"/bin/bash\"", + &auto_detect_executable.map(|x| format!("executable=\"{}\"", x)) + .unwrap_or("#executable=\"not-found\"".to_string()), "", "[alias]", "test_alias1=\"--help\"" @@ -206,9 +210,10 @@ pub fn empty_configuration() -> Configuration { Configuration { config: Table(Map::new()) } } -pub fn get_configuration(executable_dir: &Path) -> Result { +pub fn get_configuration(environment: &Environment) -> Result { + let executable_dir = environment.executable_dir(); let config_file_path = get_config_path(executable_dir); - create_config_if_needed(&config_file_path) + create_config_if_needed(&config_file_path, &environment) .unwrap(); let configuration = read_configuration(&config_file_path); if configuration.is_err() { diff --git a/src/environment/mod.rs b/src/environment/mod.rs index 2e9cf44..149607b 100644 --- a/src/environment/mod.rs +++ b/src/environment/mod.rs @@ -1,5 +1,6 @@ use std::env; use std::path::{PathBuf}; +use crate::environment::autodetect_executable::{autodetect_executable, OsFileSystemWrapper}; pub mod expand_env; pub mod autodetect_executable; @@ -27,6 +28,13 @@ impl Environment { pub fn shell(&self) -> String { self.shell.to_string() } + + pub fn try_detect_executable(&self) -> Option { + autodetect_executable( + self.executable_dir().as_path(), + self.executable_name.as_str(), + &OsFileSystemWrapper {}) + } } struct SystemEnvironment {} @@ -66,11 +74,11 @@ impl SystemEnvironment { pub fn system_environment() -> Environment { let sys_env = SystemEnvironment {}; - return Environment { + Environment { executable_name: sys_env.executable_name().unwrap(), executable_dir: sys_env.executable_dir().unwrap(), args: sys_env.call_arguments(), shell: sys_env.shell().unwrap(), - }; + } } diff --git a/src/handler/default.rs b/src/handler/default.rs index f3c5d9a..afb42be 100644 --- a/src/handler/default.rs +++ b/src/handler/default.rs @@ -1,9 +1,9 @@ -use crate::{config, environment, process}; use crate::config::Alias::{RegularAlias, ShellAlias}; -use crate::config::{Configuration, Alias}; -use crate::environment::{Environment, expand_env, autodetect_executable::{OsFileSystemWrapper, autodetect_executable}}; +use crate::config::{Alias, Configuration}; +use crate::environment::{expand_env, Environment}; use crate::handler::Handler; use crate::process::CallContext; +use crate::{config, environment, process}; fn get_call_context(environment: &environment::Environment, configuration: &config::Configuration) -> Result { @@ -38,10 +38,7 @@ fn get_call_context(environment: &environment::Environment, fn get_executable(environment: &Environment, configuration: &Configuration) -> Result, String> { Ok(configuration.get_executable()? .map(|config| expand_env::expand_env_var(&config)) - .or_else(|| autodetect_executable( - environment.executable_dir().as_path(), - environment.executable_name().as_str(), - &OsFileSystemWrapper {}))) + .or_else(|| environment.try_detect_executable())) } fn forward_call_to_target_application(configuration: &Configuration, call_arguments: &[String], executable: String, shell: String) -> Result { diff --git a/src/main.rs b/src/main.rs index 1825139..6ddeab9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,9 +26,8 @@ fn get_handler(environment: &environment::Environment) -> Box { fn main() { let environment = environment::system_environment(); - let executable_dir = environment.executable_dir(); - let configuration = config::get_configuration(executable_dir); + let configuration = config::get_configuration(&environment); match configuration { Ok(config) => {