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.

106 lines
2.8 KiB

use clap::{Parser, Subcommand};
use std::fs;
4 months ago
use std::io::{self, Write};
use std::path::Path;
4 months ago
4 months ago
const STORAGE_FOLDER: &str = "storage";
const STORAGE_PATH: &str = "storage/db.mps"; // TODO: concat from STORAGE_FOLDER
#[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 {
/// Initialisation 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")]
input: String
}
}
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"));
}
4 months ago
match fs::create_dir(STORAGE_FOLDER) {
4 months ago
Ok(_) => {
4 months ago
println!("Create folder for storage"); // TODO: make it only in debug mode
4 months ago
},
4 months ago
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Can`t create storage folder")),
}
4 months ago
match fs::File::create(STORAGE_PATH) {
Ok(_) => {
println!("Storage db created");
},
Err(_) => {
return Err(io::Error::new(io::ErrorKind::Other, "Can`t create mps.db")); // TODO: better error, not just Other
}
}
Ok(())
}
4 months ago
fn add() -> io::Result<()> {
let mut file = match fs::OpenOptions::new()
.write(true)
.open(STORAGE_PATH)
{
Ok(file) => file,
Err(_) => {
return Err(io::Error::new(io::ErrorKind::Other, "can`t open database")); // TODO: better error handling
}
};
writeln!(file, "new item")?;
Ok(())
}
fn main() -> io::Result<()> {
let cli = Cli::parse();
match &cli.command {
Some(Commands::Init) => {
4 months ago
init()?;
println!("Initialization complete");
}
Some(Commands::Add{..}) => {
4 months ago
add()?;
}
None => {
4 months ago
if !is_inited() {
println!("Do you want to init your storage? [Y/n]");
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read answer");
let input = input.trim().to_lowercase();
match input.as_str() {
"y" => init()?,
"n" => println!("You chose No."),
_ => init()?,
}
} else {
println!("login, not implemented yet")
}
}
}
Ok(())
4 months ago
}