|
|
@ -46,7 +46,10 @@ enum Commands { |
|
|
|
Edit(ItemIdArgs), |
|
|
|
Edit(ItemIdArgs), |
|
|
|
|
|
|
|
|
|
|
|
/// Delete item
|
|
|
|
/// Delete item
|
|
|
|
Delete(ItemIdArgs) |
|
|
|
Delete(ItemIdArgs), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set new passphrase
|
|
|
|
|
|
|
|
Password |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -121,10 +124,7 @@ impl MPS { |
|
|
|
Ok(ps) |
|
|
|
Ok(ps) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn init() -> io::Result<()> { |
|
|
|
fn read_new_password() -> io::Result<String> { |
|
|
|
if Storage::is_inited()? { |
|
|
|
|
|
|
|
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
let passphrase; |
|
|
|
let passphrase; |
|
|
|
loop { |
|
|
|
loop { |
|
|
|
match MPS::prompt_new_password() { |
|
|
|
match MPS::prompt_new_password() { |
|
|
@ -138,8 +138,15 @@ impl MPS { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Storage::init(passphrase)?; |
|
|
|
Ok(passphrase) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn init() -> io::Result<()> { |
|
|
|
|
|
|
|
if Storage::is_inited()? { |
|
|
|
|
|
|
|
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
let passphrase = MPS::read_new_password()?; |
|
|
|
|
|
|
|
Storage::init(passphrase) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn login(&mut self) -> io::Result<()> { |
|
|
|
pub fn login(&mut self) -> io::Result<()> { |
|
|
@ -156,6 +163,14 @@ impl MPS { |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn password(&mut self) -> io::Result<()> { |
|
|
|
|
|
|
|
self.login()?; |
|
|
|
|
|
|
|
let st = self.storage.as_ref().unwrap(); |
|
|
|
|
|
|
|
let passphrase = MPS::read_new_password()?; |
|
|
|
|
|
|
|
let new_st = st.new_from_passphrase(&passphrase); |
|
|
|
|
|
|
|
new_st.dump() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
@ -171,25 +186,12 @@ impl MPS { |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Show item. Id must exists
|
|
|
|
fn show(&mut self, id: &String) -> io::Result<()> { |
|
|
|
fn show(&mut self, id: &String) -> io::Result<()> { |
|
|
|
self.login()?; |
|
|
|
self.login()?; |
|
|
|
let st = self.storage.as_ref().unwrap(); |
|
|
|
let st = self.storage.as_ref().unwrap(); |
|
|
|
if !st.contains(id) { |
|
|
|
|
|
|
|
return Err(io::Error::new( |
|
|
|
|
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
|
|
|
|
format!("Can`t find id: {}", id) |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
let item = st.get(id); |
|
|
|
let item = st.get(id); |
|
|
|
editor::open_to_show(&item.content)?; |
|
|
|
editor::open_to_show(&item.content) |
|
|
|
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<()> { |
|
|
@ -208,10 +210,10 @@ impl MPS { |
|
|
|
// set empty string because there is no content yet
|
|
|
|
// set empty string because there is no content yet
|
|
|
|
let content = editor::open_to_edit(&String::from(""))?; |
|
|
|
let content = editor::open_to_edit(&String::from(""))?; |
|
|
|
st.add(Item::from(id.clone(), content)); |
|
|
|
st.add(Item::from(id.clone(), content)); |
|
|
|
st.dump()?; |
|
|
|
st.dump() |
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Edit item, id need not to exist
|
|
|
|
fn edit(&mut self, id: &String) -> io::Result<()> { |
|
|
|
fn edit(&mut self, id: &String) -> io::Result<()> { |
|
|
|
self.login()?; |
|
|
|
self.login()?; |
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
@ -229,40 +231,49 @@ impl MPS { |
|
|
|
let new_content = editor::open_to_edit(&item.content)?; |
|
|
|
let new_content = editor::open_to_edit(&item.content)?; |
|
|
|
item.content = new_content; |
|
|
|
item.content = new_content; |
|
|
|
st.update(item); |
|
|
|
st.update(item); |
|
|
|
st.dump()?; |
|
|
|
st.dump() |
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn edit_by_number(&mut self, number: u32) -> io::Result<()> { |
|
|
|
// Delete item by id, is must exist
|
|
|
|
|
|
|
|
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(); |
|
|
|
let id = st.get_id_by_number(number)?; |
|
|
|
st.remove(id); |
|
|
|
self.edit(&id) |
|
|
|
st.dump() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn delete(&mut self, id: &String) -> io::Result<()> { |
|
|
|
/// Resolve id by ItemArgs.
|
|
|
|
|
|
|
|
/// # Arguments
|
|
|
|
|
|
|
|
/// * `args` - arguments to parse
|
|
|
|
|
|
|
|
/// * `check` - check that id existing
|
|
|
|
|
|
|
|
fn item_id_by_item_id_args(&mut self, args: &ItemIdArgs, check: bool) -> io::Result<String> { |
|
|
|
self.login()?; |
|
|
|
self.login()?; |
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
if !st.contains(id) { |
|
|
|
let mut item_id: String = "NOT_INITIALIZED".to_string(); |
|
|
|
|
|
|
|
if let Some(id) = &args.id { |
|
|
|
|
|
|
|
item_id = id.clone(); |
|
|
|
|
|
|
|
if check && !st.contains(&id) { |
|
|
|
return Err(io::Error::new( |
|
|
|
return Err(io::Error::new( |
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
"No such item" |
|
|
|
format!("No such item {}", id) |
|
|
|
)); |
|
|
|
)); |
|
|
|
} |
|
|
|
} |
|
|
|
st.remove(id); |
|
|
|
|
|
|
|
st.dump()?; |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if let Some(number) = &args.number { |
|
|
|
fn delete_by_number(&mut self, number: u32) -> io::Result<()> { |
|
|
|
item_id = st.get_id_by_number(*number)?; |
|
|
|
self.login()?; |
|
|
|
// we can guarantee that id exists because we take it by id
|
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
} |
|
|
|
let id = st.get_id_by_number(number)?; |
|
|
|
if args.id.is_none() && args.number.is_none() { |
|
|
|
self.delete(&id) |
|
|
|
return Err(io::Error::new( |
|
|
|
|
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
|
|
|
|
format!("Bag arguments") |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Ok(item_id.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn run_command() -> io::Result<()> { |
|
|
|
fn run_command() -> io::Result<()> { |
|
|
|
let cli = Cli::parse(); |
|
|
|
let cli = Cli::parse(); |
|
|
|
match &cli.command { |
|
|
|
match &cli.command { |
|
|
@ -273,36 +284,28 @@ fn run_command() -> io::Result<()> { |
|
|
|
let mut mps = MPS::new(); |
|
|
|
let mut mps = MPS::new(); |
|
|
|
mps.list()?; |
|
|
|
mps.list()?; |
|
|
|
} |
|
|
|
} |
|
|
|
Some(Commands::Show(item)) => { |
|
|
|
Some(Commands::Show(args)) => { |
|
|
|
let mut mps = MPS::new(); |
|
|
|
let mut mps = MPS::new(); |
|
|
|
if let Some(id) = &item.id { |
|
|
|
let id = mps.item_id_by_item_id_args(args, true)?; |
|
|
|
mps.show(id)?; |
|
|
|
mps.show(&id)?; |
|
|
|
} |
|
|
|
|
|
|
|
if let Some(number) = &item.number { |
|
|
|
|
|
|
|
mps.show_by_number(*number)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
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(item)) => { |
|
|
|
Some(Commands::Edit(args)) => { |
|
|
|
let mut mps = MPS::new(); |
|
|
|
let mut mps = MPS::new(); |
|
|
|
if let Some(id) = &item.id { |
|
|
|
let id = mps.item_id_by_item_id_args(args, false)?; |
|
|
|
mps.edit(id)?; |
|
|
|
mps.edit(&id)?; |
|
|
|
} |
|
|
|
|
|
|
|
if let Some(number) = &item.number { |
|
|
|
|
|
|
|
mps.edit_by_number(*number)?; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Some(Commands::Delete(args)) => { |
|
|
|
Some(Commands::Delete(item)) => { |
|
|
|
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
let mut mps = MPS::new(); |
|
|
|
if let Some(id) = &item.id { |
|
|
|
let id = mps.item_id_by_item_id_args(args, true)?; |
|
|
|
mps.delete(id)? |
|
|
|
mps.delete(&id)?; |
|
|
|
} |
|
|
|
|
|
|
|
if let Some(number) = &item.number { |
|
|
|
|
|
|
|
mps.delete_by_number(*number)?; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Some(Commands::Password) => { |
|
|
|
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
|
|
|
mps.password()?; |
|
|
|
} |
|
|
|
} |
|
|
|
None => { |
|
|
|
None => { |
|
|
|
match Storage::check_installed() { |
|
|
|
match Storage::check_installed() { |
|
|
|