From 293113949b67ce42dc0804f0ed0c879c0a85dec9 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Tue, 13 Jun 2023 15:11:25 +0700 Subject: [PATCH] Simplified dbpath cli argument handling --- src/main.rs | 80 ++++++----------------------------------------------- 1 file changed, 8 insertions(+), 72 deletions(-) diff --git a/src/main.rs b/src/main.rs index 127f2dc..517790d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,17 +6,10 @@ use crossterm::{ }; use kanban_tui::{Project, State}; use rusqlite::Connection; -use std::{ - error::Error, - fs::{File, OpenOptions}, - io::{self, Write}, - path::PathBuf, -}; +use std::{error::Error, io, path::PathBuf}; use tui::backend::CrosstermBackend; use tui::Terminal; -const DEFAULT_DATABASE_NAME: &str = "kanban.json"; - #[derive(Debug, Parser)] #[command(name = "kanban")] /// kanban-tui is a simple, interactive TUI based task manager using kanban columns @@ -26,72 +19,17 @@ pub struct CliArgs { pub filepath: Option, } -// TODO: This should just return a struct beacuse we should add a -// "should_quit" thing instead of calling exit(0) here -fn prompt_project_init(default_name: &str) -> (String, io::Result) { - let mut input = String::new(); - - println!("Database not found, select the name of the database if it exists or enter a name to start a new project"); - print!("Database name (default: {default_name}): "); - io::stdout().flush().unwrap(); - - let result = io::stdin().read_line(&mut input); - let input = input.trim(); - - let filename = match result { - Ok(b) if b == 0 => std::process::exit(0), - Ok(b) if b > 0 && !input.is_empty() => input, - _ => default_name, - }; - - // TODO: This might be a good time to prompt the user if they want - // to change the default column names - - ( - filename.to_string(), - OpenOptions::new() - .write(true) - .read(true) - .create(true) - .open(filename), - ) -} - #[async_std::main] async fn main() -> anyhow::Result<(), Box> { - let (_filepath, _file) = match CliArgs::parse() { - CliArgs { - filepath: Some(filepath), - } => { - let fpath = filepath.into_os_string().into_string().unwrap(); - let file = OpenOptions::new().write(true).read(true).open(&fpath); + let dbpath = CliArgs::parse() + .filepath + .map(PathBuf::into_os_string) + .unwrap_or("kanban.db".into()); - if let Ok(f) = file { - (fpath, f) - } else { - let (fp, fname) = prompt_project_init(&fpath); - (fp, fname.unwrap()) - } - } - CliArgs { filepath: None } => { - let file = OpenOptions::new() - .write(true) - .read(true) - .open(DEFAULT_DATABASE_NAME); - if let Ok(f) = file { - (DEFAULT_DATABASE_NAME.to_string(), f) - } else { - let (fp, fname) = prompt_project_init(DEFAULT_DATABASE_NAME); - (fp, fname.unwrap()) - } - } - }; + let conn = Connection::open(dbpath)?; - let db_pool = Connection::open("db.sqlite")?; - - // let project = Project::load(_filepath, &_file)?; - let project = Project::load(&db_pool).await?; - let mut state = State::new(db_pool, project); + let project = Project::load(&conn).await?; + let mut state = State::new(conn, project); enable_raw_mode()?; let mut stdout = io::stdout(); @@ -104,8 +42,6 @@ async fn main() -> anyhow::Result<(), Box> { kanban_tui::handle(&mut state)?; } - // state.project.save(); - // restore terminal disable_raw_mode()?; crossterm::execute!(