mirror of
https://github.com/qwjyh/xdbm
synced 2024-11-22 14:50:12 +09:00
refactoring: modularize devices
This commit is contained in:
parent
3648a190b7
commit
26a9d91297
1 changed files with 79 additions and 56 deletions
77
src/main.rs
77
src/main.rs
|
@ -14,10 +14,7 @@ use byte_unit::Byte;
|
||||||
use clap::{Parser, Subcommand, ValueEnum};
|
use clap::{Parser, Subcommand, ValueEnum};
|
||||||
use clap_verbosity_flag::Verbosity;
|
use clap_verbosity_flag::Verbosity;
|
||||||
use git2::{Commit, IndexEntry, Oid, Repository};
|
use git2::{Commit, IndexEntry, Oid, Repository};
|
||||||
use inquire::{
|
use inquire::{validator::Validation, Text};
|
||||||
validator::Validation,
|
|
||||||
Text,
|
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_yaml;
|
use serde_yaml;
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -31,7 +28,9 @@ use std::{
|
||||||
io::{self, BufWriter},
|
io::{self, BufWriter},
|
||||||
};
|
};
|
||||||
use std::{fs, io::prelude::*};
|
use std::{fs, io::prelude::*};
|
||||||
use sysinfo::{DiskExt, System, SystemExt};
|
use sysinfo::{DiskExt, SystemExt};
|
||||||
|
|
||||||
|
use devices::{Device, DEVICESFILE};
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
|
@ -75,16 +74,28 @@ enum StorageCommands {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
/// Manipulates each client device.
|
||||||
struct 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,
|
name: String,
|
||||||
os_name: String,
|
os_name: String,
|
||||||
os_version: String,
|
os_version: String,
|
||||||
hostname: String,
|
hostname: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Device {
|
impl Device {
|
||||||
fn new(name: String) -> Device {
|
/// Create new `Device` of name `name`. Additional data is obtained via sysinfo.
|
||||||
|
pub fn new(name: String) -> Device {
|
||||||
let sys = System::new();
|
let sys = System::new();
|
||||||
Device {
|
Device {
|
||||||
name: name,
|
name: name,
|
||||||
|
@ -102,9 +113,13 @@ impl Device {
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const DEVICESFILE: &str = "devices.yml";
|
/// Get name.
|
||||||
|
pub fn name(&self) -> String {
|
||||||
|
self.name.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(ValueEnum, Clone, Copy, Debug)]
|
#[derive(ValueEnum, Clone, Copy, Debug)]
|
||||||
enum StorageType {
|
enum StorageType {
|
||||||
|
@ -166,7 +181,7 @@ impl PhysicalDrivePartition {
|
||||||
capacity: disk.total_space(),
|
capacity: disk.total_space(),
|
||||||
fs: fs.to_string(),
|
fs: fs.to_string(),
|
||||||
is_removable: disk.is_removable(),
|
is_removable: disk.is_removable(),
|
||||||
system_names: HashMap::from([(device.name, alias)]),
|
system_names: HashMap::from([(device.name(), alias)]),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,7 +199,7 @@ impl PhysicalDrivePartition {
|
||||||
None => return Err("Failed to convert storage name to valid str.".to_string()),
|
None => return Err("Failed to convert storage name to valid str.".to_string()),
|
||||||
};
|
};
|
||||||
let mut aliases = self.system_names;
|
let mut aliases = self.system_names;
|
||||||
let _ = match aliases.insert(device.name, alias) {
|
let _ = match aliases.insert(device.name(), alias) {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
None => return Err("Failed to insert alias".to_string()),
|
None => return Err("Failed to insert alias".to_string()),
|
||||||
};
|
};
|
||||||
|
@ -258,7 +273,7 @@ fn main() -> Result<()> {
|
||||||
{
|
{
|
||||||
let f = File::create(devname_path).context("Failed to create devname file")?;
|
let f = File::create(devname_path).context("Failed to create devname file")?;
|
||||||
let writer = BufWriter::new(f);
|
let writer = BufWriter::new(f);
|
||||||
serde_yaml::to_writer(writer, &device.name).unwrap();
|
serde_yaml::to_writer(writer, &device.name()).unwrap();
|
||||||
};
|
};
|
||||||
full_status(&repo)?;
|
full_status(&repo)?;
|
||||||
|
|
||||||
|
@ -269,7 +284,7 @@ fn main() -> Result<()> {
|
||||||
} else {
|
} else {
|
||||||
get_devices(&config_dir)?
|
get_devices(&config_dir)?
|
||||||
};
|
};
|
||||||
if devices.iter().any(|x| x.name == device.name) {
|
if devices.iter().any(|x| x.name() == device.name()) {
|
||||||
return Err(anyhow!("device name is already used."));
|
return Err(anyhow!("device name is already used."));
|
||||||
}
|
}
|
||||||
devices.push(device.clone());
|
devices.push(device.clone());
|
||||||
|
@ -282,10 +297,10 @@ fn main() -> Result<()> {
|
||||||
add_and_commit(
|
add_and_commit(
|
||||||
&repo,
|
&repo,
|
||||||
&Path::new(DEVICESFILE),
|
&Path::new(DEVICESFILE),
|
||||||
&format!("Add new devname: {}", &device.name),
|
&format!("Add new devname: {}", &device.name()),
|
||||||
)?;
|
)?;
|
||||||
full_status(&repo)?;
|
full_status(&repo)?;
|
||||||
},
|
}
|
||||||
Commands::Storage(storage) => match storage.command {
|
Commands::Storage(storage) => match storage.command {
|
||||||
StorageCommands::Add { storage_type } => {
|
StorageCommands::Add { storage_type } => {
|
||||||
trace!("Storage Add {:?}", storage_type);
|
trace!("Storage Add {:?}", storage_type);
|
||||||
|
@ -294,7 +309,8 @@ fn main() -> Result<()> {
|
||||||
match storage_type {
|
match storage_type {
|
||||||
StorageType::Physical => {
|
StorageType::Physical => {
|
||||||
// Get storages
|
// Get storages
|
||||||
let mut storages: Vec<Storage> = if let Some(storages_file) = fs::read_dir(&config_dir)?
|
let mut storages: Vec<Storage> = if let Some(storages_file) =
|
||||||
|
fs::read_dir(&config_dir)?
|
||||||
.filter(|f| {
|
.filter(|f| {
|
||||||
f.as_ref().map_or_else(
|
f.as_ref().map_or_else(
|
||||||
|_e| false,
|
|_e| false,
|
||||||
|
@ -328,7 +344,11 @@ fn main() -> Result<()> {
|
||||||
write_storages(&config_dir, storages)?;
|
write_storages(&config_dir, storages)?;
|
||||||
|
|
||||||
// commit
|
// commit
|
||||||
add_and_commit(&repo, &Path::new(STORAGESFILE), &format!("Add new storage(physical drive): {}", new_storage_name))?;
|
add_and_commit(
|
||||||
|
&repo,
|
||||||
|
&Path::new(STORAGESFILE),
|
||||||
|
&format!("Add new storage(physical drive): {}", new_storage_name),
|
||||||
|
)?;
|
||||||
|
|
||||||
println!("Added new storage.");
|
println!("Added new storage.");
|
||||||
trace!("Finished adding storage");
|
trace!("Finished adding storage");
|
||||||
|
@ -338,10 +358,10 @@ fn main() -> Result<()> {
|
||||||
},
|
},
|
||||||
Commands::Path {} => {
|
Commands::Path {} => {
|
||||||
println!("{}", &config_dir.display());
|
println!("{}", &config_dir.display());
|
||||||
},
|
}
|
||||||
Commands::Sync {} => {
|
Commands::Sync {} => {
|
||||||
unimplemented!("Sync is not implemented")
|
unimplemented!("Sync is not implemented")
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
full_status(&Repository::open(&config_dir)?)?;
|
full_status(&Repository::open(&config_dir)?)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -367,7 +387,7 @@ fn get_device(config_dir: &Path) -> Result<Device> {
|
||||||
trace!("devices: {:?}", devices);
|
trace!("devices: {:?}", devices);
|
||||||
devices
|
devices
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|dev| dev.name == devname)
|
.filter(|dev| dev.name() == devname)
|
||||||
.next()
|
.next()
|
||||||
.context("Couldn't find Device in devices.yml")
|
.context("Couldn't find Device in devices.yml")
|
||||||
}
|
}
|
||||||
|
@ -426,7 +446,10 @@ fn write_devices(config_dir: &Path, devices: Vec<Device>) -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Interactively select physical storage from available disks in sysinfo.
|
/// Interactively select physical storage from available disks in sysinfo.
|
||||||
fn select_physical_storage(device: Device, storages: &Vec<Storage>) -> Result<PhysicalDrivePartition> {
|
fn select_physical_storage(
|
||||||
|
device: Device,
|
||||||
|
storages: &Vec<Storage>,
|
||||||
|
) -> Result<PhysicalDrivePartition> {
|
||||||
trace!("select_physical_storage");
|
trace!("select_physical_storage");
|
||||||
// get disk info fron sysinfo
|
// get disk info fron sysinfo
|
||||||
let mut sys_disks = sysinfo::System::new_all();
|
let mut sys_disks = sysinfo::System::new_all();
|
||||||
|
@ -477,16 +500,16 @@ fn select_physical_storage(device: Device, storages: &Vec<Storage>) -> Result<Ph
|
||||||
trace!("{}", disk_name);
|
trace!("{}", disk_name);
|
||||||
loop {
|
loop {
|
||||||
disk_name = Text::new("Name for the disk:").prompt()?;
|
disk_name = Text::new("Name for the disk:").prompt()?;
|
||||||
if storages.iter().all(|s| {s.name() != &disk_name}) {
|
if storages.iter().all(|s| s.name() != &disk_name) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
println!("The name {} is already used.", disk_name);
|
println!("The name {} is already used.", disk_name);
|
||||||
};
|
}
|
||||||
trace!("selected name: {}", disk_name);
|
trace!("selected name: {}", disk_name);
|
||||||
PhysicalDrivePartition::try_from_sysinfo_disk(disk, disk_name, device)
|
PhysicalDrivePartition::try_from_sysinfo_disk(disk, disk_name, device)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get Vec<Storage> from devices.yml([DEVICESFILE])
|
/// Get `Vec<Storage>` from devices.yml([DEVICESFILE])
|
||||||
fn get_storages(config_dir: &Path) -> Result<Vec<Storage>> {
|
fn get_storages(config_dir: &Path) -> Result<Vec<Storage>> {
|
||||||
let f = File::open(config_dir.join(STORAGESFILE))?;
|
let f = File::open(config_dir.join(STORAGESFILE))?;
|
||||||
let reader = BufReader::new(f);
|
let reader = BufReader::new(f);
|
||||||
|
|
Loading…
Reference in a new issue