@@ -2,7 +2,7 @@ mod config;
22mod i18n;
33mod utils;
44
5- use std:: collections:: HashMap ;
5+ use std:: { collections:: HashMap , path :: PathBuf } ;
66
77use config:: { init_config, print_config} ;
88use i18n:: t;
@@ -15,12 +15,12 @@ use utils::{
1515 save_report:: save_report_markdown,
1616} ;
1717
18- fn run ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
18+ fn run ( root_path : PathBuf ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
1919 // Initialize global configuration.
2020 // After initialization, the config is stored in a global OnceLock singleton,
2121 // and can be accessed from other modules via `config::store::global()`,
2222 // such as in the i18n module.
23- let config = init_config ( ) ?;
23+ let config = init_config ( & root_path ) ?;
2424
2525 print_config ( ) ;
2626
@@ -68,8 +68,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
6868 . push ( log_info) ;
6969 }
7070
71- // save_report_markdown(&result, "report.txt").expect(t("report_gen_error"));
72- save_report_markdown ( & result, "report.txt" ) ?;
71+ save_report_markdown ( & result, & root_path) ?;
7372
7473 exit_on_keypress ( Some ( t ( "press_to_exit" ) ) ) ;
7574 }
@@ -78,13 +77,24 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
7877}
7978
8079fn main ( ) {
81- // Print the current working directory
82- // When the program crashes, it can help locate the cause
80+ // Get the APP_ENV environment variable from the startup command (see Makefile.toml).
81+ // Since the working directories differ between development and production,
82+ // we need to handle them separately.
83+ let env = std:: env:: var ( "APP_ENV" ) . unwrap_or_else ( |_| "production" . to_string ( ) ) ;
84+
85+ let root_path = if env == "development" {
86+ std:: env:: current_dir ( ) . unwrap ( )
87+ } else {
88+ std:: env:: current_exe ( )
89+ . unwrap ( )
90+ . parent ( )
91+ . unwrap ( )
92+ . to_path_buf ( )
93+ } ;
94+
95+ // Print the root path to help locate issues if the program crashes.
8396 println ! ( "" ) ;
84- println ! (
85- "Current working directory: \n {}" ,
86- & std:: env:: current_dir( ) . unwrap( ) . display( )
87- ) ;
97+ println ! ( "\n Program root directory:\n {}" , root_path. display( ) ) ;
8898
8999 // Using `?` to propagate errors to `main` causes Rust's default error output
90100 // (via the `std::process::Termination` trait) to use the Debug format (`{:?}`)
@@ -93,7 +103,7 @@ fn main() {
93103 //
94104 // Wrapping the main logic in a separate `run` function and handling errors
95105 // explicitly with `eprintln!` ensures that the Display output is used.
96- if let Err ( e) = run ( ) {
106+ if let Err ( e) = run ( root_path ) {
97107 eprintln ! ( "{}" , e) ; // Use Display format to output the error
98108 std:: process:: exit ( 1 ) ;
99109 }
0 commit comments