Skip to content

Commit

Permalink
feat: support dir in config path
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorfdl committed Jun 19, 2024
1 parent 79f3bd0 commit 6ef8d57
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
39 changes: 29 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// Path to the configuration file
#[arg(short, long)]
config_path: Option<String>,
Expand All @@ -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"
)]
Expand All @@ -63,22 +69,35 @@ enum Commands {
},
}

fn init_log_level(verbose: &Option<String>) {
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);
Expand Down
13 changes: 10 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ fn get_config_path(user_path: Option<String>) -> 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
}
}

/**
Expand All @@ -42,7 +46,7 @@ fn get_config_path(user_path: Option<String>) -> std::path::PathBuf {
pub fn init_config(user_path: Option<impl AsRef<str>>) {
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);
}

Expand All @@ -52,7 +56,10 @@ pub fn init_config(user_path: Option<impl AsRef<str>>) {
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());
}
Expand Down

0 comments on commit 6ef8d57

Please sign in to comment.