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 {
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");
}
}

Loading…
Cancel
Save