From 243063826f9a84b7119c4667bd94172b34b8b4bf Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 26 Aug 2023 11:54:28 -0700 Subject: [PATCH 1/9] Remove all calls to .clone() --- status_cloud/src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index 37b92f2..dea5526 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -22,10 +22,10 @@ fn main() -> Result<(), Box> { debug!("Opening Config file: {}", args.config_file); 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()); @@ -67,9 +67,9 @@ 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(&args.hddtemp_executable) .arg("--unit=F") - .arg(drive.clone()) + .arg(&drive) .output() { Ok(val) => String::from_utf8_lossy(&val.stdout).into_owned(), @@ -93,12 +93,12 @@ fn main() -> Result<(), Box> { 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, From 6576e2f3f9446f1905244051d4a55938c1ebec5c Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 26 Aug 2023 11:55:04 -0700 Subject: [PATCH 2/9] Derive Default for Config --- status_cloud/src/main.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index dea5526..a73f297 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -127,7 +127,7 @@ struct NoteUpdate { content: String, } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Default)] struct Config { user: String, pswd: String, @@ -135,17 +135,6 @@ 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 { From 4bad0eef9b503d4672c576556e31befde29da228 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 26 Aug 2023 11:59:27 -0700 Subject: [PATCH 3/9] Clean format strings --- status_cloud/src/main.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index a73f297..c2c8413 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -28,16 +28,16 @@ fn main() -> Result<(), Box> { 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()) { 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 +58,8 @@ fn main() -> Result<(), Box> { } } Err(e) => { - error!("Error opening /dev/: {}", e.to_string()); - panic!("{}", e); + error!("Error opening /dev/: {e}"); + panic!("{e}"); } }; @@ -74,8 +74,8 @@ fn main() -> Result<(), Box> { { 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() } }; From c0d20759a1ecebc0191dfd4d2837db6bb13eec56 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 26 Aug 2023 12:02:54 -0700 Subject: [PATCH 4/9] Remove unnecessary .as_str() calls --- status_cloud/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index c2c8413..85138b3 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -33,7 +33,7 @@ fn main() -> Result<(), Box> { } }; - 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}"); @@ -87,7 +87,7 @@ 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() From 5d64a8ed1fc4b81f0c34ea42bfad573a9b453d5d Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 26 Aug 2023 12:07:33 -0700 Subject: [PATCH 5/9] Use PathBuf instead of String for CliArgs::config_file --- status_cloud/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index 85138b3..e92f08f 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -2,7 +2,7 @@ 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(); @@ -19,7 +19,7 @@ fn main() -> Result<(), Box> { ); } - 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).is_ok() @@ -140,7 +140,7 @@ struct Config { struct CliArgs { /// Path to config .toml file #[arg(short, long)] - config_file: String, + config_file: PathBuf, #[arg(short, long)] hddtemp_executable: String, From 70505f2078c0d55f0e805a9bb00a731120c997a7 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 26 Aug 2023 12:12:43 -0700 Subject: [PATCH 6/9] Fix logging initialization --- status_cloud/src/main.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index e92f08f..7a49987 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -7,17 +7,13 @@ 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.display()); debug!( From 185c97e30edc5cdd262d201e5d4a9fee1c2b0564 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 26 Aug 2023 12:13:28 -0700 Subject: [PATCH 7/9] Remove unnecessary .unwrap() --- status_cloud/src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index 7a49987..5391d91 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -98,8 +98,7 @@ fn main() -> Result<(), Box> { .body( serde_json::to_string(&NoteUpdate { content: body_content, - }) - .unwrap(), + })?, ) .send()?; From 54c63e8cf4e9500f3e3006ef1049350cac6ee2bc Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 26 Aug 2023 12:30:06 -0700 Subject: [PATCH 8/9] Patch hddtemp Nix store path --- flake.nix | 5 ++++- status_cloud/src/main.rs | 5 +---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/flake.nix b/flake.nix index ae418eb..d91f0a3 100644 --- a/flake.nix +++ b/flake.nix @@ -63,6 +63,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 +107,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 5391d91..f160bb5 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -63,7 +63,7 @@ fn main() -> Result<(), Box> { let mut drive_temps: Vec = vec![]; for drive in drives { - let output = match Command::new(&args.hddtemp_executable) + let output = match Command::new("hddtemp") .arg("--unit=F") .arg(&drive) .output() @@ -137,9 +137,6 @@ struct CliArgs { #[arg(short, long)] config_file: PathBuf, - #[arg(short, long)] - hddtemp_executable: String, - #[arg(short, long)] debug: bool, } From 5ff00ddc06fe816955536e8ba955467c12cb45a4 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 26 Aug 2023 12:31:11 -0700 Subject: [PATCH 9/9] Remove redundant openssl dev shell dependency --- flake.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/flake.nix b/flake.nix index d91f0a3..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 ];