use crate::libs::{config::Config, daemon, messages::Message, monitor::Monitor};
use crate::msg_print;
use anyhow::Result;
use clap::Args;
use tracing::instrument;
#[derive(Debug, Args)]
pub struct WatchArgs {
#[arg(long)]
foreground: bool,
#[arg(long, short)]
stop: bool,
}
#[instrument]
pub async fn cmd(args: WatchArgs) -> Result<()> {
if args.stop {
daemon::stop()?;
} else if args.foreground {
msg_print!(Message::WatcherStartingForeground);
run_monitor().await?;
} else {
daemon::spawn()?;
}
Ok(())
}
#[instrument]
async fn run_monitor() -> Result<()> {
let config = Config::read()?;
let monitor_config = config.monitor.unwrap_or_default();
let mut monitor = Monitor::new(monitor_config)?;
let inbox_handle = tokio::spawn(async move {
crate::libs::jira_inbox::run_poller().await;
});
let result = monitor.run().await;
inbox_handle.abort();
result
}
#[instrument]
pub async fn run_as_daemon() -> Result<()> {
daemon::run_with_signal_handling().await
}