From ebfb69b0783570d4ca26488d0276e49cfae22859 Mon Sep 17 00:00:00 2001 From: Alois Date: Mon, 18 May 2026 21:25:19 +0200 Subject: [PATCH] [Fix] use working directory instead of executable directory --- src/main.rs | 6 ++++-- src/util/file_util.rs | 14 ++++++++++---- src/util/logger.rs | 6 ++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 67f8bb1..f92ccc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,12 +10,14 @@ use crate::sql::sql::print_users; use crate::transport::omikron_connection; use crate::util::crypto_helper::load_public_key; use crate::util::crypto_helper::load_secret_key; +use crate::util::file_util::get_directory; use crate::util::logger::PrintType; use crate::util::logger::startup; -use dotenv::dotenv; +use dotenv::from_path; use once_cell::sync::Lazy; use rustls::crypto::aws_lc_rs::default_provider; use std::env; +use std::path::Path; static PRIVATE_KEY: Lazy = Lazy::new(|| env::var("PRIVATE_KEY").unwrap()); pub fn get_private_key() -> x448::Secret { @@ -32,7 +34,7 @@ async fn main() { println!("Error loading Provider"); return; } - dotenv().ok(); + let _ = from_path(Path::new(&get_directory()).join(".env")); startup(); log_in!("Incoming messages"); log_out!("Outgoing messages"); diff --git a/src/util/file_util.rs b/src/util/file_util.rs index f1c51db..593adbe 100644 --- a/src/util/file_util.rs +++ b/src/util/file_util.rs @@ -1,3 +1,4 @@ +use once_cell::sync::Lazy; use std::fs::{self, File}; use std::io::{self, BufReader, Read}; use std::path::{Path, PathBuf}; @@ -7,6 +8,10 @@ use zip::ZipArchive; use crate::log; +static WORKING_DIR: Lazy = Lazy::new(|| { + std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")) +}); + pub fn delete_file(path: &str, name: &str) -> bool { let dir = Path::new(&get_directory()).join(path); let file = dir.join(name); @@ -163,10 +168,11 @@ pub fn get_children(path: &str) -> Vec { } pub fn get_directory() -> String { - std::env::current_dir() - .unwrap_or_else(|_| PathBuf::from(".")) - .to_string_lossy() - .to_string() + WORKING_DIR.to_string_lossy().to_string() +} + +pub fn working_dir() -> &'static Path { + &WORKING_DIR } // Helper to download the zip file content to a file on disk diff --git a/src/util/logger.rs b/src/util/logger.rs index 7fa3947..845489f 100644 --- a/src/util/logger.rs +++ b/src/util/logger.rs @@ -11,6 +11,8 @@ use std::{ use ansi_term::Color; use ttp_core::{CommunicationValue, DataTypes, DataValue}; +use crate::util::file_util::get_directory; + static LOGGER: OnceLock> = OnceLock::new(); #[derive(Clone, Copy)] @@ -38,8 +40,8 @@ pub fn startup() { LOGGER.set(tx).expect("Logger already initialized"); thread::spawn(move || { - let log_dir = Path::new("logs"); - fs::create_dir_all(log_dir).expect("Failed to create log directory"); + let log_dir = Path::new(&get_directory()).join("logs"); + fs::create_dir_all(&log_dir).expect("Failed to create log directory"); let start_ts = SystemTime::now() .duration_since(UNIX_EPOCH)