fix: storage bind (add writing and commiting)

This commit is contained in:
qwjyh 2023-08-30 02:52:12 +09:00
parent d7e7665739
commit 78b2d956a7
2 changed files with 77 additions and 64 deletions

View file

@ -17,6 +17,7 @@ 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::fmt::format;
use std::{env, io::BufReader, path::Path}; use std::{env, io::BufReader, path::Path};
use std::{ use std::{
ffi::OsString, ffi::OsString,
@ -76,6 +77,7 @@ enum StorageCommands {
/// List all storages. /// List all storages.
List {}, List {},
/// Add new device-specific name to existing storage. /// Add new device-specific name to existing storage.
/// For physical disk, the name is taken from system info automatically.
Bind { storage: String }, Bind { storage: String },
} }
@ -171,13 +173,14 @@ fn main() -> Result<()> {
println!("Device added"); println!("Device added");
full_status(&repo)?; full_status(&repo)?;
} }
Commands::Storage(storage) => match storage.command { Commands::Storage(storage) => {
StorageCommands::Add { storage_type } => {
trace!("Storage Add {:?}", storage_type);
let repo = Repository::open(&config_dir).context( let repo = Repository::open(&config_dir).context(
"Repository doesn't exist. Please run init to initialize the repository.", "Repository doesn't exist. Please run init to initialize the repository.",
)?; )?;
trace!("repo state: {:?}", repo.state()); trace!("repo state: {:?}", repo.state());
match storage.command {
StorageCommands::Add { storage_type } => {
trace!("Storage Add {:?}", storage_type);
match storage_type { match storage_type {
StorageType::Physical => { StorageType::Physical => {
// Get storages // Get storages
@ -222,7 +225,7 @@ fn main() -> Result<()> {
} => { } => {
// get storages // get storages
let mut storages: Vec<Storage> = get_storages(&config_dir)?; let mut storages: Vec<Storage> = get_storages(&config_dir)?;
{ let commit_comment = {
// find matching storage // find matching storage
let storage = &mut storages let storage = &mut storages
.iter_mut() .iter_mut()
@ -232,14 +235,26 @@ fn main() -> Result<()> {
let mut sysinfo = sysinfo::System::new_all(); let mut sysinfo = sysinfo::System::new_all();
sysinfo.refresh_disks(); sysinfo.refresh_disks();
let disk = select_sysinfo_disk(&sysinfo)?; let disk = select_sysinfo_disk(&sysinfo)?;
let system_name = disk.name().to_str().context("Failed to convert disk name to valid string")?;
// add to storages // add to storages
storage.add_alias(disk, &config_dir)?; storage.add_alias(disk, &config_dir)?;
trace!("storage: {}", storage); trace!("storage: {}", storage);
} format!("{} to {}", system_name, storage.name())
};
trace!("bound new system name to the storage"); trace!("bound new system name to the storage");
trace!("storages: {:#?}", storages); trace!("storages: {:#?}", storages);
write_storages(&config_dir, storages)?;
// commit
add_and_commit(
&repo,
&Path::new(STORAGESFILE),
&format!("Bound new storage name to physical drive ({})", commit_comment),
)?;
println!("Bound new storage name to physical drive ({})", commit_comment);
}
}
} }
},
Commands::Path {} => { Commands::Path {} => {
println!("{}", &config_dir.display()); println!("{}", &config_dir.display());
} }

View file

@ -60,11 +60,10 @@ impl PhysicalDrivePartition {
let aliases = &mut self.system_names; let aliases = &mut self.system_names;
trace!("aliases: {:?}", aliases); trace!("aliases: {:?}", aliases);
match aliases.insert(device.name(), alias) { match aliases.insert(device.name(), alias) {
Some(v) => trace!("old val is: {}", v), Some(v) => println!("Value updated. old val is: {}", v),
None => trace!("inserted new val"), None => println!("inserted new val"),
}; };
trace!("aliases: {:?}", aliases); trace!("aliases: {:?}", aliases);
// self.system_names = aliases;
Ok(()) Ok(())
} }
} }
@ -73,7 +72,6 @@ impl StorageExt for PhysicalDrivePartition {
fn name(&self) -> &String { fn name(&self) -> &String {
&self.name &self.name
} }
} }
impl fmt::Display for PhysicalDrivePartition { impl fmt::Display for PhysicalDrivePartition {