#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]
use clap::Parser;
use mho::cli::Cli;
use mho::cli::commands::handle;
use mho::error::Result;
use mho::log::{LogConfig, init_logger};
use mho::{log_error, log_info};
#[cfg(windows)]
fn attach_console() {
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
use windows_sys::Win32::System::Console::{
ATTACH_PARENT_PROCESS, AttachConsole, GetStdHandle, STD_ERROR_HANDLE, STD_OUTPUT_HANDLE,
SetStdHandle,
};
unsafe {
if AttachConsole(ATTACH_PARENT_PROCESS) == 0 {
return;
}
let stdout = GetStdHandle(STD_OUTPUT_HANDLE);
let stderr = GetStdHandle(STD_ERROR_HANDLE);
if stdout != INVALID_HANDLE_VALUE {
SetStdHandle(STD_OUTPUT_HANDLE, stdout);
}
if stderr != INVALID_HANDLE_VALUE {
SetStdHandle(STD_ERROR_HANDLE, stderr);
}
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
if let Err(e) = init_logging() {
eprintln!("Failed to initialize logging: {}", e);
}
log_info!("Mho application starting");
if let Err(e) = run().await {
log_error!("Application error: {}", e);
eprintln!("Error: {}", e);
std::process::exit(1);
}
log_info!("Mho application exiting");
}
fn init_logging() -> Result<()> {
let config = LogConfig::default();
init_logger(config)?;
Ok(())
}
async fn run() -> Result<()> {
#[cfg(windows)]
{
if std::env::args().find(|i| i == "--schedule").is_none() {
attach_console();
}
}
let cli = Cli::parse();
log_info!("Parsed CLI arguments");
handle(cli.command).await?;
Ok(())
}