use carrot_utils::logger;
use clap::ArgAction;
use clap::Parser;
use log::{info, LevelFilter};
use std::fs;
use std::io::{Read, Write};
use nvidia_checker::NvidiaEnvironment;
#[derive(Parser, Debug)]
struct Args {
#[clap(long, action=ArgAction::SetTrue)]
latest: bool,
#[clap(long, default_value = None)]
diff: Option<String>,
#[clap(long, default_value = "Info")]
log_level: String,
}
fn main() {
let args = Args::parse();
let log_level: LevelFilter = logger::get_log_level(&args.log_level);
let _logger = logger::Logger::new(
"./data/{TIME_SEC}",
"log_{TIME_SEC}.txt",
log_level,
log_level,
LevelFilter::Debug,
);
let home_path = dirs::home_dir().unwrap();
let directory_path = home_path.join(".local/share/nvidia-checker/");
fs::create_dir_all(&directory_path).unwrap();
let latest_file_path = directory_path.join("latest.toml");
info!("===== Start nvidia-checker =====");
let now_environment = NvidiaEnvironment::init();
info!("===== Your environment =====\n{:#?}", &now_environment,);
if args.latest {
let mut latest_config_str = String::new();
fs::File::open(&latest_file_path)
.and_then(|mut f| f.read_to_string(&mut latest_config_str))
.unwrap();
let latest_environment: NvidiaEnvironment = toml::from_str(&latest_config_str).unwrap();
println!("Before checked at {}", &latest_environment.checked_time);
now_environment.print_check_results(&latest_environment);
}
if args.diff.is_some() {
let mut target_environment_str = String::new();
fs::File::open(&args.diff.unwrap())
.and_then(|mut f| f.read_to_string(&mut target_environment_str))
.unwrap();
let target_environment: NvidiaEnvironment =
toml::from_str(&target_environment_str).unwrap();
now_environment.print_check_results(&target_environment);
}
let mut latest_environment_file = fs::File::create(&latest_file_path).unwrap();
let toml = toml::to_string(&now_environment).unwrap();
write!(latest_environment_file, "{}", toml).unwrap();
latest_environment_file.flush().unwrap();
}