|
|
|
@ -14,8 +14,11 @@ use std::io::{self, Write};
|
|
|
|
|
use std::process; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const VERSION: &str = "0.0.1"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Parser)] |
|
|
|
|
#[command(name = "mps", version = "0.0.1", about = "MyPasswordStorage: Tool for storing your passwords locally with git synchronization")] |
|
|
|
|
#[command(name = "mps", version = VERSION, about = "MyPasswordStorage: Tool for storing your passwords locally with git synchronization")] |
|
|
|
|
struct Cli { |
|
|
|
|
#[command(subcommand)] |
|
|
|
|
command: Option<Commands>, |
|
|
|
@ -32,8 +35,13 @@ enum Commands {
|
|
|
|
|
|
|
|
|
|
/// Show content of an item
|
|
|
|
|
Show { |
|
|
|
|
#[arg(value_name="item_id")] |
|
|
|
|
id: String |
|
|
|
|
// positional
|
|
|
|
|
#[arg(value_name="item_id", required=false)] |
|
|
|
|
id: Option<String>, |
|
|
|
|
|
|
|
|
|
// named
|
|
|
|
|
#[arg(short, long, value_name="item_number", required=false)] |
|
|
|
|
number: Option<u32> |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/// Adds new item with unique id to the storage
|
|
|
|
@ -44,8 +52,13 @@ enum Commands {
|
|
|
|
|
|
|
|
|
|
/// Edit item content
|
|
|
|
|
Edit { |
|
|
|
|
#[arg(value_name="item_id")] |
|
|
|
|
id: String |
|
|
|
|
// positional
|
|
|
|
|
#[arg(value_name="item_id", required=false)] |
|
|
|
|
id: Option<String>, |
|
|
|
|
|
|
|
|
|
// named
|
|
|
|
|
#[arg(short, long, value_name="item_number", required=false)] |
|
|
|
|
number: Option<u32> |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Delete { |
|
|
|
@ -149,13 +162,13 @@ impl MPS {
|
|
|
|
|
fn list(&mut self) -> io:: Result<()> { |
|
|
|
|
self.login()?; |
|
|
|
|
let ids = self.storage.as_ref().unwrap().ids(); |
|
|
|
|
// let mut counter = 1;
|
|
|
|
|
let mut counter = 1; |
|
|
|
|
if ids.len() == 0 { |
|
|
|
|
println!("No items"); |
|
|
|
|
} else { |
|
|
|
|
for id in ids { |
|
|
|
|
println!("{}", id); |
|
|
|
|
//counter += 1;
|
|
|
|
|
println!("[{}] {}", counter, id); |
|
|
|
|
counter += 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Ok(()) |
|
|
|
@ -175,6 +188,13 @@ impl MPS {
|
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn show_by_number(&mut self, number: u32) -> io::Result<()> { |
|
|
|
|
self.login()?; |
|
|
|
|
let st = self.storage.as_ref().unwrap(); |
|
|
|
|
let id = st.get_id_by_number(number)?; |
|
|
|
|
self.show(&id) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn add(&mut self, id: &String) -> io::Result<()> { |
|
|
|
|
self.login()?; |
|
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
@ -216,6 +236,13 @@ impl MPS {
|
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn edit_by_number(&mut self, number: u32) -> io::Result<()> { |
|
|
|
|
self.login()?; |
|
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
|
let id = st.get_id_by_number(number)?; |
|
|
|
|
self.edit(&id) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn delete(&mut self, id: &String) -> io::Result<()> { |
|
|
|
|
self.login()?; |
|
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
@ -242,17 +269,57 @@ fn run_command() -> io::Result<()> {
|
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.list()?; |
|
|
|
|
} |
|
|
|
|
Some(Commands::Show { id }) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.show(id)?; |
|
|
|
|
Some(Commands::Show { id, number }) => { |
|
|
|
|
match (id, number) { |
|
|
|
|
(None, None) => { |
|
|
|
|
return Err(io::Error::new( |
|
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
|
"Must provide <item_id> or -n <item_number>" |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
(Some(item_id), None) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.show(item_id)?; |
|
|
|
|
}, |
|
|
|
|
(None, Some(item_number)) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.show_by_number(*item_number)?; |
|
|
|
|
}, |
|
|
|
|
_ => { |
|
|
|
|
return Err(io::Error::new( |
|
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
|
"Must provide <item_id> or -n <item_number> but not both" |
|
|
|
|
)) |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
Some(Commands::Add { id }) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.add(id)?; |
|
|
|
|
} |
|
|
|
|
Some(Commands::Edit { id }) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.edit(id)? |
|
|
|
|
Some(Commands::Edit { id, number }) => { |
|
|
|
|
match (id, number) { |
|
|
|
|
(None, None) => { |
|
|
|
|
return Err(io::Error::new( |
|
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
|
"Must provide <item_id> or -n <item_number>" |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
(Some(item_id), None) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.edit(item_id)?; |
|
|
|
|
}, |
|
|
|
|
(None, Some(item_number)) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.edit_by_number(*item_number)?; |
|
|
|
|
}, |
|
|
|
|
_ => { |
|
|
|
|
return Err(io::Error::new( |
|
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
|
"Must provide <item_id> or -n <item_number> but not both" |
|
|
|
|
)) |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
Some(Commands::Delete { id }) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|