metasearch/src/main.rs

78 lines
1.8 KiB
Rust
Raw Normal View History

2024-07-01 04:04:04 +00:00
use std::{
env,
path::{Path, PathBuf},
};
2024-04-18 04:41:26 +00:00
2024-04-13 02:17:34 +00:00
use config::Config;
2024-04-16 00:19:09 +00:00
use tracing::error;
2024-04-13 02:17:34 +00:00
pub mod config;
2023-12-20 06:18:09 +00:00
pub mod engines;
pub mod normalize;
pub mod parse;
pub mod web;
#[tokio::main(flavor = "current_thread")]
2023-12-20 06:18:09 +00:00
async fn main() {
2024-04-16 00:19:09 +00:00
tracing_subscriber::fmt::init();
2024-07-01 04:04:04 +00:00
if env::args().any(|arg| arg == "--help" || arg == "-h" || arg == "help" || arg == "h") {
println!("Usage: metasearch [config_path]");
return;
}
2024-04-18 04:41:26 +00:00
2024-07-01 04:04:04 +00:00
let config_path = config_path();
let config = match Config::read_or_create(&config_path) {
2024-04-13 02:17:34 +00:00
Ok(config) => config,
Err(err) => {
2024-04-16 00:19:09 +00:00
error!("Couldn't parse config:\n{err}");
2024-04-13 02:17:34 +00:00
return;
}
};
web::run(config).await;
2023-12-20 06:18:09 +00:00
}
2024-07-01 04:04:04 +00:00
fn config_path() -> PathBuf {
if let Some(config_path) = env::args().nth(1) {
return PathBuf::from(config_path);
}
let app_name = env!("CARGO_PKG_NAME");
let mut default_config_dir = None;
// $XDG_CONFIG_HOME/metasearch/config.toml
if let Ok(xdg_config_home) = env::var("XDG_CONFIG_HOME") {
let path = PathBuf::from(xdg_config_home)
.join(app_name)
.join("config.toml");
if path.is_file() {
return path;
}
if default_config_dir.is_none() {
default_config_dir = Some(path);
}
}
// $HOME/.config/metasearch/config.toml
if let Ok(home) = env::var("HOME") {
let path = PathBuf::from(home)
.join(".config")
.join(app_name)
.join("config.toml");
if path.is_file() {
return path;
}
if default_config_dir.is_none() {
default_config_dir = Some(path);
}
}
// ./config.toml
let path = Path::new("config.toml");
if path.exists() {
return path.to_path_buf();
}
default_config_dir.unwrap_or(PathBuf::from("config.toml"))
}