Skip to content

Commit

Permalink
put current executable name to the default config, not /bin/bash
Browse files Browse the repository at this point in the history
  • Loading branch information
yantonov committed Nov 3, 2024
1 parent 98a9c1b commit 0aadcd1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
19 changes: 12 additions & 7 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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() {
Expand All @@ -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\""
Expand Down Expand Up @@ -206,9 +210,10 @@ pub fn empty_configuration() -> Configuration {
Configuration { config: Table(Map::new()) }
}

pub fn get_configuration(executable_dir: &Path) -> Result<Configuration, String> {
pub fn get_configuration(environment: &Environment) -> Result<Configuration, String> {
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() {
Expand Down
12 changes: 10 additions & 2 deletions src/environment/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -27,6 +28,13 @@ impl Environment {
pub fn shell(&self) -> String {
self.shell.to_string()
}

pub fn try_detect_executable(&self) -> Option<String> {
autodetect_executable(
self.executable_dir().as_path(),
self.executable_name.as_str(),
&OsFileSystemWrapper {})
}
}

struct SystemEnvironment {}
Expand Down Expand Up @@ -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(),
};
}
}

11 changes: 4 additions & 7 deletions src/handler/default.rs
Original file line number Diff line number Diff line change
@@ -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<CallContext, String> {
Expand Down Expand Up @@ -38,10 +38,7 @@ fn get_call_context(environment: &environment::Environment,
fn get_executable(environment: &Environment, configuration: &Configuration) -> Result<Option<String>, 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<CallContext, String> {
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ fn get_handler(environment: &environment::Environment) -> Box<dyn Handler> {

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) => {
Expand Down

0 comments on commit 0aadcd1

Please sign in to comment.