From 56e27e6595a208256f88454b9dd1ac5ff9a71080 Mon Sep 17 00:00:00 2001 From: Pushkar Mishra Date: Fri, 8 Mar 2024 15:54:59 +0530 Subject: [PATCH] add binary exe Signed-off-by: Pushkar Mishra --- fs-storage/src/cli/read.rs | 31 +++++++++++++++++ fs-storage/src/cli/write.rs | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 fs-storage/src/cli/read.rs create mode 100644 fs-storage/src/cli/write.rs diff --git a/fs-storage/src/cli/read.rs b/fs-storage/src/cli/read.rs new file mode 100644 index 00000000..173f5da7 --- /dev/null +++ b/fs-storage/src/cli/read.rs @@ -0,0 +1,31 @@ +use std::env; +use fs_storage::file_storage::FileStorage; // Import FileStorage +use std::collections::HashMap; +use std::str::FromStr; +use data_error::ArklibError; +use std::fmt::Debug; + +fn main() { + let args: Vec = env::args().collect(); + if args.len() != 3 { + println!("Usage: {} ", args[0]); + return; + } + let storage_path = &args[1]; + let key = &args[2]; + + let file_storage = FileStorage::new("Storage".to_string(), storage_path); + + match file_storage.read_file::(|value_by_id| { + if let Some(value) = value_by_id.get(key) { + println!("Value for key '{}': {:?}", key, value); + } else { + println!("Key '{}' not found in storage.", key); + } + }) { + Ok(_) => {} + Err(e) => { + println!("Error reading storage file: {:?}", e); + } + } +} \ No newline at end of file diff --git a/fs-storage/src/cli/write.rs b/fs-storage/src/cli/write.rs new file mode 100644 index 00000000..bb036df4 --- /dev/null +++ b/fs-storage/src/cli/write.rs @@ -0,0 +1,68 @@ +use std::collections::HashMap; +use std::env; +use std::fs::{self, File}; +use std::io::{self, Write, BufWriter}; +use std::path::Path; + +fn main() { + let args: Vec = env::args().collect(); + if args.len() != 2 { + println!("Usage: {} ", args[0]); + return; + } + + let storage_path = &args[1]; + if fs::metadata(storage_path).is_ok() { + println!("Storage already exists at {}", storage_path); + return; + } + if let Err(err) = fs::create_dir(storage_path) { + println!("Error creating storage directory: {}", err); + return; + } + println!("Storage created successfully at {}", storage_path); + + let mut kv_pairs: HashMap = HashMap::new(); + + println!("Please specify the storage type:"); + let mut storage_type = String::new(); + io::stdin().read_line(&mut storage_type).expect("Failed to read line"); + let storage_type = storage_type.trim(); + + loop { + println!("Enter a key-value pair (key=value), or enter 'done' to finish:"); + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Failed to read line"); + let input = input.trim(); + + if input.eq_ignore_ascii_case("done") { + break; + } + + let pair: Vec<&str> = input.splitn(2, '=').collect(); + if pair.len() != 2 { + println!("Invalid input, key-value pair must be in the format 'key=value'"); + continue; + } + + let key = pair[0].trim().to_string(); + let value = pair[1].trim().to_string(); + + kv_pairs.insert(key, value); + } + + println!("Storage Type: {}", storage_type); + println!("Key-Value Pairs:"); + for (key, value) in &kv_pairs { + println!("{}: {}", key, value); + } + + if let Err(err) = write_to_file(kv_pairs, storage_path) { + println!("Error writing to file: {}", err); + } +} + +fn write_to_file(kv_pairs: HashMap, storage_path: &str) -> io::Result<()> { + let mut storage = file_storage::FileStorage::new(storage_path); + storage.write_file(&kv_pairs) +} \ No newline at end of file