diff --git a/fs-storage/src/bin/cli.rs b/fs-storage/src/bin/cli.rs new file mode 100644 index 00000000..a0a3635f --- /dev/null +++ b/fs-storage/src/bin/cli.rs @@ -0,0 +1,64 @@ +use std::collections::HashMap; +use std::env; +use std::path::Path; + +use fs_storage::file_storage::FileStorage; + +fn main() { + let args: Vec = env::args().collect(); + if args.len() < 3 { + println!("Usage: cargo run -- [read|write] path [key1,key2|key1:value1,key2:value2]"); + return; + } + + let command = &args[1]; + let path = &args[2]; + + match command.as_str() { + "read" => { + let keys = if args.len() > 3 { + args[3] + .split(",") + .map(|s| s.to_string()) + .collect::>() + } else { + vec![] + }; + let fs = FileStorage::new("cli".to_string(), Path::new(path)); + fs.read_file(|map: HashMap| { + if keys.is_empty() { + for (key, value) in map { + println!("{}: {}", key, value); + } + } else { + for key in &keys { + if let Some(value) = map.get(key) { + println!("{}: {}", key, value); + } else { + println!("Key '{}' not found", key); + } + } + } + }) + .unwrap(); + } + "write" => { + if args.len() < 4 { + println!( + "Usage: cargo run -- write path key1:value1,key2:value2" + ); + return; + } + let kv_pairs = args[3] + .split(",") + .map(|s| { + let kv: Vec<&str> = s.split(":").collect(); + (kv[0].to_string(), kv[1].to_string()) + }) + .collect::>(); + let mut fs = FileStorage::new("cli".to_string(), Path::new(path)); + fs.write_file(&kv_pairs).unwrap(); + } + _ => println!("Invalid command. Use 'read' or 'write'."), + } +} diff --git a/fs-storage/src/cli/read.rs b/fs-storage/src/cli/read.rs deleted file mode 100644 index 6f4b3f2b..00000000 --- a/fs-storage/src/cli/read.rs +++ /dev/null @@ -1,36 +0,0 @@ -use std::env; -use std::path::Path; -use fs_storage::file_storage::FileStorage; - -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]; - - println!("Storage Path: {}", storage_path); - println!("Key: {}", key); - - if !Path::new(storage_path).exists() { - println!("Error: Storage file does not exist."); - return; - } - - let file_storage = FileStorage::new("our_label".to_string(), Path::new(storage_path)); - match file_storage.read_file::(|value_by_id| { - println!("ruun3"); - 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); - } - } -} diff --git a/fs-storage/src/cli/write.rs b/fs-storage/src/cli/write.rs deleted file mode 100644 index e484ae14..00000000 --- a/fs-storage/src/cli/write.rs +++ /dev/null @@ -1,72 +0,0 @@ -use std::collections::HashMap; -use std::env; -use std::fs; -use std::io; -use std::path::PathBuf; -use fs_storage::file_storage::FileStorage; - -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_all(storage_path) { - println!("Error creating storage directory: {}", err); - return; - } - println!("Storage directory created successfully at {}", storage_path); - - let mut kv_pairs: HashMap = HashMap::new(); - 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!("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 = PathBuf::from(storage_path); - storage_file.push("storage.txt"); - - let mut storage = FileStorage::new("our_label".to_string(), &storage_file); - match storage.write_file(&kv_pairs) { - Ok(_) => Ok(()), - Err(err) => { - let io_err = io::Error::new(io::ErrorKind::Other, format!("ArklibError: {:?}", err)); - Err(io_err) - } - } -} diff --git a/fs-storage/src/file_storage.rs b/fs-storage/src/file_storage.rs index 9e595c31..d235e974 100644 --- a/fs-storage/src/file_storage.rs +++ b/fs-storage/src/file_storage.rs @@ -25,7 +25,7 @@ impl FileStorage { /// Create a new file storage with a diagnostic label and file path pub fn new(label: String, path: &Path) -> Self { Self { - label: label.clone(), + label, path: PathBuf::from(path), timestamp: SystemTime::now(), }