From 05fb399530c4e9c3f06b909b5e57a1aa85242a07 Mon Sep 17 00:00:00 2001 From: Andrei Stoica Date: Thu, 22 Feb 2024 01:49:57 -0500 Subject: [PATCH] added cli interface options and some error msgs --- src/cli/mod.rs | 13 +++++++++++++ src/main.rs | 44 ++++++++++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 src/cli/mod.rs diff --git a/src/cli/mod.rs b/src/cli/mod.rs new file mode 100644 index 0000000..2d7807f --- /dev/null +++ b/src/cli/mod.rs @@ -0,0 +1,13 @@ +use clap::{Parser, Subcommand}; + +#[derive(Parser, Debug)] +#[command(version, about)] +pub struct Args { + /// set config file to use + #[arg(short, long, value_name = "FILE")] + pub config: Option, + + /// show current config file + #[arg(short = 'C', long)] + pub current_config: bool, +} diff --git a/src/main.rs b/src/main.rs index 11a57cd..702b91c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,13 +15,8 @@ use comrak::{parse_document, Arena}; use comrak::{ComrakExtensionOptions, ComrakOptions, ComrakParseOptions}; use std::borrow::Borrow; use std::collections::HashMap; -<<<<<<< Updated upstream -use std::env; use std::fs::{create_dir_all, metadata, read, read_dir, File}; -======= -use std::fs::{read, read_dir, File}; ->>>>>>> Stashed changes -use std::io::Write; +use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::process::Command; use std::{env, str}; @@ -35,6 +30,7 @@ use std::{env, str}; #[derive(Debug)] enum ExitError { ConfigError(String), + IOError(String, io::Error), } fn main() -> Result<(), ExitError> { @@ -50,7 +46,10 @@ fn main() -> Result<(), ExitError> { if cfg_files.len() <= 0 { let status = Config::write_default(expected_cfg_files[0].to_str().unwrap()); if let Err(e) = status { - println!("Could not write to default cfg location: {:#?}", e); + return Err(ExitError::ConfigError(format!( + "Could not write to default cfg location: {:#?}", + e + ))); } } @@ -62,18 +61,27 @@ fn main() -> Result<(), ExitError> { let cfg = Config::load(cfg_file).unwrap(); println!("{:#?}", cfg); - let data_dir = get_data_dir( - &cfg.notes_dir - .clone() - .expect("Could not get notes dir from config"), - ); + let data_dir = match &cfg.notes_dir { + Some(dir) => get_data_dir(dir), + _ => { + return Err(ExitError::ConfigError( + "Could not get notes dir from config".to_string(), + )) + } + }; + if !metadata(&data_dir).is_ok() { - if let Err(e) = create_dir_all(&data_dir) { - println!( - "Could not create defult directory({}): {:#?}", - &data_dir.to_str().unwrap(), - e - ); + match create_dir_all(&data_dir) { + Err(e) => { + return Err(ExitError::IOError( + format!( + "Could not create defult directory: {}", + &data_dir.to_str().unwrap(), + ), + e, + )) + } + _ => (), }; } println!("dir = {}", data_dir.to_str().unwrap());