mirror of
https://github.com/qwjyh/xdbm
synced 2024-11-22 14:50:12 +09:00
refactoring: separate file for storages
This commit is contained in:
parent
1cb8659412
commit
092809c2c6
3 changed files with 124 additions and 100 deletions
108
src/main.rs
108
src/main.rs
|
@ -11,26 +11,26 @@ extern crate dirs;
|
||||||
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use byte_unit::Byte;
|
use byte_unit::Byte;
|
||||||
use clap::{Parser, Subcommand, ValueEnum};
|
use clap::{Parser, Subcommand};
|
||||||
use clap_verbosity_flag::Verbosity;
|
use clap_verbosity_flag::Verbosity;
|
||||||
use git2::{Commit, IndexEntry, Oid, Repository};
|
use git2::{Commit, Oid, Repository};
|
||||||
use inquire::{validator::Validation, Text};
|
use inquire::{validator::Validation, Text};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_yaml;
|
use serde_yaml;
|
||||||
use std::{
|
|
||||||
collections::{hash_map::RandomState, HashMap},
|
|
||||||
fmt::Debug,
|
|
||||||
fs::{File, OpenOptions},
|
|
||||||
};
|
|
||||||
use std::{env, io::BufReader, path::Path};
|
use std::{env, io::BufReader, path::Path};
|
||||||
use std::{
|
use std::{
|
||||||
ffi::OsString,
|
ffi::OsString,
|
||||||
io::{self, BufWriter},
|
io::{self, BufWriter},
|
||||||
};
|
};
|
||||||
|
use std::{
|
||||||
|
fmt::Debug,
|
||||||
|
fs::{File, OpenOptions},
|
||||||
|
};
|
||||||
use std::{fs, io::prelude::*};
|
use std::{fs, io::prelude::*};
|
||||||
use sysinfo::{DiskExt, SystemExt};
|
use sysinfo::{DiskExt, SystemExt};
|
||||||
|
|
||||||
use devices::{Device, DEVICESFILE};
|
use devices::{Device, DEVICESFILE};
|
||||||
|
use storages::{physical_drive_partition::*, Storage, StorageExt, StorageType, STORAGESFILE};
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
|
@ -75,99 +75,7 @@ enum StorageCommands {
|
||||||
}
|
}
|
||||||
|
|
||||||
mod devices;
|
mod devices;
|
||||||
|
mod storages;
|
||||||
#[derive(ValueEnum, Clone, Copy, Debug)]
|
|
||||||
enum StorageType {
|
|
||||||
Physical,
|
|
||||||
// Online,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// All storage types.
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
enum Storage {
|
|
||||||
PhysicalStorage(PhysicalDrivePartition),
|
|
||||||
// /// Online storage provided by others.
|
|
||||||
// OnlineStorage {
|
|
||||||
// name: String,
|
|
||||||
// provider: String,
|
|
||||||
// capacity: u8,
|
|
||||||
// },
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Storage {
|
|
||||||
fn name(&self) -> &String {
|
|
||||||
match self {
|
|
||||||
Self::PhysicalStorage(s) => s.name(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const STORAGESFILE: &str = "storages.yml";
|
|
||||||
|
|
||||||
/// Partitoin of physical (on-premises) drive.
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct PhysicalDrivePartition {
|
|
||||||
name: String,
|
|
||||||
kind: String,
|
|
||||||
capacity: u64,
|
|
||||||
fs: String,
|
|
||||||
is_removable: bool,
|
|
||||||
system_names: HashMap<String, String, RandomState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PhysicalDrivePartition {
|
|
||||||
/// Try to get Physical drive info from sysinfo.
|
|
||||||
fn try_from_sysinfo_disk(
|
|
||||||
disk: &sysinfo::Disk,
|
|
||||||
name: String,
|
|
||||||
device: Device,
|
|
||||||
) -> Result<PhysicalDrivePartition> {
|
|
||||||
let alias = disk
|
|
||||||
.name()
|
|
||||||
.to_str()
|
|
||||||
.context("Failed to convert storage name to valid str.")?
|
|
||||||
.to_string();
|
|
||||||
let fs = disk.file_system();
|
|
||||||
trace!("fs: {:?}", fs);
|
|
||||||
let fs = std::str::from_utf8(fs)?;
|
|
||||||
Ok(PhysicalDrivePartition {
|
|
||||||
name: name,
|
|
||||||
kind: format!("{:?}", disk.kind()),
|
|
||||||
capacity: disk.total_space(),
|
|
||||||
fs: fs.to_string(),
|
|
||||||
is_removable: disk.is_removable(),
|
|
||||||
system_names: HashMap::from([(device.name(), alias)]),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn name(&self) -> &String {
|
|
||||||
&self.name
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_alias(
|
|
||||||
self,
|
|
||||||
disk: sysinfo::Disk,
|
|
||||||
device: Device,
|
|
||||||
) -> Result<PhysicalDrivePartition, String> {
|
|
||||||
let alias = match disk.name().to_str() {
|
|
||||||
Some(s) => s.to_string(),
|
|
||||||
None => return Err("Failed to convert storage name to valid str.".to_string()),
|
|
||||||
};
|
|
||||||
let mut aliases = self.system_names;
|
|
||||||
let _ = match aliases.insert(device.name(), alias) {
|
|
||||||
Some(v) => v,
|
|
||||||
None => return Err("Failed to insert alias".to_string()),
|
|
||||||
};
|
|
||||||
Ok(PhysicalDrivePartition {
|
|
||||||
name: self.name,
|
|
||||||
kind: self.kind,
|
|
||||||
capacity: self.capacity,
|
|
||||||
fs: self.fs,
|
|
||||||
is_removable: self.is_removable,
|
|
||||||
system_names: aliases,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct BackupLog {}
|
struct BackupLog {}
|
||||||
|
|
||||||
|
|
41
src/storages.rs
Normal file
41
src/storages.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
//! Manipulates storages.
|
||||||
|
|
||||||
|
use clap::ValueEnum;
|
||||||
|
use physical_drive_partition::PhysicalDrivePartition;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// YAML file to store known storages..
|
||||||
|
pub const STORAGESFILE: &str = "storages.yml";
|
||||||
|
|
||||||
|
#[derive(ValueEnum, Clone, Copy, Debug)]
|
||||||
|
pub enum StorageType {
|
||||||
|
Physical,
|
||||||
|
// Online,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// All storage types.
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub enum Storage {
|
||||||
|
PhysicalStorage(PhysicalDrivePartition),
|
||||||
|
// /// Online storage provided by others.
|
||||||
|
// OnlineStorage {
|
||||||
|
// name: String,
|
||||||
|
// provider: String,
|
||||||
|
// capacity: u8,
|
||||||
|
// },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Storage {
|
||||||
|
pub fn name(&self) -> &String {
|
||||||
|
match self {
|
||||||
|
Self::PhysicalStorage(s) => s.name(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait to manipulate all `Storage`s (Enums).
|
||||||
|
pub trait StorageExt {
|
||||||
|
fn name(&self) -> &String;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod physical_drive_partition;
|
75
src/storages/physical_drive_partition.rs
Normal file
75
src/storages/physical_drive_partition.rs
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
//! Manipulate partition of physical drive (both removable and unremovable).
|
||||||
|
|
||||||
|
use crate::devices::Device;
|
||||||
|
use crate::storages::StorageExt;
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::{hash_map::RandomState, HashMap};
|
||||||
|
use sysinfo::DiskExt;
|
||||||
|
|
||||||
|
/// Partitoin of physical (on-premises) drive.
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct PhysicalDrivePartition {
|
||||||
|
name: String,
|
||||||
|
kind: String,
|
||||||
|
capacity: u64,
|
||||||
|
fs: String,
|
||||||
|
is_removable: bool,
|
||||||
|
system_names: HashMap<String, String, RandomState>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PhysicalDrivePartition {
|
||||||
|
/// Try to get Physical drive info from sysinfo.
|
||||||
|
pub fn try_from_sysinfo_disk(
|
||||||
|
disk: &sysinfo::Disk,
|
||||||
|
name: String,
|
||||||
|
device: Device,
|
||||||
|
) -> Result<PhysicalDrivePartition> {
|
||||||
|
let alias = disk
|
||||||
|
.name()
|
||||||
|
.to_str()
|
||||||
|
.context("Failed to convert storage name to valid str.")?
|
||||||
|
.to_string();
|
||||||
|
let fs = disk.file_system();
|
||||||
|
trace!("fs: {:?}", fs);
|
||||||
|
let fs = std::str::from_utf8(fs)?;
|
||||||
|
Ok(PhysicalDrivePartition {
|
||||||
|
name: name,
|
||||||
|
kind: format!("{:?}", disk.kind()),
|
||||||
|
capacity: disk.total_space(),
|
||||||
|
fs: fs.to_string(),
|
||||||
|
is_removable: disk.is_removable(),
|
||||||
|
system_names: HashMap::from([(device.name(), alias)]),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_alias(
|
||||||
|
self,
|
||||||
|
disk: sysinfo::Disk,
|
||||||
|
device: Device,
|
||||||
|
) -> Result<PhysicalDrivePartition, String> {
|
||||||
|
let alias = match disk.name().to_str() {
|
||||||
|
Some(s) => s.to_string(),
|
||||||
|
None => return Err("Failed to convert storage name to valid str.".to_string()),
|
||||||
|
};
|
||||||
|
let mut aliases = self.system_names;
|
||||||
|
let _ = match aliases.insert(device.name(), alias) {
|
||||||
|
Some(v) => v,
|
||||||
|
None => return Err("Failed to insert alias".to_string()),
|
||||||
|
};
|
||||||
|
Ok(PhysicalDrivePartition {
|
||||||
|
name: self.name,
|
||||||
|
kind: self.kind,
|
||||||
|
capacity: self.capacity,
|
||||||
|
fs: self.fs,
|
||||||
|
is_removable: self.is_removable,
|
||||||
|
system_names: aliases,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StorageExt for PhysicalDrivePartition {
|
||||||
|
fn name(&self) -> &String {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue