Browse Source

better init

db-refactoring
Coin de Gamma 4 months ago
parent
commit
6896827b3c
  1. 38
      src/main.rs

38
src/main.rs

@ -29,23 +29,21 @@ enum Commands {
} }
enum InitError {
Reinit,
BadInit
}
fn is_inited() -> bool { fn is_inited() -> bool {
let path = Path::new(STORAGE_PATH); let path = Path::new(STORAGE_PATH);
return path.exists(); return path.exists();
} }
fn init() -> Result<(), InitError> { fn init() -> io::Result<()> {
if is_inited() { if is_inited() {
return Err(InitError::Reinit); return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted"));
} }
match fs::create_dir(STORAGE_PATH) { match fs::create_dir(STORAGE_PATH) {
Ok(_) => return Ok(()), Ok(_) => {
Err(_) => return Err(InitError::BadInit), 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(); let cli = Cli::parse();
match &cli.command { match &cli.command {
Some(Commands::Init) => { Some(Commands::Init) => {
init().map_err(|e| match e { init()?;
InitError::Reinit => std::io::Error::new(std::io::ErrorKind::AlreadyExists, "Reinitialization attempted"), println!("Initialization complete");
InitError::BadInit => std::io::Error::new(std::io::ErrorKind::Other, "Bad initialization"),
})?;
println!("Initializing storage and config.");
} }
Some(Commands::Add{..}) => { Some(Commands::Add{..}) => {
println!("about to add new item"); println!("about to add new item");
} }
None => { 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"); println!("Will be here init or list if storage inited");
} }
} }

Loading…
Cancel
Save