Skip to content

Commit

Permalink
some basic code
Browse files Browse the repository at this point in the history
Signed-off-by: Pushkar Mishra <[email protected]>
  • Loading branch information
Pushkarm029 committed Mar 5, 2024
1 parent bcbaace commit efa1801
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
"fs-atomic-versions",
"fs-atomic-light",
"fs-index",
"fs-storage",
"fs-storage", "sample-cli",
]

default-members = [
Expand Down
8 changes: 8 additions & 0 deletions sample-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "sample-cli"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Empty file added sample-cli/src/lib.rs
Empty file.
36 changes: 36 additions & 0 deletions sample-cli/src/read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};

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 = match File::open(storage_path) {
Ok(file) => file,
Err(err) => {
println!("Error opening storage file: {}", err);
return;
}
};

let reader = BufReader::new(file);
let mut found = false;
for line in reader.lines() {
if let Ok(line) = line {
let parts: Vec<&str> = line.splitn(2, '=').collect();
if parts.len() == 2 && parts[0] == key {
println!("Value for key '{}': {}", key, parts[1]);
found = true;
break;
}
}
}
if !found {
println!("Key '{}' not found in storage.", key);
}
}
66 changes: 66 additions & 0 deletions sample-cli/src/write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::env;
use std::fs;
use std::io::{self, Write};

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 storage_type: String = String::new();
let mut kv_pairs: Vec<(String, String)> = Vec::new();

println!("Please specify the storage type:");
io::stdout().flush().unwrap();
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:");
io::stdout().flush().unwrap();

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.push((key, value));
}

println!("Storage Type: {}", storage_type);
println!("Key-Value Pairs:");
for (key, value) in kv_pairs {
println!("{}: {}", key, value);
}
}

fn echo(s: &str, path: &Path) -> io::Result<()> {
let mut f = File::create(path)?;

f.write_all(s.as_bytes())
}

0 comments on commit efa1801

Please sign in to comment.