-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pushkar Mishra <[email protected]>
- Loading branch information
1 parent
bcbaace
commit efa1801
Showing
5 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |