Add separate utils module

This commit is contained in:
Kiana Sheibani 2023-11-26 00:22:08 -05:00
parent 1aa85bd989
commit faf0f30d36
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 29 additions and 26 deletions

View file

@ -5,7 +5,6 @@
use clap::{Parser, Subcommand};
use std::io::{self, Write};
use std::path::PathBuf;
use std::process::exit;
use std::time::SystemTime;
use time_format::strftime_utc;
@ -15,20 +14,8 @@ mod datasets;
use datasets::*;
mod sync;
use sync::*;
const SECS_IN_HR: u64 = 3600;
const SECS_IN_DAY: u64 = SECS_IN_HR * 24;
const SECS_IN_WEEK: u64 = SECS_IN_DAY * 7;
pub fn error(msg: &str, code: i32) -> ! {
println!("\nERROR: {}", msg);
exit(code)
}
pub fn issue(msg: &str, code: i32) -> ! {
println!("\n{}", msg);
exit(code)
}
mod util;
use util::*;
/// ## CLI Structs
@ -190,17 +177,6 @@ fn dataset_list() {
}
}
fn read_string() -> String {
let mut line = String::new();
io::stdout()
.flush()
.unwrap_or_else(|_| error("Could not access stdout", 2));
io::stdin()
.read_line(&mut line)
.unwrap_or_else(|_| error("Could not read from stdin", 2));
line.trim().to_owned()
}
fn dataset_new(name: Option<String>, auth_token: Option<String>) {
let config_dir = dirs::config_dir().expect("Could not determine config directory");

27
src/util.rs Normal file
View file

@ -0,0 +1,27 @@
use std::io::{self, Write};
use std::process::exit;
pub const SECS_IN_HR: u64 = 3600;
pub const SECS_IN_DAY: u64 = SECS_IN_HR * 24;
pub const SECS_IN_WEEK: u64 = SECS_IN_DAY * 7;
pub fn error(msg: &str, code: i32) -> ! {
println!("\nERROR: {}", msg);
exit(code)
}
pub fn issue(msg: &str, code: i32) -> ! {
println!("\n{}", msg);
exit(code)
}
pub fn read_string() -> String {
let mut line = String::new();
io::stdout()
.flush()
.unwrap_or_else(|_| error("Could not access stdout", 2));
io::stdin()
.read_line(&mut line)
.unwrap_or_else(|_| error("Could not read from stdin", 2));
line.trim().to_owned()
}