Compare commits

...

2 Commits

Author SHA1 Message Date
Andrei Stoica 05fb399530 added cli interface options and some error msgs 2024-02-22 01:49:57 -05:00
Andrei Stoica 695e31c7e4 fixed merge conflict 2024-02-22 01:35:07 -05:00
3 changed files with 54 additions and 16 deletions

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
chrono = "0.4.26"
clap = { version = "4.5.1", features = ["derive"] }
comrak = "0.18.0"
figment = { version = "0.10.10", features = ["env", "serde_json", "json"] }
regex = "1.8.4"

13
src/cli/mod.rs Normal file
View File

@ -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<String>,
/// show current config file
#[arg(short = 'C', long)]
pub current_config: bool,
}

View File

@ -1,6 +1,10 @@
mod cli;
mod config;
mod todo;
use crate::cli::Args;
use clap::Parser;
use crate::config::Config;
use crate::todo::File as TodoFile;
use crate::todo::{Status as TaskStatus, TaskGroup};
@ -11,19 +15,25 @@ use comrak::{parse_document, Arena};
use comrak::{ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
use std::borrow::Borrow;
use std::collections::HashMap;
use std::env;
use std::fs::{create_dir_all, metadata, read, read_dir, File};
use std::io::Write;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
use std::{env, str};
//TODO handle unwraps and errors more uniformly
//TODO refactor creating new file
//TODO clean up verbose printing
//TODO create custom errors for better error handling
//TODO Default path for note_dir should start with curent path not home
fn main() {
#[derive(Debug)]
enum ExitError {
ConfigError(String),
IOError(String, io::Error),
}
fn main() -> Result<(), ExitError> {
let expected_cfg_files = Config::expected_locations().unwrap();
println!("{:#?}", expected_cfg_files);
let cfg_files: Vec<&Path> = expected_cfg_files
@ -36,7 +46,10 @@ fn main() {
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
)));
}
}
@ -48,18 +61,27 @@ fn main() {
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({}): {:#?}",
match create_dir_all(&data_dir) {
Err(e) => {
return Err(ExitError::IOError(
format!(
"Could not create defult directory: {}",
&data_dir.to_str().unwrap(),
e
);
),
e,
))
}
_ => (),
};
}
println!("dir = {}", data_dir.to_str().unwrap());
@ -126,6 +148,8 @@ fn main() {
.args([current_file])
.status()
.expect(format!("failed to launch editor {}", "vim").as_str());
Ok(())
}
fn get_filepath(data_dir: &PathBuf, date: &NaiveDate) -> PathBuf {