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 serde::{Deserialize, Serialize};
use serde_yaml;
use std::fmt::format;
use std::{env, io::BufReader, path::Path};
use std::{
ffi::OsString,
@ -76,6 +77,7 @@ enum StorageCommands {
/// List all storages.
List {},
/// Add new device-specific name to existing storage.
/// For physical disk, the name is taken from system info automatically.
Bind { storage: String },
}
@ -171,75 +173,88 @@ fn main() -> Result<()> {
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).context(
"Repository doesn't exist. Please run init to initialize the repository.",
)?;
trace!("repo state: {:?}", repo.state());
match storage_type {
StorageType::Physical => {
// Get storages
let mut storages: Vec<Storage> = get_storages(&config_dir)?;
trace!("found storages: {:?}", storages);
Commands::Storage(storage) => {
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.command {
StorageCommands::Add { storage_type } => {
trace!("Storage Add {:?}", storage_type);
match storage_type {
StorageType::Physical => {
// Get storages
let mut storages: Vec<Storage> = get_storages(&config_dir)?;
trace!("found storages: {:?}", storages);
// select storage
let device = get_device(&config_dir)?;
let storage = select_physical_storage(device, &storages)?;
println!("storage: {:?}", storage);
let new_storage_name = storage.name().clone();
// select storage
let device = get_device(&config_dir)?;
let storage = select_physical_storage(device, &storages)?;
println!("storage: {:?}", storage);
let new_storage_name = storage.name().clone();
// add to storages
storages.push(Storage::PhysicalStorage(storage));
trace!("updated storages: {:?}", storages);
// add to storages
storages.push(Storage::PhysicalStorage(storage));
trace!("updated storages: {:?}", storages);
// write to file
write_storages(&config_dir, storages)?;
// write to file
write_storages(&config_dir, storages)?;
// commit
add_and_commit(
&repo,
&Path::new(STORAGESFILE),
&format!("Add new storage(physical drive): {}", new_storage_name),
)?;
// commit
add_and_commit(
&repo,
&Path::new(STORAGESFILE),
&format!("Add new storage(physical drive): {}", new_storage_name),
)?;
println!("Added new storage.");
trace!("Finished adding storage");
println!("Added new storage.");
trace!("Finished adding storage");
}
}
}
}
StorageCommands::List {} => {
// Get storages
let storages: Vec<Storage> = get_storages(&config_dir)?;
trace!("found storages: {:?}", storages);
for storage in storages {
println!("{}", storage);
StorageCommands::List {} => {
// Get storages
let storages: Vec<Storage> = get_storages(&config_dir)?;
trace!("found storages: {:?}", storages);
for storage in storages {
println!("{}", storage);
}
}
StorageCommands::Bind {
storage: storage_name,
} => {
// get storages
let mut storages: Vec<Storage> = get_storages(&config_dir)?;
let commit_comment = {
// find matching storage
let storage = &mut storages
.iter_mut()
.find(|s| s.name() == &storage_name)
.context(format!("No storage has name {}", storage_name))?;
// get disk from sysinfo
let mut sysinfo = sysinfo::System::new_all();
sysinfo.refresh_disks();
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
storage.add_alias(disk, &config_dir)?;
trace!("storage: {}", storage);
format!("{} to {}", system_name, storage.name())
};
trace!("bound new system name to the storage");
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);
}
}
StorageCommands::Bind {
storage: storage_name,
} => {
// get storages
let mut storages: Vec<Storage> = get_storages(&config_dir)?;
{
// find matching storage
let storage = &mut storages
.iter_mut()
.find(|s| s.name() == &storage_name)
.context(format!("No storage has name {}", storage_name))?;
// get disk from sysinfo
let mut sysinfo = sysinfo::System::new_all();
sysinfo.refresh_disks();
let disk = select_sysinfo_disk(&sysinfo)?;
// add to storages
storage.add_alias(disk, &config_dir)?;
trace!("storage: {}", storage);
}
trace!("bound new system name to the storage");
trace!("storages: {:#?}", storages);
}
},
}
Commands::Path {} => {
println!("{}", &config_dir.display());
}

View file

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