diff --git a/src/main.rs b/src/main.rs index 58986ba..c4fe7d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,10 +30,16 @@ enum Commands { Examples:\n\ - Initialize with default path:\n\ tago-relay init\n\ + - Initialize with verbose mode:\n\ + tago-relay init --verbose info,error\n\ - Initialize with custom path:\n\ tago-relay init --config-path /path/to/config" )] Init { + /// Verbose mode (-v) + #[arg(short, long)] + verbose: Option, + /// Path to the configuration file #[arg(short, long)] config_path: Option, @@ -48,7 +54,7 @@ enum Commands { - Start with default configuration:\n\ tago-relay start\n\ - Start with verbose mode:\n\ - tago-relay start --verbose info,mqtt\n\ + tago-relay start --verbose info,error,mqtt,network\n\ - Start with custom configuration path:\n\ tago-relay start --config-path /path/to/config.toml" )] @@ -63,22 +69,35 @@ enum Commands { }, } +fn init_log_level(verbose: &Option) { + let log_level: String = verbose + .as_ref() + .map(|v| v.to_string()) + .unwrap_or_else(|| "error,info".to_string()); + + env_logger::init_from_env(env_logger::Env::new().default_filter_or(log_level)); +} + #[tokio::main] async fn main() { let cli = Cli::parse(); + // Initialize log level + match &cli.command { + Commands::Init { verbose, .. } => init_log_level(verbose), + Commands::Start { verbose, .. } => init_log_level(verbose), + } match &cli.command { - Commands::Init { config_path } => { + Commands::Init { + verbose: _, + config_path, + } => { init_config(config_path.as_deref()); } - Commands::Start { verbose, config_path } => { - let log_level: String = verbose - .as_ref() - .map(|v| v.to_string()) - .unwrap_or_else(|| "error,info".to_string()); - - env_logger::init_from_env(env_logger::Env::new().default_filter_or(log_level)); - + Commands::Start { + verbose: _, + config_path, + } => { let config = utils::fetch_config_file(config_path.clone()); if let Some(config) = config { *CONFIG_FILE.write().unwrap() = Some(config); diff --git a/src/utils.rs b/src/utils.rs index d1efc81..fb9cfab 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -33,7 +33,11 @@ fn get_config_path(user_path: Option) -> std::path::PathBuf { std::path::PathBuf::from(config_path_str) }); - config_path + if config_path.is_dir() { + config_path.join(".tagoio-mqtt-relay.toml") + } else { + config_path + } } /** @@ -42,7 +46,7 @@ fn get_config_path(user_path: Option) -> std::path::PathBuf { pub fn init_config(user_path: Option>) { let config_path = get_config_path(user_path.map(|s| s.as_ref().to_string())); if config_path.exists() { - log::error!(target: "error", "Configuration file already exists."); + log::error!(target: "error", "Configuration file already exists: {}", config_path.display()); std::process::exit(1); } @@ -52,7 +56,10 @@ pub fn init_config(user_path: Option>) { std::fs::create_dir_all(config_dir).expect("Failed to create config directory"); } - std::fs::write(&config_path, DEFAULT_CONFIG).expect("Failed to create default config file"); + std::fs::write(&config_path, DEFAULT_CONFIG).unwrap_or_else(|err| { + log::error!(target: "error", "Failed to create default config file: {}", err); + std::process::exit(1); + }); log::info!("Configuration file created at {}", config_path.display()); }