|
|
|
@ -7,18 +7,15 @@ mod git;
|
|
|
|
|
|
|
|
|
|
use storage::{Storage, Item}; |
|
|
|
|
|
|
|
|
|
use clap::{Parser, Subcommand, ArgGroup}; |
|
|
|
|
use clap::{Parser, Subcommand}; |
|
|
|
|
use rpassword; |
|
|
|
|
|
|
|
|
|
use std::io::{self, Write}; |
|
|
|
|
use std::process; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const VERSION: &str = "0.0.1"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Parser)] |
|
|
|
|
#[command(name = "mps", version = VERSION, about = "MyPasswordStorage: Tool for storing your passwords locally with git synchronization")] |
|
|
|
|
#[command(name = "mps", version = "0.0.1", about = "MyPasswordStorage: Tool for storing your passwords locally with git synchronization")] |
|
|
|
|
struct Cli { |
|
|
|
|
#[command(subcommand)] |
|
|
|
|
command: Option<Commands>, |
|
|
|
@ -34,7 +31,10 @@ enum Commands {
|
|
|
|
|
List, |
|
|
|
|
|
|
|
|
|
/// Show content of an item
|
|
|
|
|
Show(ItemIdArgs), |
|
|
|
|
Show { |
|
|
|
|
#[arg(value_name="item_id")] |
|
|
|
|
id: String |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/// Adds new item with unique id to the storage
|
|
|
|
|
Add { |
|
|
|
@ -43,29 +43,16 @@ enum Commands {
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/// Edit item content
|
|
|
|
|
Edit(ItemIdArgs), |
|
|
|
|
|
|
|
|
|
/// Delete item
|
|
|
|
|
Delete(ItemIdArgs), |
|
|
|
|
|
|
|
|
|
/// Set new passphrase
|
|
|
|
|
Password |
|
|
|
|
Edit { |
|
|
|
|
#[arg(value_name="item_id")] |
|
|
|
|
id: String |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Delete { |
|
|
|
|
#[arg(value_name="item_id")] |
|
|
|
|
id: String |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Parser)] |
|
|
|
|
#[command(group(
|
|
|
|
|
ArgGroup::new("item") |
|
|
|
|
.required(true) |
|
|
|
|
.args(&["id", "number"]) |
|
|
|
|
))] |
|
|
|
|
struct ItemIdArgs { |
|
|
|
|
/// Item id
|
|
|
|
|
#[arg(required=false)] |
|
|
|
|
id: Option<String>, |
|
|
|
|
|
|
|
|
|
#[arg(short='n', long, value_name="number")] |
|
|
|
|
number: Option<u32> |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
enum PROMPT { |
|
|
|
@ -99,10 +86,11 @@ impl MPS {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn print_init_hint() { |
|
|
|
|
println!("1. Run `mps init`"); |
|
|
|
|
println!("2. Init `storage` under git:"); |
|
|
|
|
println!(" cd storage"); |
|
|
|
|
println!(" git init"); |
|
|
|
|
println!("1. Create empty repository for storing your passwords on gitlab or somewhere you prefer"); |
|
|
|
|
println!("2. Create dir `mps`: `mkdir ~/mps`"); |
|
|
|
|
println!("3. Clone to your home folder `~/.mps/storage`: `git clone <your_storage_url> storage`"); |
|
|
|
|
println!("4. xport varable `$MPS_HOME`: `export MPS_HOME=\"/home/<your_username>/.mps\"`"); |
|
|
|
|
println!("5. Run `mps init`"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn prompt_new_password() -> io::Result<String> { |
|
|
|
@ -124,7 +112,10 @@ impl MPS {
|
|
|
|
|
Ok(ps) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn read_new_password() -> io::Result<String> { |
|
|
|
|
fn init() -> io::Result<()> { |
|
|
|
|
if Storage::is_inited()? { |
|
|
|
|
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); |
|
|
|
|
} |
|
|
|
|
let passphrase; |
|
|
|
|
loop { |
|
|
|
|
match MPS::prompt_new_password() { |
|
|
|
@ -138,15 +129,8 @@ impl MPS {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Ok(passphrase) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
Storage::init(passphrase)?; |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn login(&mut self) -> io::Result<()> { |
|
|
|
@ -163,35 +147,33 @@ impl MPS {
|
|
|
|
|
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<()> { |
|
|
|
|
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!("[{}] {}", counter, id); |
|
|
|
|
counter += 1; |
|
|
|
|
println!("{}", id); |
|
|
|
|
//counter += 1;
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Show item. Id must exists
|
|
|
|
|
fn show(&mut self, id: &String) -> io::Result<()> { |
|
|
|
|
self.login()?; |
|
|
|
|
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); |
|
|
|
|
editor::open_to_show(&item.content) |
|
|
|
|
editor::open_to_show(&item.content)?; |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn add(&mut self, id: &String) -> io::Result<()> { |
|
|
|
@ -210,10 +192,10 @@ impl MPS {
|
|
|
|
|
// set empty string because there is no content yet
|
|
|
|
|
let content = editor::open_to_edit(&String::from(""))?; |
|
|
|
|
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<()> { |
|
|
|
|
self.login()?; |
|
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
@ -231,48 +213,25 @@ impl MPS {
|
|
|
|
|
let new_content = editor::open_to_edit(&item.content)?; |
|
|
|
|
item.content = new_content; |
|
|
|
|
st.update(item); |
|
|
|
|
st.dump() |
|
|
|
|
st.dump()?; |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Delete item by id, is must exist
|
|
|
|
|
fn delete(&mut self, id: &String) -> io::Result<()> { |
|
|
|
|
self.login()?; |
|
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
|
st.remove(id); |
|
|
|
|
st.dump() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// 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()?; |
|
|
|
|
let st = self.storage.as_mut().unwrap(); |
|
|
|
|
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( |
|
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
|
format!("No such item {}", id) |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if let Some(number) = &args.number { |
|
|
|
|
item_id = st.get_id_by_number(*number)?; |
|
|
|
|
// we can guarantee that id exists because we take it by id
|
|
|
|
|
} |
|
|
|
|
if args.id.is_none() && args.number.is_none() { |
|
|
|
|
if !st.contains(id) { |
|
|
|
|
return Err(io::Error::new( |
|
|
|
|
io::ErrorKind::InvalidInput, |
|
|
|
|
format!("Bag arguments") |
|
|
|
|
"No such item" |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
Ok(item_id.clone()) |
|
|
|
|
} |
|
|
|
|
st.remove(id); |
|
|
|
|
st.dump()?; |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn run_command() -> io::Result<()> { |
|
|
|
|
let cli = Cli::parse(); |
|
|
|
@ -284,28 +243,21 @@ fn run_command() -> io::Result<()> {
|
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.list()?; |
|
|
|
|
} |
|
|
|
|
Some(Commands::Show(args)) => { |
|
|
|
|
Some(Commands::Show { id }) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
let id = mps.item_id_by_item_id_args(args, true)?; |
|
|
|
|
mps.show(&id)?; |
|
|
|
|
mps.show(id)?; |
|
|
|
|
} |
|
|
|
|
Some(Commands::Add { id }) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.add(id)?; |
|
|
|
|
} |
|
|
|
|
Some(Commands::Edit(args)) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
let id = mps.item_id_by_item_id_args(args, false)?; |
|
|
|
|
mps.edit(&id)?; |
|
|
|
|
} |
|
|
|
|
Some(Commands::Delete(args)) => { |
|
|
|
|
Some(Commands::Edit { id }) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
let id = mps.item_id_by_item_id_args(args, true)?; |
|
|
|
|
mps.delete(&id)?; |
|
|
|
|
mps.edit(id)? |
|
|
|
|
} |
|
|
|
|
Some(Commands::Password) => { |
|
|
|
|
Some(Commands::Delete { id }) => { |
|
|
|
|
let mut mps = MPS::new(); |
|
|
|
|
mps.password()?; |
|
|
|
|
mps.delete(id)? |
|
|
|
|
} |
|
|
|
|
None => { |
|
|
|
|
match Storage::check_installed() { |
|
|
|
|