From 6896827b3caf91754bf296b5c1c8a75c9583ad4f Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Tue, 3 Sep 2024 16:03:16 +0000 Subject: [PATCH] better init --- src/main.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2f2431b..8f6f99b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,23 +29,21 @@ enum Commands { } -enum InitError { - Reinit, - BadInit -} - fn is_inited() -> bool { let path = Path::new(STORAGE_PATH); return path.exists(); } -fn init() -> Result<(), InitError> { +fn init() -> io::Result<()> { if is_inited() { - return Err(InitError::Reinit); + return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); } match fs::create_dir(STORAGE_PATH) { - Ok(_) => return Ok(()), - Err(_) => return Err(InitError::BadInit), + Ok(_) => { + println!("Initialization complete"); + return Ok(()); + }, + Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Bad initialization")), } } @@ -53,16 +51,28 @@ fn main() -> io::Result<()> { let cli = Cli::parse(); match &cli.command { Some(Commands::Init) => { - init().map_err(|e| match e { - InitError::Reinit => std::io::Error::new(std::io::ErrorKind::AlreadyExists, "Reinitialization attempted"), - InitError::BadInit => std::io::Error::new(std::io::ErrorKind::Other, "Bad initialization"), - })?; - println!("Initializing storage and config."); + 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"); } }