Christoffer Martinsson a6c2983f65 Add automatic config file detection for dashboard TUI
- Dashboard now automatically looks for /etc/cm-dashboard/dashboard.toml
- No need to specify --config flag when using standard NixOS deployment
- Fallback to manual config path if default not found
- Update help text to reflect optional config parameter
- Simplifies dashboard usage - just run 'cm-dashboard' without arguments
2025-10-21 22:11:35 +02:00

87 lines
2.2 KiB
Rust

use anyhow::Result;
use clap::Parser;
use tracing::{error, info};
use tracing_subscriber::EnvFilter;
mod app;
mod communication;
mod config;
mod metrics;
mod ui;
use app::Dashboard;
#[derive(Parser)]
#[command(name = "cm-dashboard")]
#[command(about = "CM Dashboard TUI with individual metric consumption")]
#[command(version)]
struct Cli {
/// Increase logging verbosity (-v, -vv)
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
/// Configuration file path (defaults to /etc/cm-dashboard/dashboard.toml)
#[arg(short, long)]
config: Option<String>,
/// Run in headless mode (no TUI, just logging)
#[arg(long)]
headless: bool,
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
// Setup logging - only if headless or verbose
if cli.headless || cli.verbose > 0 {
let log_level = match cli.verbose {
0 => "warn", // Only warnings and errors when not verbose
1 => "info",
2 => "debug",
_ => "trace",
};
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env().add_directive(log_level.parse()?))
.init();
} else {
// No logging output when running TUI mode
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env().add_directive("off".parse()?))
.init();
}
if cli.headless || cli.verbose > 0 {
info!("CM Dashboard starting with individual metrics architecture...");
}
// Create and run dashboard
let mut dashboard = Dashboard::new(cli.config, cli.headless).await?;
// Setup graceful shutdown
let ctrl_c = async {
tokio::signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
// Run dashboard with graceful shutdown
tokio::select! {
result = dashboard.run() => {
if let Err(e) = result {
error!("Dashboard error: {}", e);
return Err(e);
}
}
_ = ctrl_c => {
info!("Shutdown signal received");
}
}
if cli.headless || cli.verbose > 0 {
info!("Dashboard shutdown complete");
}
Ok(())
}