A small tool for storing passwords locally with git sync
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
4.8 KiB

3 months ago
mod storage;
3 months ago
mod editor;
4 months ago
3 months ago
use storage::{Storage, Item};
4 months ago
use clap::{Parser, Subcommand};
3 months ago
use rpassword;
4 months ago
use std::io::{self, Write};
use std::process;
4 months ago
#[derive(Parser)]
4 months ago
#[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>,
}
#[derive(Subcommand)]
enum Commands {
3 months ago
3 months ago
/// Initialization of storage and config, use this in first time of usage of mps
Init,
3 months ago
/// Lists all ids stored in db
List,
3 months ago
/// Show content of item
Show {
#[arg(value_name="item_id")]
id: String
},
/// Adds new item with unique id to the db
Add {
#[arg(value_name="item_id")]
4 months ago
id: String
4 months ago
},
3 months ago
3 months ago
/// Edit item content
Edit {
3 months ago
#[arg(value_name="item_id")]
id: String
}
}
3 months ago
enum PROMPT {
YES,
NO
}
fn get_prompt(question: &str) -> io::Result<PROMPT> {
print!("{} [Y/n] ", question);
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let input = input.trim().to_lowercase();
match input.as_str() {
"y" => Ok(PROMPT::YES),
"yes" => Ok(PROMPT::YES),
"n" => Ok(PROMPT::NO),
"no" => Ok(PROMPT::NO),
_ => Ok(PROMPT::YES),
}
}
// TODO: change password functionality
3 months ago
// TODO: return storage inited
3 months ago
fn login() -> io::Result<String> {
3 months ago
print!("Enter passphrase for storage: ");
io::stdout().flush()?;
3 months ago
// TODO: check in db
// TODO: return error if db is not inited
let password = rpassword::read_password()?;
if password != "pass" {
3 months ago
return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase"));
3 months ago
}
3 months ago
print!("\x1B[1A\x1B[2K"); // Move cursor up one line and clear that line
io::stdout().flush()?; // Ensure the changes are reflected immediately
3 months ago
Ok(String::from(password))
}
4 months ago
4 months ago
fn init() -> io::Result<()> {
3 months ago
if Storage::is_inited() {
4 months ago
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted"));
}
3 months ago
print!("Enter passphrase for storage: ");
3 months ago
io::stdout().flush()?;
3 months ago
// TODO: rename to passphrase
3 months ago
let password = rpassword::read_password()?;
3 months ago
print!("Reenter passphrase: ");
io::stdout().flush()?;
3 months ago
let password2 = rpassword::read_password()?;
if password != password2 {
3 months ago
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passwords must be equal"));
3 months ago
}
3 months ago
Storage::init(password)?;
4 months ago
Ok(())
}
3 months ago
fn list() -> io:: Result<()> {
3 months ago
// TODO: get storage from login
3 months ago
let passphrase = login()?;
let st = Storage::from_db(passphrase)?;
3 months ago
for id in st.ids() {
println!("{}", id);
3 months ago
}
Ok(())
}
4 months ago
fn add(id: &String) -> io::Result<()> {
3 months ago
let passphrase = login()?;
let mut st = Storage::from_db(passphrase)?;
3 months ago
if st.contains(id) {
4 months ago
// TODO: ask to edit existing in outer function which invoked this one
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Dublicate item id: {}. Item id's must be unique.", id)
3 months ago
// TODO: ask to edit [Y/n]
4 months ago
))
}
3 months ago
let content = editor::open_to_edit()?;
3 months ago
st.add(Item::from(id.clone(), content));
3 months ago
st.dump()?;
3 months ago
4 months ago
Ok(())
}
3 months ago
fn edit(id: &String) -> io::Result<()> {
// TODO: implement
Ok(())
}
3 months ago
fn show(id: &String) -> io::Result<()> {
let passphrase = login()?;
let mut st = Storage::from_db(passphrase)?;
if !st.contains(id) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Can`t find id: {}", id)
));
4 months ago
}
3 months ago
let item = st.get(id);
3 months ago
editor::open_to_show(&item.content)?;
4 months ago
Ok(())
}
fn run_command() -> io::Result<()> {
let cli = Cli::parse();
match &cli.command {
Some(Commands::Init) => {
4 months ago
init()?;
}
3 months ago
Some(Commands::List) => {
list()?;
}
3 months ago
Some(Commands::Show { id }) => {
3 months ago
show(id)?;
}
3 months ago
Some(Commands::Add { id }) => {
add(id)?;
}
Some(Commands::Edit { id }) => {
edit(id)?
4 months ago
}
None => {
3 months ago
if !Storage::is_inited() {
3 months ago
match get_prompt("Do you want to init your storage?")? {
PROMPT::YES => init()?,
3 months ago
PROMPT::NO => Storage::print_init_hint(),
3 months ago
}
4 months ago
} else {
3 months ago
list()?
4 months ago
}
}
}
Ok(())
4 months ago
}
fn main() {
match run_command() {
Ok(()) => return,
Err(e) => {
println!("{}", e);
process::exit(2); // TODO: better codes for different errors
}
}
}
3 months ago