use anyhow::Result;
use clap::{Parser, Subcommand};
use darpan::core::engine::CoreEngine;
use darpan::ui::cli::CliInterface;
use darpan::ui::tui::TuiInterface;
use std::path::PathBuf;
use tracing_subscriber;
#[derive(Parser)]
#[command(name = "darpan")]
#[command(author, version, about, long_about = None)]
#[command(about = "A Linux developer service monitoring utility")]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
#[arg(short, long)]
path: Option<PathBuf>,
#[arg(short, long)]
verbose: bool,
}
#[derive(Subcommand)]
enum Commands {
Status {
#[arg(short, long)]
all: bool,
#[arg(short, long, default_value = "text")]
format: String,
},
Why {
service: String,
},
Init,
Add {
service_type: String,
#[arg(short, long)]
port: Option<u16>,
#[arg(short, long)]
name: Option<String>,
},
Watch,
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
let is_watch = matches!(cli.command, Some(Commands::Watch));
if !is_watch {
if cli.verbose {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
} else {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
}
}
let project_path = cli.path.unwrap_or_else(|| std::env::current_dir().unwrap());
let mut engine = CoreEngine::new(project_path)?;
match cli.command {
Some(Commands::Status { all, format }) => {
let cli_ui = CliInterface::new();
cli_ui.show_status(&mut engine, all, &format).await?;
}
Some(Commands::Why { service }) => {
let cli_ui = CliInterface::new();
cli_ui.show_why(&mut engine, &service).await?;
}
Some(Commands::Init) => {
let cli_ui = CliInterface::new();
cli_ui.init_config().await?;
}
Some(Commands::Add {
service_type,
port,
name,
}) => {
let cli_ui = CliInterface::new();
cli_ui.add_service(&service_type, port, name).await?;
}
Some(Commands::Watch) => {
let mut tui = TuiInterface::new()?;
tui.run(&mut engine).await?;
}
None => {
let cli_ui = CliInterface::new();
cli_ui.show_status(&mut engine, false, "text").await?;
}
}
Ok(())
}