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 {
extensions = [ "rust-src" ];
})
openssl
bacon
];
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";
buildAndTestSubdir = "status_cloud";
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 // {
description = "Server status saved to a nextcloud note service.";
};
@ -106,7 +104,7 @@ rust-project TODO: write shell script for automatically updating `cargoHash`
serviceConfig = {
Type = "oneshot";
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 reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use std::{process::Command, path::PathBuf};
use std::process::Command;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = CliArgs::parse();
simplelog::SimpleLogger::init(
match args.debug {
true => simplelog::LevelFilter::Debug,
false => simplelog::LevelFilter::Info,
},
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.display());
debug!("Opening Config file: {}", args.config_file);
debug!(
"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,
Err(e) => {
error!("Could not read config file: {e}");
panic!("{e}");
error!("Could not read config file: {}", e.to_string());
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,
Err(e) => {
error!("Could not parse config file: {e}");
panic!("{e}");
error!("Could not parse config file: {}", e.to_string());
panic!("{}", e);
}
};
@ -54,8 +58,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
Err(e) => {
error!("Error opening /dev/: {e}");
panic!("{e}");
error!("Error opening /dev/: {}", e.to_string());
panic!("{}", e);
}
};
@ -63,15 +67,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut drive_temps: Vec<String> = vec![];
for drive in drives {
let output = match Command::new("hddtemp")
let output = match Command::new(args.hddtemp_executable.clone())
.arg("--unit=F")
.arg(&drive)
.arg(drive.clone())
.output()
{
Ok(val) => String::from_utf8_lossy(&val.stdout).into_owned(),
Err(e) => {
warn!("Error running hddtemp: {e}");
warn!("Drive was: '{drive}'");
warn!("Error running hddtemp: {}", e.to_string());
warn!("Drive was: '{}'", drive);
"".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(&drive_temps.join("\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,
&cfg.note_id
cfg.server_url.clone(),
cfg.note_id.clone()
))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.basic_auth(&cfg.user, Some(&cfg.pswd))
.basic_auth(cfg.user.clone(), Some(cfg.pswd.clone()))
.body(
serde_json::to_string(&NoteUpdate {
content: body_content,
})?,
})
.unwrap(),
)
.send()?;
@ -122,7 +127,7 @@ struct NoteUpdate {
content: String,
}
#[derive(Serialize, Deserialize, Default)]
#[derive(Serialize, Deserialize)]
struct Config {
user: String,
pswd: String,
@ -130,12 +135,26 @@ struct Config {
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)]
#[command(author, version, about, long_about=None)]
struct CliArgs {
/// Path to config .toml file
#[arg(short, long)]
config_file: PathBuf,
config_file: String,
#[arg(short, long)]
hddtemp_executable: String,
#[arg(short, long)]
debug: bool,