mod app;
mod config;
mod debug;
mod metrics;
mod sources;
use app::App;
use clap::{CommandFactory, Parser, Subcommand, parser::ValueSource};
use metrics::Sampler;
use std::error::Error;
#[derive(Debug, Subcommand)]
enum Commands {
#[command(alias = "raw")]
Pipe {
#[arg(short, long, default_value_t = 0)]
samples: u32,
#[arg(long, default_value_t = false)]
soc_info: bool,
},
Debug,
}
#[derive(Debug, Parser)]
#[command(version, verbatim_doc_comment)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
#[arg(short, long, global = true, default_value_t = 1000)]
interval: u32,
}
fn main() -> Result<(), Box<dyn Error>> {
let args = Cli::parse();
match &args.command {
Some(Commands::Pipe { samples, soc_info }) => {
let mut sampler = Sampler::new()?;
let mut counter = 0u32;
let soc_info_val = if *soc_info { Some(sampler.get_soc_info().clone()) } else { None };
loop {
let doc = sampler.get_metrics(args.interval.max(100))?;
let mut doc = serde_json::to_value(&doc)?;
if let Some(ref soc) = soc_info_val {
doc["soc"] = serde_json::to_value(soc)?;
}
doc["timestamp"] = serde_json::to_value(chrono::Utc::now().to_rfc3339())?;
let doc = serde_json::to_string(&doc)?;
println!("{}", doc);
counter += 1;
if *samples > 0 && counter >= *samples {
break;
}
}
}
Some(Commands::Debug) => debug::print_debug()?,
_ => {
let mut app = App::new()?;
let matches = Cli::command().get_matches();
let msec = match matches.value_source("interval") {
Some(ValueSource::CommandLine) => Some(args.interval),
_ => None,
};
app.run_loop(msec)?;
}
}
Ok(())
}