Compare commits

..

No commits in common. "a33598f0d4156bf4bffccd8e02d24da303e64023" and "11790783fccef0cc40e5b81dded65776c610aeff" have entirely different histories.

2 changed files with 50 additions and 33 deletions

View file

@ -52,6 +52,7 @@ rust-project TODO: write shell script for automatically updating `cargoHash`
(pkgs.rust-bin.stable.latest.default.override { (pkgs.rust-bin.stable.latest.default.override {
extensions = [ "rust-src" ]; extensions = [ "rust-src" ];
}) })
openssl
bacon bacon
]; ];
inputsFrom = with self.packages.${system}; [ status_cloud ]; inputsFrom = with self.packages.${system}; [ status_cloud ];
@ -62,9 +63,6 @@ rust-project TODO: write shell script for automatically updating `cargoHash`
version = "0.1.0"; version = "0.1.0";
buildAndTestSubdir = "status_cloud"; buildAndTestSubdir = "status_cloud";
cargoHash = "sha256-mMxHI/rU1Gd5UR+hZ+5+FnBrff8uF+SrEvGJT7wh5tI="; cargoHash = "sha256-mMxHI/rU1Gd5UR+hZ+5+FnBrff8uF+SrEvGJT7wh5tI=";
preBuild = ''
sed -i 's/Command::new("hddtemp")/${nixpkgs.lib.escape [ "/" ] "Command::new(\"${pkgs.hddtemp}\")"}/g' status_cloud/src/main.rs
'';
meta = meta // { meta = meta // {
description = "Server status saved to a nextcloud note service."; description = "Server status saved to a nextcloud note service.";
}; };
@ -106,7 +104,7 @@ rust-project TODO: write shell script for automatically updating `cargoHash`
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
ExecStart = '' ExecStart = ''
${cfg.package}/bin/status_cloud --config-file ${builtins.toString cfg.config_path} ${cfg.package}/bin/status_cloud --hddtemp-executable ${pkgs.hddtemp}/bin/hddtemp --config-file ${builtins.toString cfg.config_path}
''; '';
}; };
}; };

View file

@ -2,38 +2,42 @@ use clap::Parser;
use log::{debug, error, warn}; use log::{debug, error, warn};
use reqwest::blocking::Client; use reqwest::blocking::Client;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{process::Command, path::PathBuf}; use std::process::Command;
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = CliArgs::parse(); let args = CliArgs::parse();
simplelog::SimpleLogger::init( if args.debug {
match args.debug { let _ = simplelog::SimpleLogger::init(
true => simplelog::LevelFilter::Debug, simplelog::LevelFilter::Debug,
false => simplelog::LevelFilter::Info,
},
simplelog::Config::default(), simplelog::Config::default(),
)?; );
} else {
let _ = simplelog::SimpleLogger::init(
simplelog::LevelFilter::Info,
simplelog::Config::default(),
);
}
debug!("Opening Config file: {}", args.config_file.display()); debug!("Opening Config file: {}", args.config_file);
debug!( debug!(
"Config file exists: {}", "Config file exists: {}",
std::fs::metadata(&args.config_file).is_ok() std::fs::metadata(args.config_file.clone()).is_ok()
); );
let file_contents = match std::fs::read_to_string(&args.config_file) { let file_contents = match std::fs::read_to_string(args.config_file) {
Ok(val) => val, Ok(val) => val,
Err(e) => { Err(e) => {
error!("Could not read config file: {e}"); error!("Could not read config file: {}", e.to_string());
panic!("{e}"); panic!("{}", e);
} }
}; };
let cfg: Config = match toml::from_str(&file_contents) { let cfg: Config = match toml::from_str(file_contents.as_str()) {
Ok(val) => val, Ok(val) => val,
Err(e) => { Err(e) => {
error!("Could not parse config file: {e}"); error!("Could not parse config file: {}", e.to_string());
panic!("{e}"); panic!("{}", e);
} }
}; };
@ -54,8 +58,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
} }
Err(e) => { Err(e) => {
error!("Error opening /dev/: {e}"); error!("Error opening /dev/: {}", e.to_string());
panic!("{e}"); panic!("{}", e);
} }
}; };
@ -63,15 +67,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut drive_temps: Vec<String> = vec![]; let mut drive_temps: Vec<String> = vec![];
for drive in drives { for drive in drives {
let output = match Command::new("hddtemp") let output = match Command::new(args.hddtemp_executable.clone())
.arg("--unit=F") .arg("--unit=F")
.arg(&drive) .arg(drive.clone())
.output() .output()
{ {
Ok(val) => String::from_utf8_lossy(&val.stdout).into_owned(), Ok(val) => String::from_utf8_lossy(&val.stdout).into_owned(),
Err(e) => { Err(e) => {
warn!("Error running hddtemp: {e}"); warn!("Error running hddtemp: {}", e.to_string());
warn!("Drive was: '{drive}'"); warn!("Drive was: '{}'", drive);
"".to_string() "".to_string()
} }
}; };
@ -83,22 +87,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
} }
body_content.push_str("## Hard Drive Temps\n"); body_content.push_str("## Hard Drive Temps\n");
body_content.push_str(&drive_temps.join("\n")); body_content.push_str(drive_temps.join("\n").as_str());
} }
Client::new() Client::new()
.put(format!( .put(format!(
"https://{}/index.php/apps/notes/api/v1/notes/{}", "https://{}/index.php/apps/notes/api/v1/notes/{}",
&cfg.server_url, cfg.server_url.clone(),
&cfg.note_id cfg.note_id.clone()
)) ))
.header("Accept", "application/json") .header("Accept", "application/json")
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.basic_auth(&cfg.user, Some(&cfg.pswd)) .basic_auth(cfg.user.clone(), Some(cfg.pswd.clone()))
.body( .body(
serde_json::to_string(&NoteUpdate { serde_json::to_string(&NoteUpdate {
content: body_content, content: body_content,
})?, })
.unwrap(),
) )
.send()?; .send()?;
@ -122,7 +127,7 @@ struct NoteUpdate {
content: String, content: String,
} }
#[derive(Serialize, Deserialize, Default)] #[derive(Serialize, Deserialize)]
struct Config { struct Config {
user: String, user: String,
pswd: String, pswd: String,
@ -130,12 +135,26 @@ struct Config {
server_url: String, server_url: String,
} }
impl Default for Config {
fn default() -> Self {
Self {
user: "".to_string(),
pswd: "".to_string(),
note_id: "".to_string(),
server_url: "".to_string(),
}
}
}
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about=None)] #[command(author, version, about, long_about=None)]
struct CliArgs { struct CliArgs {
/// Path to config .toml file /// Path to config .toml file
#[arg(short, long)] #[arg(short, long)]
config_file: PathBuf, config_file: String,
#[arg(short, long)]
hddtemp_executable: String,
#[arg(short, long)] #[arg(short, long)]
debug: bool, debug: bool,