diff --git a/flake.nix b/flake.nix index ae418eb..86e058a 100644 --- a/flake.nix +++ b/flake.nix @@ -52,7 +52,6 @@ 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 ]; @@ -63,6 +62,9 @@ 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."; }; @@ -104,7 +106,7 @@ rust-project TODO: write shell script for automatically updating `cargoHash` serviceConfig = { Type = "oneshot"; ExecStart = '' - ${cfg.package}/bin/status_cloud --hddtemp-executable ${pkgs.hddtemp}/bin/hddtemp --config-file ${builtins.toString cfg.config_path} + ${cfg.package}/bin/status_cloud --config-file ${builtins.toString cfg.config_path} ''; }; }; diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index 37b92f2..f160bb5 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -2,42 +2,38 @@ use clap::Parser; use log::{debug, error, warn}; use reqwest::blocking::Client; use serde::{Deserialize, Serialize}; -use std::process::Command; +use std::{process::Command, path::PathBuf}; fn main() -> Result<(), Box> { let args = CliArgs::parse(); - if args.debug { - let _ = simplelog::SimpleLogger::init( - simplelog::LevelFilter::Debug, - simplelog::Config::default(), - ); - } else { - let _ = simplelog::SimpleLogger::init( - simplelog::LevelFilter::Info, - simplelog::Config::default(), - ); - } + simplelog::SimpleLogger::init( + match args.debug { + true => simplelog::LevelFilter::Debug, + false => simplelog::LevelFilter::Info, + }, + simplelog::Config::default(), + )?; - debug!("Opening Config file: {}", args.config_file); + debug!("Opening Config file: {}", args.config_file.display()); debug!( "Config file exists: {}", - std::fs::metadata(args.config_file.clone()).is_ok() + std::fs::metadata(&args.config_file).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.to_string()); - panic!("{}", e); + error!("Could not read config file: {e}"); + panic!("{e}"); } }; - let cfg: Config = match toml::from_str(file_contents.as_str()) { + let cfg: Config = match toml::from_str(&file_contents) { Ok(val) => val, Err(e) => { - error!("Could not parse config file: {}", e.to_string()); - panic!("{}", e); + error!("Could not parse config file: {e}"); + panic!("{e}"); } }; @@ -58,8 +54,8 @@ fn main() -> Result<(), Box> { } } Err(e) => { - error!("Error opening /dev/: {}", e.to_string()); - panic!("{}", e); + error!("Error opening /dev/: {e}"); + panic!("{e}"); } }; @@ -67,15 +63,15 @@ fn main() -> Result<(), Box> { let mut drive_temps: Vec = vec![]; for drive in drives { - let output = match Command::new(args.hddtemp_executable.clone()) + let output = match Command::new("hddtemp") .arg("--unit=F") - .arg(drive.clone()) + .arg(&drive) .output() { Ok(val) => String::from_utf8_lossy(&val.stdout).into_owned(), Err(e) => { - warn!("Error running hddtemp: {}", e.to_string()); - warn!("Drive was: '{}'", drive); + warn!("Error running hddtemp: {e}"); + warn!("Drive was: '{drive}'"); "".to_string() } }; @@ -87,23 +83,22 @@ fn main() -> Result<(), Box> { } } body_content.push_str("## Hard Drive Temps\n"); - body_content.push_str(drive_temps.join("\n").as_str()); + body_content.push_str(&drive_temps.join("\n")); } Client::new() .put(format!( "https://{}/index.php/apps/notes/api/v1/notes/{}", - cfg.server_url.clone(), - cfg.note_id.clone() + &cfg.server_url, + &cfg.note_id )) .header("Accept", "application/json") .header("Content-Type", "application/json") - .basic_auth(cfg.user.clone(), Some(cfg.pswd.clone())) + .basic_auth(&cfg.user, Some(&cfg.pswd)) .body( serde_json::to_string(&NoteUpdate { content: body_content, - }) - .unwrap(), + })?, ) .send()?; @@ -127,7 +122,7 @@ struct NoteUpdate { content: String, } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Default)] struct Config { user: String, pswd: String, @@ -135,26 +130,12 @@ 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: String, - - #[arg(short, long)] - hddtemp_executable: String, + config_file: PathBuf, #[arg(short, long)] debug: bool,