mirror of
https://github.com/qwjyh/xdbm
synced 2024-11-22 14:50:12 +09:00
fix: storage bind (add writing and commiting)
This commit is contained in:
parent
d7e7665739
commit
78b2d956a7
2 changed files with 77 additions and 64 deletions
135
src/main.rs
135
src/main.rs
|
@ -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,75 +173,88 @@ 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 } => {
|
let repo = Repository::open(&config_dir).context(
|
||||||
trace!("Storage Add {:?}", storage_type);
|
"Repository doesn't exist. Please run init to initialize the repository.",
|
||||||
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 {
|
||||||
trace!("repo state: {:?}", repo.state());
|
StorageCommands::Add { storage_type } => {
|
||||||
match storage_type {
|
trace!("Storage Add {:?}", storage_type);
|
||||||
StorageType::Physical => {
|
match storage_type {
|
||||||
// Get storages
|
StorageType::Physical => {
|
||||||
let mut storages: Vec<Storage> = get_storages(&config_dir)?;
|
// Get storages
|
||||||
trace!("found storages: {:?}", storages);
|
let mut storages: Vec<Storage> = get_storages(&config_dir)?;
|
||||||
|
trace!("found storages: {:?}", storages);
|
||||||
|
|
||||||
// select storage
|
// select storage
|
||||||
let device = get_device(&config_dir)?;
|
let device = get_device(&config_dir)?;
|
||||||
let storage = select_physical_storage(device, &storages)?;
|
let storage = select_physical_storage(device, &storages)?;
|
||||||
println!("storage: {:?}", storage);
|
println!("storage: {:?}", storage);
|
||||||
let new_storage_name = storage.name().clone();
|
let new_storage_name = storage.name().clone();
|
||||||
|
|
||||||
// add to storages
|
// add to storages
|
||||||
storages.push(Storage::PhysicalStorage(storage));
|
storages.push(Storage::PhysicalStorage(storage));
|
||||||
trace!("updated storages: {:?}", storages);
|
trace!("updated storages: {:?}", storages);
|
||||||
|
|
||||||
// write to file
|
// write to file
|
||||||
write_storages(&config_dir, storages)?;
|
write_storages(&config_dir, storages)?;
|
||||||
|
|
||||||
// commit
|
// commit
|
||||||
add_and_commit(
|
add_and_commit(
|
||||||
&repo,
|
&repo,
|
||||||
&Path::new(STORAGESFILE),
|
&Path::new(STORAGESFILE),
|
||||||
&format!("Add new storage(physical drive): {}", new_storage_name),
|
&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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
StorageCommands::List {} => {
|
||||||
StorageCommands::List {} => {
|
// Get storages
|
||||||
// Get storages
|
let storages: Vec<Storage> = get_storages(&config_dir)?;
|
||||||
let storages: Vec<Storage> = get_storages(&config_dir)?;
|
trace!("found storages: {:?}", storages);
|
||||||
trace!("found storages: {:?}", storages);
|
for storage in storages {
|
||||||
for storage in storages {
|
println!("{}", storage);
|
||||||
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 {} => {
|
Commands::Path {} => {
|
||||||
println!("{}", &config_dir.display());
|
println!("{}", &config_dir.display());
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue