|
|
@ -7,20 +7,11 @@ use db::{DB, Item}; |
|
|
|
use clap::{Parser, Subcommand}; |
|
|
|
use clap::{Parser, Subcommand}; |
|
|
|
use rpassword; |
|
|
|
use rpassword; |
|
|
|
|
|
|
|
|
|
|
|
use once_cell::sync::Lazy; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::fs; |
|
|
|
use std::fs; |
|
|
|
use std::io::{self, Write}; |
|
|
|
use std::io::{self, Write}; |
|
|
|
use std::path::Path; |
|
|
|
|
|
|
|
use std::process; |
|
|
|
use std::process; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static STORAGE_FOLDER: &str = "storage"; |
|
|
|
|
|
|
|
static STORAGE_PATH: Lazy<String> = Lazy::new(|| {
|
|
|
|
|
|
|
|
format!("{}/db.mps", STORAGE_FOLDER) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[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 = "0.0.1", about = "MyPasswordStorage: Tool for storing your passwords locally with git synchronization")] |
|
|
|
struct Cli { |
|
|
|
struct Cli { |
|
|
@ -70,56 +61,43 @@ fn get_prompt(question: &str) -> io::Result<PROMPT> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO: change password functionality
|
|
|
|
// TODO: change password functionality
|
|
|
|
|
|
|
|
|
|
|
|
fn login() -> io::Result<String> { |
|
|
|
fn login() -> io::Result<String> { |
|
|
|
println!("Enter passphrase: "); |
|
|
|
// TODO: check if inited
|
|
|
|
|
|
|
|
print!("Enter passphrase for storage: "); |
|
|
|
|
|
|
|
io::stdout().flush()?; |
|
|
|
// TODO: check in db
|
|
|
|
// TODO: check in db
|
|
|
|
// TODO: return error if db is not inited
|
|
|
|
// TODO: return error if db is not inited
|
|
|
|
let password = rpassword::read_password()?; |
|
|
|
let password = rpassword::read_password()?; |
|
|
|
if password != "pass" { |
|
|
|
if password != "pass" { |
|
|
|
return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong password")); |
|
|
|
return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase")); |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(String::from(password)) |
|
|
|
Ok(String::from(password)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn is_inited() -> bool { |
|
|
|
|
|
|
|
let path = Path::new(STORAGE_FOLDER); |
|
|
|
|
|
|
|
return path.exists(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn init() -> io::Result<()> { |
|
|
|
fn init() -> io::Result<()> { |
|
|
|
if is_inited() { |
|
|
|
if db::DB::is_inited() { |
|
|
|
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); |
|
|
|
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); |
|
|
|
} |
|
|
|
} |
|
|
|
print!("Enter passphrase for db: "); |
|
|
|
print!("Enter passphrase for storage: "); |
|
|
|
io::stdout().flush()?; |
|
|
|
io::stdout().flush()?; |
|
|
|
|
|
|
|
// TODO: rename to passphrase
|
|
|
|
let password = rpassword::read_password()?; |
|
|
|
let password = rpassword::read_password()?; |
|
|
|
print!("Reenter passphrase: "); |
|
|
|
print!("Reenter passphrase: "); |
|
|
|
io::stdout().flush()?; |
|
|
|
io::stdout().flush()?; |
|
|
|
let password2 = rpassword::read_password()?; |
|
|
|
let password2 = rpassword::read_password()?; |
|
|
|
|
|
|
|
|
|
|
|
if password != password2 { |
|
|
|
if password != password2 { |
|
|
|
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passwords should be equal")); |
|
|
|
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passwords must be equal")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fs::create_dir(STORAGE_FOLDER)?; |
|
|
|
db:::DB::init(password)?; |
|
|
|
println!("Storage folder created"); |
|
|
|
|
|
|
|
//let mut db = DB::init(&*STORAGE_PATH, pass)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: enter password
|
|
|
|
|
|
|
|
// TODO: dont create storate if master password hasn't entered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs::File::create(&*STORAGE_PATH)?; |
|
|
|
|
|
|
|
println!("Storage db created."); |
|
|
|
|
|
|
|
println!("Initialization complete."); |
|
|
|
|
|
|
|
println!(""); |
|
|
|
|
|
|
|
println!("Now it's required to add folder `{}` under git manually.", STORAGE_FOLDER); |
|
|
|
|
|
|
|
println!("Don't worry it's going to be encrypted."); |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn add(id: &String) -> io::Result<()> { |
|
|
|
fn add(id: &String) -> io::Result<()> { |
|
|
|
let mut db = DB::new(&*STORAGE_PATH, String::from(""))?; |
|
|
|
// TODO: get login passphrase
|
|
|
|
|
|
|
|
let mut db = DB::new(String::from(""))?; |
|
|
|
if db.contains(id) { |
|
|
|
if db.contains(id) { |
|
|
|
// TODO: ask to edit existing in outer function which invoked this one
|
|
|
|
// TODO: ask to edit existing in outer function which invoked this one
|
|
|
|
return Err(io::Error::new( |
|
|
|
return Err(io::Error::new( |
|
|
@ -131,12 +109,12 @@ fn add(id: &String) -> io::Result<()> { |
|
|
|
let content = editor::open_to_edit()?; |
|
|
|
let content = editor::open_to_edit()?; |
|
|
|
db.items.insert(Item::from(id.clone(), content)); |
|
|
|
db.items.insert(Item::from(id.clone(), content)); |
|
|
|
db.dump()?; |
|
|
|
db.dump()?; |
|
|
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn list() -> io:: Result<()> { |
|
|
|
fn list() -> io:: Result<()> { |
|
|
|
let db = DB::new(&STORAGE_PATH, String::from(""))?; |
|
|
|
let db = DB::new(String::from(""))?; |
|
|
|
let mut vec: Vec<_> = db.items.iter().collect(); |
|
|
|
let mut vec: Vec<_> = db.items.iter().collect(); |
|
|
|
vec.sort(); |
|
|
|
vec.sort(); |
|
|
|
for item in &vec { |
|
|
|
for item in &vec { |
|
|
@ -158,7 +136,7 @@ fn run_command() -> io::Result<()> { |
|
|
|
list()?; |
|
|
|
list()?; |
|
|
|
} |
|
|
|
} |
|
|
|
None => { |
|
|
|
None => { |
|
|
|
if !is_inited() { |
|
|
|
if !db::DB::is_inited() { |
|
|
|
match get_prompt("Do you want to init your storage?")? { |
|
|
|
match get_prompt("Do you want to init your storage?")? { |
|
|
|
PROMPT::YES => init()?, |
|
|
|
PROMPT::YES => init()?, |
|
|
|
PROMPT::NO => { |
|
|
|
PROMPT::NO => { |
|
|
@ -167,7 +145,7 @@ fn run_command() -> io::Result<()> { |
|
|
|
println!(" git clone <your_storage_git_url> {}", STORAGE_FOLDER); |
|
|
|
println!(" git clone <your_storage_git_url> {}", STORAGE_FOLDER); |
|
|
|
println!("to init manually your storage and config") |
|
|
|
println!("to init manually your storage and config") |
|
|
|
}, |
|
|
|
}, |
|
|
|
}
|
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
login()?; |
|
|
|
login()?; |
|
|
|
} |
|
|
|
} |
|
|
@ -184,5 +162,5 @@ fn main() { |
|
|
|
process::exit(2); // TODO: better codes for different errors
|
|
|
|
process::exit(2); // TODO: better codes for different errors
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|