use clap::{Parser, Subcommand}; use std::fs; use std::io; 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, } #[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 } } fn is_inited() -> bool { let path = Path::new(STORAGE_PATH); return path.exists(); } fn init() -> io::Result<()> { if is_inited() { return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); } match fs::create_dir(STORAGE_PATH) { Ok(_) => { println!("Initialization complete"); return Ok(()); }, Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Bad initialization")), } } fn main() -> io::Result<()> { let cli = Cli::parse(); match &cli.command { Some(Commands::Init) => { init()?; println!("Initialization complete"); } Some(Commands::Add{..}) => { println!("about to add new item"); } None => { 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") } println!("Will be here init or list if storage inited"); } } Ok(()) }