mirror of
https://github.com/qwjyh/xdbm
synced 2024-11-25 08:01:04 +09:00
refactoring: separate file for devices
This commit is contained in:
parent
26a9d91297
commit
1cb8659412
2 changed files with 61 additions and 47 deletions
56
src/devices.rs
Normal file
56
src/devices.rs
Normal file
|
@ -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());
|
||||
}
|
||||
}
|
52
src/main.rs
52
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 => {
|
||||
|
|
Loading…
Reference in a new issue