|
|
|
@ -1,71 +1,65 @@
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use reqwest::blocking::Client;
|
|
|
|
|
|
|
|
|
|
use toml;
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
|
|
|
|
|
use log::{debug, error, warn};
|
|
|
|
|
use reqwest::blocking::Client;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: logging
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
|
|
|
|
|
let args = CliArgs::parse();
|
|
|
|
|
|
|
|
|
|
println!("Opening Config file: {}", args.config_file);
|
|
|
|
|
println!("File exists: {}", std::fs::metadata(args.config_file.clone()).is_ok());
|
|
|
|
|
if args.debug {
|
|
|
|
|
let _ = simplelog::SimpleLogger::init(
|
|
|
|
|
simplelog::LevelFilter::Debug,
|
|
|
|
|
simplelog::Config::default(),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
let _ = simplelog::SimpleLogger::init(
|
|
|
|
|
simplelog::LevelFilter::Info,
|
|
|
|
|
simplelog::Config::default(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug!("Opening Config file: {}", args.config_file);
|
|
|
|
|
debug!(
|
|
|
|
|
"Config file exists: {}",
|
|
|
|
|
std::fs::metadata(args.config_file.clone()).is_ok()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let file_contents = match std::fs::read_to_string(args.config_file) {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("Could not read file: {}", e.to_string());
|
|
|
|
|
return Ok(());
|
|
|
|
|
error!("Could not read config file: {}", e.to_string());
|
|
|
|
|
panic!("{}", e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let cfg: Config = match toml::from_str(file_contents.as_str()) {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("Could not parse file: {}", e.to_string());
|
|
|
|
|
return Ok(());
|
|
|
|
|
error!("Could not parse config file: {}", e.to_string());
|
|
|
|
|
panic!("{}", e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut body_content: String = format!("*Last Updated:* {} \n", chrono::Local::now().format("%D - %H:%M:%S"));
|
|
|
|
|
let mut body_content: String = format!(
|
|
|
|
|
"*Last Updated:* {} \n",
|
|
|
|
|
chrono::Local::now().format("%D - %H:%M:%S")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut drives: Vec<String> = vec![];
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let rust_drives = match std::fs::read_dir("/dev/") {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("Error opening /dev/: {}", e.to_string());
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for path in rust_drives {
|
|
|
|
|
match path {
|
|
|
|
|
Ok(ref val) => {
|
|
|
|
|
let tmp = val.path().to_string_lossy().to_string();
|
|
|
|
|
if tmp.starts_with("/dev/sd") {
|
|
|
|
|
if tmp.len() == 8 {
|
|
|
|
|
drives.push(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
match std::fs::read_dir("/dev/") {
|
|
|
|
|
Ok(paths) => {
|
|
|
|
|
for path in paths.flatten() {
|
|
|
|
|
let tmp = path.path().to_string_lossy().to_string();
|
|
|
|
|
if tmp.starts_with("/dev/sd") && tmp.len() == 8 {
|
|
|
|
|
drives.push(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match Command::new(args.hddtemp_executable.clone()).output() {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("Error running hddtemp at all: {}", e.to_string());
|
|
|
|
|
println!("hddtemp = {}", args.hddtemp_executable.clone());
|
|
|
|
|
return Ok(());
|
|
|
|
|
error!("Error opening /dev/: {}", e.to_string());
|
|
|
|
|
panic!("{}", e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -73,46 +67,49 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
let mut drive_temps: Vec<String> = vec![];
|
|
|
|
|
|
|
|
|
|
for drive in drives {
|
|
|
|
|
let output = match Command::new(args.hddtemp_executable.clone()).arg(drive.clone()).output() {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
let output = match Command::new(args.hddtemp_executable.clone())
|
|
|
|
|
.arg("--unit=F")
|
|
|
|
|
.arg(drive.clone())
|
|
|
|
|
.output()
|
|
|
|
|
{
|
|
|
|
|
Ok(val) => String::from_utf8_lossy(&val.stdout).into_owned(),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("Error running hddtemp: {}", e.to_string());
|
|
|
|
|
println!("Drive was: '{}'", drive);
|
|
|
|
|
return Ok(());
|
|
|
|
|
warn!("Error running hddtemp: {}", e.to_string());
|
|
|
|
|
warn!("Drive was: '{}'", drive);
|
|
|
|
|
"".to_string()
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let tmp = match std::str::from_utf8(&output.stdout) {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("Error parsing hddtemp output: {}", e.to_string());
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
}.to_string();
|
|
|
|
|
|
|
|
|
|
if !tmp.contains("sensor") {
|
|
|
|
|
drive_temps.push(tmp.replace("\n", "Not running as sudo?"));
|
|
|
|
|
if !output.contains("sensor") {
|
|
|
|
|
drive_temps.push(output.replace('\n', ""));
|
|
|
|
|
} else {
|
|
|
|
|
drive_temps.push(tmp[0..9].to_string() + " No Sensor");
|
|
|
|
|
drive_temps.push(output[0..9].to_string() + " No Sensor");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
body_content.push_str("## Hard Drive Temps\n");
|
|
|
|
|
body_content.push_str(drive_temps.join("\n").as_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Client::new()
|
|
|
|
|
.put(format!("https://{}/index.php/apps/notes/api/v1/notes/{}", cfg.server_url.clone(), cfg.note_id.clone()))
|
|
|
|
|
.put(format!(
|
|
|
|
|
"https://{}/index.php/apps/notes/api/v1/notes/{}",
|
|
|
|
|
cfg.server_url.clone(),
|
|
|
|
|
cfg.note_id.clone()
|
|
|
|
|
))
|
|
|
|
|
.header("Accept", "application/json")
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
|
.basic_auth(cfg.user.clone(), Some(cfg.pswd.clone()))
|
|
|
|
|
.body(serde_json::to_string(&NoteUpdate {content: body_content}).unwrap())
|
|
|
|
|
.send()?;
|
|
|
|
|
.body(
|
|
|
|
|
serde_json::to_string(&NoteUpdate {
|
|
|
|
|
content: body_content,
|
|
|
|
|
})
|
|
|
|
|
.unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.send()?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
|
struct Note {
|
|
|
|
|
id: usize,
|
|
|
|
@ -122,7 +119,7 @@ struct Note {
|
|
|
|
|
title: String,
|
|
|
|
|
category: String,
|
|
|
|
|
content: String,
|
|
|
|
|
favorite: bool
|
|
|
|
|
favorite: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
@ -135,7 +132,7 @@ struct Config {
|
|
|
|
|
user: String,
|
|
|
|
|
pswd: String,
|
|
|
|
|
note_id: String,
|
|
|
|
|
server_url: String
|
|
|
|
|
server_url: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
@ -158,4 +155,7 @@ struct CliArgs {
|
|
|
|
|
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
hddtemp_executable: String,
|
|
|
|
|
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
debug: bool,
|
|
|
|
|
}
|
|
|
|
|