use clap::{Parser, Subcommand}; use std::fs; use std::io::{self, Write, BufRead}; use std::path::Path; const STORAGE_FOLDER: &str = "storage"; const STORAGE_PATH: &str = "storage/db.mps"; // TODO: concat from STORAGE_FOLDER #[derive(Parser)] #[command(name = "mps", version = "0.0.1", about = "MyPasswordStorage: Tool for storing your passwords locally with git synchronization")] 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")] id: String }, /// Lists all ids stored in db List } fn is_inited() -> bool { let path = Path::new(STORAGE_FOLDER); return path.exists(); } fn init() -> io::Result<()> { if is_inited() { return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); } fs::create_dir(STORAGE_FOLDER)?; println!("Storage folder created"); fs::File::create(STORAGE_PATH)?; println!("Storage db created"); println!("Initialization complete. It's required to add folder `{}` under git manually.", STORAGE_FOLDER); Ok(()) } fn add(id: &String) -> io::Result<()> { let mut file = fs::OpenOptions::new() .write(true) .append(true) .open(STORAGE_PATH)?; writeln!(file, "{}", id)?; Ok(()) } // TODO: make this to return just set on values, will need later to check ids fn list() -> io:: Result<()> { let file = fs::File::open(STORAGE_PATH)?; let reader = io::BufReader::new(file); for line in reader.lines() { match line { Ok(content) => { println!("{}", content) }, Err(e) => { eprintln!("Error reading line , {}", e); } } } Ok(()) } fn main() -> io::Result<()> { let cli = Cli::parse(); match &cli.command { Some(Commands::Init) => { init()?; } Some(Commands::Add{ id }) => { add(id)?; } Some(Commands::List) => { list()?; } 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") } } } Ok(()) }