|
|
@ -14,8 +14,11 @@ use std::io::{self, Write}; |
|
|
|
use std::process; |
|
|
|
use std::process; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const VERSION: &str = "0.0.1"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Parser)] |
|
|
|
#[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 { |
|
|
|
struct Cli { |
|
|
|
#[command(subcommand)] |
|
|
|
#[command(subcommand)] |
|
|
|
command: Option<Commands>, |
|
|
|
command: Option<Commands>, |
|
|
@ -32,8 +35,13 @@ enum Commands { |
|
|
|
|
|
|
|
|
|
|
|
/// Show content of an item
|
|
|
|
/// Show content of an item
|
|
|
|
Show { |
|
|
|
Show { |
|
|
|
#[arg(value_name="item_id")] |
|
|
|
// positional
|
|
|
|
id: String |
|
|
|
#[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
|
|
|
|
/// Adds new item with unique id to the storage
|
|
|
@ -44,8 +52,13 @@ enum Commands { |
|
|
|
|
|
|
|
|
|
|
|
/// Edit item content
|
|
|
|
/// Edit item content
|
|
|
|
Edit { |
|
|
|
Edit { |
|
|
|
#[arg(value_name="item_id")] |
|
|
|
// positional
|
|
|
|
id: String |
|
|
|
#[arg(value_name="item_id", required=false)] |
|
|
|
|
|
|
|
id: Option<String>, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// named
|
|
|
|
|
|
|
|
#[arg(short, long, value_name="item_number", required=false)] |
|
|
|
|
|
|
|
number: Option<u32> |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
Delete { |
|
|
|
Delete { |
|
|
@ -149,13 +162,13 @@ impl MPS { |
|
|
|
fn list(&mut self) -> io:: Result<()> { |
|
|
|
fn list(&mut self) -> io:: Result<()> { |
|
|
|
self.login()?; |
|
|
|
self.login()?; |
|
|
|
let ids = self.storage.as_ref().unwrap().ids(); |
|
|
|
let ids = self.storage.as_ref().unwrap().ids(); |
|
|
|
// let mut counter = 1;
|
|
|
|
let mut counter = 1; |
|
|
|
if ids.len() == 0 { |
|
|
|
if ids.len() == 0 { |
|
|
|
println!("No items"); |
|
|
|
println!("No items"); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
for id in ids { |
|
|
|
for id in ids { |
|
|
|
println!("{}", id); |
|
|
|
println!("[{}] {}", counter, id); |
|
|
|
//counter += 1;
|
|
|
|
counter += 1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
@ -175,6 +188,13 @@ impl MPS { |
|
|
|
Ok(()) |
|
|
|
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<()> { |
|
|
|
fn add(&mut self, id: &String) -> io::Result<()> { |
|
|
|
self.login()?; |
|
|
|
self.login()?; |
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
@ -216,6 +236,13 @@ impl MPS { |
|
|
|
Ok(()) |
|
|
|
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<()> { |
|
|
|
fn delete(&mut self, id: &String) -> io::Result<()> { |
|
|
|
self.login()?; |
|
|
|
self.login()?; |
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
@ -242,17 +269,57 @@ fn run_command() -> io::Result<()> { |
|
|
|
let mut mps = MPS::new(); |
|
|
|
let mut mps = MPS::new(); |
|
|
|
mps.list()?; |
|
|
|
mps.list()?; |
|
|
|
} |
|
|
|
} |
|
|
|
Some(Commands::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(); |
|
|
|
let mut mps = MPS::new(); |
|
|
|
mps.show(id)?; |
|
|
|
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 }) => { |
|
|
|
Some(Commands::Add { id }) => { |
|
|
|
let mut mps = MPS::new(); |
|
|
|
let mut mps = MPS::new(); |
|
|
|
mps.add(id)?; |
|
|
|
mps.add(id)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Some(Commands::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(); |
|
|
|
let mut mps = MPS::new(); |
|
|
|
mps.edit(id)? |
|
|
|
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 }) => { |
|
|
|
Some(Commands::Delete { id }) => { |
|
|
|
let mut mps = MPS::new(); |
|
|
|
let mut mps = MPS::new(); |
|
|
|