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.

88 lines
2.3 KiB

use clap::{Parser, Subcommand};
use std::fs;
use std::io;
use std::fmt;
use std::path::Path;
const STORAGE_PATH: &str = "storage";
#[derive(Parser)]
#[command(name = "mps", version = "0.0.1", about = "MyPasswordStorage: Tool for storing your passwords locally with synchronization with git.")]
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
}
}
enum RuntimeError {
Reinit,
BadInit
}
impl fmt::Display for RuntimeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RuntimeError::Reinit => write!(f, "Initialization can't be second time. Remove store folder if you want new inizilization"),
RuntimeError::BadInit => write!(f, "Can`t create storage folder")
}
}
}
impl From<RuntimeError> for std::io::Error {
fn from(err: RuntimeError) -> std::io::Error {
match err {
RuntimeError::Reinit => std::io::Error::new(std::io::ErrorKind::AlreadyExists, "Reinitialization attempted"),
RuntimeError::BadInit => std::io::Error::new(std::io::ErrorKind::Other, "Bad initialization"),
}
}
}
fn init() -> Result<(), RuntimeError> {
let path = Path::new(STORAGE_PATH);
if path.exists() {
return Err(RuntimeError::Reinit);
}
match fs::create_dir(STORAGE_PATH) {
Ok(_) => return Ok(()),
Err(_) => return Err(RuntimeError::BadInit),
}
Ok(())
}
fn main() -> io::Result<()> {
let cli = Cli::parse();
match &cli.command {
Some(Commands::Init) => {
init().map_err(|e| match e {
RuntimeError::Reinit => std::io::Error::new(std::io::ErrorKind::AlreadyExists, "Reinitialization attempted"),
RuntimeError::BadInit => std::io::Error::new(std::io::ErrorKind::Other, "Bad initialization"),
})?;
println!("Initializing storage and config.");
}
Some(Commands::Add{..}) => {
println!("about to add new item");
}
None => {
println!("Will be here init or list if storage inited");
}
}
Ok(())
4 months ago
}