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.

147 lines
3.6 KiB

4 months ago
mod db;
3 months ago
mod editor;
4 months ago
use db::{DB, Item};
4 months ago
use once_cell::sync::Lazy;
4 months ago
use clap::{Parser, Subcommand};
4 months ago
use std::fs;
4 months ago
use std::io::{self, Write};
use std::path::Path;
4 months ago
static STORAGE_FOLDER: &str = "storage";
static STORAGE_PATH: Lazy<String> = Lazy::new(|| {
format!("{}/db.mps", STORAGE_FOLDER)
});
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
/// Initialization of storage and config, use this in first time of usage of mps
Init,
/// Adds new item with unique id to the db
Add {
#[arg(value_name="item_id")]
4 months ago
id: String
4 months ago
},
/// Lists all ids stored in db
List
3 months ago
}
4 months ago
4 months ago
fn is_inited() -> bool {
4 months ago
let path = Path::new(STORAGE_FOLDER);
4 months ago
return path.exists();
}
4 months ago
fn init() -> io::Result<()> {
4 months ago
if is_inited() {
4 months ago
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted"));
}
fs::create_dir(STORAGE_FOLDER)?;
println!("Storage folder created");
4 months ago
fs::File::create(&*STORAGE_PATH)?;
4 months ago
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.");
4 months ago
Ok(())
}
4 months ago
fn add(id: &String) -> io::Result<()> {
let mut db = DB::new(&*STORAGE_PATH)?;
if db.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: implement encryption
let content = editor::open_to_edit()?;
db.items.insert(Item::from(id.clone(), content));
4 months ago
db.dump()?;
4 months ago
4 months ago
Ok(())
}
4 months ago
fn list() -> io:: Result<()> {
let db = DB::new(&STORAGE_PATH)?;
4 months ago
let mut vec: Vec<_> = db.items.iter().collect();
4 months ago
vec.sort();
for item in &vec {
4 months ago
println!("{}", item);
4 months ago
}
Ok(())
}
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),
3 months ago
"yes" => Ok(PROMPT::YES),
3 months ago
"n" => Ok(PROMPT::NO),
3 months ago
"no" => Ok(PROMPT::NO),
3 months ago
_ => Ok(PROMPT::YES),
}
}
fn main() -> io::Result<()> {
let cli = Cli::parse();
match &cli.command {
Some(Commands::Init) => {
4 months ago
init()?;
}
4 months ago
Some(Commands::Add{ id }) => {
add(id)?;
}
4 months ago
Some(Commands::List) => {
list()?;
}
None => {
4 months ago
if !is_inited() {
3 months ago
match get_prompt("Do you want to init your storage?")? {
PROMPT::YES => init()?,
PROMPT::NO => {
println!("mps can work only when storage inited.");
3 months ago
println!("Hint: you can restore your storage if you have it already:");
println!(" git clone <your_storage_git_url> {}", STORAGE_FOLDER);
println!("to init manually your storage and config")
3 months ago
},
3 months ago
}
4 months ago
} else {
println!("login, not implemented yet")
}
}
}
Ok(())
4 months ago
}