Skip to content

Commit

Permalink
Implement example fs cli read write
Browse files Browse the repository at this point in the history
  • Loading branch information
twitu committed Mar 14, 2024
1 parent ff68e4e commit a4dd690
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 109 deletions.
64 changes: 64 additions & 0 deletions fs-storage/src/bin/cli.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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::<Vec<String>>()
} else {
vec![]
};
let fs = FileStorage::new("cli".to_string(), Path::new(path));
fs.read_file(|map: HashMap<String, String>| {
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::<HashMap<String, String>>();
let mut fs = FileStorage::new("cli".to_string(), Path::new(path));
fs.write_file(&kv_pairs).unwrap();
}
_ => println!("Invalid command. Use 'read' or 'write'."),
}
}
36 changes: 0 additions & 36 deletions fs-storage/src/cli/read.rs

This file was deleted.

72 changes: 0 additions & 72 deletions fs-storage/src/cli/write.rs

This file was deleted.

2 changes: 1 addition & 1 deletion fs-storage/src/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down

0 comments on commit a4dd690

Please sign in to comment.