From 1cb86594125fdcc81c2d754935893bd0210e2482 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Tue, 29 Aug 2023 01:30:24 +0900 Subject: [PATCH] refactoring: separate file for devices --- src/devices.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 52 +++++----------------------------------------- 2 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 src/devices.rs diff --git a/src/devices.rs b/src/devices.rs new file mode 100644 index 0000000..e308929 --- /dev/null +++ b/src/devices.rs @@ -0,0 +1,56 @@ +//! Manipulates each client device. + +use serde::{Deserialize, Serialize}; +use sysinfo::{System, SystemExt}; + +/// YAML file to store known devices. +pub const DEVICESFILE: &str = "devices.yml"; + +/// Represents each devices. +/// Identified by name, which is accessible from `name()`. +/// Store os name, os version and hostname as supplimental information. +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Device { + name: String, + os_name: String, + os_version: String, + hostname: String, +} + +impl Device { + /// Create new `Device` of name `name`. Additional data is obtained via sysinfo. + pub fn new(name: String) -> Device { + let sys = System::new(); + Device { + name: name, + os_name: sys.name().unwrap_or_else(|| { + warn!("Failed to get OS name. Saving as \"unknown\"."); + "unknown".to_string() + }), + os_version: sys.os_version().unwrap_or_else(|| { + warn!("Failed to get OS version. Saving as \"unknown\"."); + "unknown".to_string() + }), + hostname: sys.host_name().unwrap_or_else(|| { + warn!("Failed to get hostname. Saving as \"unknown\"."); + "unknown".to_string() + }), + } + } + + /// Get name. + pub fn name(&self) -> String { + self.name.to_string() + } +} + +#[cfg(test)] +mod tests { + use super::Device; + + #[test] + fn get_name() { + let device = Device::new("test".to_string()); + assert_eq!("test".to_string(), device.name()); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 79acdd2..1964218 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,52 +74,7 @@ enum StorageCommands { }, } -/// Manipulates each client device. -mod devices { - - use serde::{Deserialize, Serialize}; - use sysinfo::{System, SystemExt}; - - /// YAML file to store known devices. - pub const DEVICESFILE: &str = "devices.yml"; - - /// Represents each devices. - /// Identified by name, which is accessible from `name()`. - #[derive(Serialize, Deserialize, Debug, Clone)] - pub struct Device { - name: String, - os_name: String, - os_version: String, - hostname: String, - } - - impl Device { - /// Create new `Device` of name `name`. Additional data is obtained via sysinfo. - pub fn new(name: String) -> Device { - let sys = System::new(); - Device { - name: name, - os_name: sys.name().unwrap_or_else(|| { - warn!("Failed to get OS name. Saving as \"unknown\"."); - "unknown".to_string() - }), - os_version: sys.os_version().unwrap_or_else(|| { - warn!("Failed to get OS version. Saving as \"unknown\"."); - "unknown".to_string() - }), - hostname: sys.host_name().unwrap_or_else(|| { - warn!("Failed to get hostname. Saving as \"unknown\"."); - "unknown".to_string() - }), - } - } - - /// Get name. - pub fn name(&self) -> String { - self.name.to_string() - } - } -} +mod devices; #[derive(ValueEnum, Clone, Copy, Debug)] enum StorageType { @@ -299,12 +254,15 @@ fn main() -> Result<()> { &Path::new(DEVICESFILE), &format!("Add new devname: {}", &device.name()), )?; + println!("Device added"); full_status(&repo)?; } Commands::Storage(storage) => match storage.command { StorageCommands::Add { storage_type } => { trace!("Storage Add {:?}", storage_type); - let repo = Repository::open(&config_dir)?; + let repo = Repository::open(&config_dir).context( + "Repository doesn't exist. Please run init to initialize the repository.", + )?; trace!("repo state: {:?}", repo.state()); match storage_type { StorageType::Physical => {