Skip to content

Commit

Permalink
add binary exe
Browse files Browse the repository at this point in the history
Signed-off-by: Pushkar Mishra <[email protected]>
  • Loading branch information
Pushkarm029 committed Mar 8, 2024
1 parent 6428daa commit 56e27e6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
31 changes: 31 additions & 0 deletions fs-storage/src/cli/read.rs
Original file line number Diff line number Diff line change
@@ -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<String> = env::args().collect();
if args.len() != 3 {
println!("Usage: {} <path_to_storage> <key>", 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::<String, String>(|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);
}
}
}
68 changes: 68 additions & 0 deletions fs-storage/src/cli/write.rs
Original file line number Diff line number Diff line change
@@ -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<String> = env::args().collect();
if args.len() != 2 {
println!("Usage: {} <path_to_storage>", 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<String, String> = 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<String, String>, storage_path: &str) -> io::Result<()> {
let mut storage = file_storage::FileStorage::new(storage_path);
storage.write_file(&kv_pairs)
}

0 comments on commit 56e27e6

Please sign in to comment.