use std::process;
use tracing::info;
use tracing_subscriber::{fmt, EnvFilter};
use midas_fetcher::cli::{
handle_auth, handle_cache, handle_download, handle_manifest, Cli, Commands,
};
use midas_fetcher::config::AppConfig;
use midas_fetcher::errors::Result;
#[tokio::main]
async fn main() {
let result = run().await;
if let Err(e) = result {
eprintln!("Error: {}", e);
process::exit(1);
}
}
async fn run() -> Result<()> {
dotenv::dotenv().ok();
let cli = Cli::parse_args();
AppConfig::initialize_first_run().await?;
let _config = AppConfig::load(cli.global.config.clone()).await?;
init_logging(&cli);
info!("MIDAS Fetcher v{} starting", env!("CARGO_PKG_VERSION"));
match cli.command {
Commands::Download(args) => {
info!("Executing download command");
handle_download(args).await
}
Commands::Manifest(args) => {
info!("Executing manifest command");
handle_manifest(args).await
}
Commands::Auth(args) => {
info!("Executing auth command");
handle_auth(args).await
}
Commands::Cache(args) => {
info!("Executing cache command");
handle_cache(args).await
}
}
}
fn init_logging(cli: &Cli) {
let log_level = cli.log_level();
let filter = EnvFilter::from_default_env()
.add_directive(format!("midas_fetcher={}", log_level).parse().unwrap());
fmt()
.with_env_filter(filter)
.with_target(false)
.with_level(cli.global.very_verbose) .init();
if cli.global.very_verbose {
info!("Very verbose logging enabled");
} else if cli.global.verbose {
info!("Verbose logging enabled");
}
}