From b9f4f86758fb5cf03db0a7b2b8e77ce87f43d6b4 Mon Sep 17 00:00:00 2001 From: quirinecker Date: Mon, 6 Mar 2023 21:46:12 +0100 Subject: [PATCH] now exporting path variable at the start of the application --- src/app/commands/health.rs | 6 +++++- src/app/fs_util/mod.rs | 17 +++++++++-------- src/app/mod.rs | 6 ++++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/app/commands/health.rs b/src/app/commands/health.rs index b9a980e..06beebe 100644 --- a/src/app/commands/health.rs +++ b/src/app/commands/health.rs @@ -2,6 +2,8 @@ use std::{collections::HashMap, process, io::ErrorKind}; use colored::Colorize; +use crate::app::fs_util; + use super::traits::Command; pub struct Health; @@ -71,7 +73,9 @@ impl Health { } fn check_command(command: &str) -> bool { - return match process::Command::new(command).output() { + return match process::Command::new(command) + // .env("PATH", fs_util::docki_path_env()) + .output() { Ok(_) => true, Err(e) => ErrorKind::NotFound != e.kind() } diff --git a/src/app/fs_util/mod.rs b/src/app/fs_util/mod.rs index d44eb2d..e01d58e 100644 --- a/src/app/fs_util/mod.rs +++ b/src/app/fs_util/mod.rs @@ -1,4 +1,4 @@ -use std::{fs, path::Path, env}; +use std::{env, fs, path::Path}; struct RecursivePathFetch { paths: Vec, @@ -53,8 +53,7 @@ impl RecursivePathFetch { } pub fn fetch_paths_recursive(path: &str) -> Result, String> { - let mut path_fetch = - RecursivePathFetch::new_with_extension_filter(path.to_string()); + let mut path_fetch = RecursivePathFetch::new_with_extension_filter(path.to_string()); return path_fetch.fetch(); } @@ -73,11 +72,13 @@ pub fn directory_exists(path: &String) -> bool { Path::new(path).is_dir() } - pub fn expand_path(path: String) -> String { - let home_dir = env::var("HOME") - .expect("could not find home dir"); - - return path.replace("~", &home_dir) + let home_dir = env::var("HOME").expect("could not find home dir"); + return path.replace("~", &home_dir); +} + +pub fn docki_path_env() -> String { + let current = env::var("PATH").unwrap_or("".to_string()); + return expand_path(format!("{}:~/.docki/", current)); } diff --git a/src/app/mod.rs b/src/app/mod.rs index 1c39049..ff73392 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -3,6 +3,7 @@ pub mod builder; pub mod fs_util; use std::collections::HashMap; +use std::env; use commands::traits::Command; use commands::CommandRegistry; @@ -19,6 +20,7 @@ impl App { } pub fn start(self, args: Vec) { + Self::preapare_env_path(); let command_args = &args[1..]; let mut path = String::from(""); let mut argument_map = HashMap::new(); @@ -45,6 +47,10 @@ impl App { self.execute_path(&path, &argument_map); } + fn preapare_env_path() { + env::set_var("PATH", fs_util::docki_path_env()); + } + fn execute_path(self, path: &String, args: &HashMap) { let command = self.command_regisrty.command_by(path);