Improve dialog if no datasets exist

This commit is contained in:
Kiana Sheibani 2023-10-03 23:21:31 -04:00
parent 1421720b2b
commit ad6445bcd2
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -3,6 +3,7 @@
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use std::io::{self, Write}; use std::io::{self, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit;
mod queries; mod queries;
use queries::*; use queries::*;
@ -12,13 +13,11 @@ mod sync;
use sync::*; use sync::*;
pub fn error(msg: &str, code: i32) -> ! { pub fn error(msg: &str, code: i32) -> ! {
use std::process::exit;
println!("\nERROR: {}", msg); println!("\nERROR: {}", msg);
exit(code) exit(code)
} }
pub fn issue(msg: &str, code: i32) -> ! { pub fn issue(msg: &str, code: i32) -> ! {
use std::process::exit;
println!("\n{}", msg); println!("\n{}", msg);
exit(code) exit(code)
} }
@ -222,15 +221,25 @@ fn sync(datasets: Vec<String>, all: bool, auth_token: Option<String>) {
let connection = let connection =
open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 1)); open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 1));
let all_datasets = list_dataset_names(&connection).unwrap();
#[allow(unused_must_use)] #[allow(unused_must_use)]
let datasets = if all { let datasets = if all {
list_dataset_names(&connection).unwrap() all_datasets
} else if datasets.len() == 0 { } else if datasets.len() == 0 {
print!("No datasets provided; create a new one? (y/n) "); if all_datasets.len() == 0 {
if read_string() == "y" { print!("No datasets exist; create one? (y/n) ");
dataset_new(Some(String::from("default")), Some(auth.clone())); if let Some('y') = read_string().chars().next() {
dataset_new(Some(String::from("default")), Some(auth.clone()));
vec![String::from("default")]
} else {
error("No datasets specified and no default dataset", 1)
}
} else if all_datasets.iter().any(|x| x == "default") {
vec![String::from("default")]
} else {
error("No datasets specified and no default dataset", 1);
} }
vec![String::from("default")]
} else { } else {
datasets datasets
}; };
@ -240,7 +249,7 @@ fn sync(datasets: Vec<String>, all: bool, auth_token: Option<String>) {
.expect("Error communicating with SQLite") .expect("Error communicating with SQLite")
.unwrap_or_else(|| error(&format!("Dataset {} does not exist!", dataset), 1)); .unwrap_or_else(|| error(&format!("Dataset {} does not exist!", dataset), 1));
sync_dataset(&connection, &dataset, dataset_config, &auth).unwrap_or_else(|err| { sync_dataset(&connection, &dataset, dataset_config, &auth).unwrap_or_else(|_| {
connection.execute("ROLLBACK;").unwrap(); connection.execute("ROLLBACK;").unwrap();
error("Error communicating with SQLite", 2) error("Error communicating with SQLite", 2)
}); });