Initial commit
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
||||
use clap::Parser;
|
||||
use core::panic;
|
||||
use json::JsonValue;
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
let args = Cli::parse();
|
||||
if args.query == "cpu" {
|
||||
let sensors = get_json_command_result(Command::new("sensors").arg("-j"));
|
||||
let cpu_temp = &sensors["k10temp-pci-00c3"]["Tctl"]["temp1_input"];
|
||||
let cpu_temp = json_to_int_value(cpu_temp);
|
||||
println!("{}", cpu_temp);
|
||||
} else {
|
||||
let gpu_temp = get_string_command_result(
|
||||
Command::new("nvidia-smi")
|
||||
.arg("--query-gpu=temperature.gpu")
|
||||
.arg("--format=csv,noheader"),
|
||||
);
|
||||
let gpu_temp = str_to_int_value(gpu_temp);
|
||||
|
||||
println!("{}", gpu_temp);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Cli {
|
||||
query: String,
|
||||
}
|
||||
|
||||
fn json_to_int_value(number: &JsonValue) -> i64 {
|
||||
return match number {
|
||||
JsonValue::Number(_) => {
|
||||
return match number.as_f64() {
|
||||
Some(x) => x as i64,
|
||||
None => -1,
|
||||
}
|
||||
}
|
||||
_ => -2,
|
||||
};
|
||||
}
|
||||
|
||||
fn str_to_int_value(number: Option<String>) -> i64 {
|
||||
return match number {
|
||||
Some(x) => {
|
||||
return match x.trim().parse::<i64>() {
|
||||
Ok(y) => y,
|
||||
Err(y) => panic!("{}", y),
|
||||
};
|
||||
}
|
||||
None => -1,
|
||||
};
|
||||
}
|
||||
|
||||
fn get_string_command_result(command: &mut Command) -> Option<String> {
|
||||
let command_result = command.output();
|
||||
return match command_result {
|
||||
Ok(x) => {
|
||||
let status = String::from_utf8(x.stdout);
|
||||
return match status {
|
||||
Ok(x) => Some(x),
|
||||
Err(_) => None,
|
||||
};
|
||||
}
|
||||
Err(_) => None,
|
||||
};
|
||||
}
|
||||
|
||||
fn get_json_command_result(command: &mut Command) -> JsonValue {
|
||||
let string_command_result = get_string_command_result(command);
|
||||
return match string_command_result {
|
||||
Some(x) => {
|
||||
return match json::parse(x.as_str()) {
|
||||
Ok(x) => x,
|
||||
Err(_) => JsonValue::new_object(),
|
||||
};
|
||||
}
|
||||
None => JsonValue::new_object(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user