use anyhow::{Context, Result}; use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; use structopt::StructOpt; use ibis::config::{Config, ListOptions}; use ibis::courses::Course; use ibis::{rofi, util}; #[derive(StructOpt)] struct Cli { #[structopt(short, long)] debug: bool, #[structopt(subcommand)] cmd: Option, } #[derive(StructOpt)] enum Command { ListCourses(ListOptions), ListLectures(ListOptions), SelectCourse(ListOptions), SelectLecture, AddCourse(Course), AddLecture, Init, Check(ListOptions), Meetings(ListOptions), } // #[derive(StructOpt)] // struct ObjectStruct { // #[structopt(subcommand)] // object: Option, // } #[derive(StructOpt, Debug)] enum Object { Courses, Lectures, } fn main() -> Result<()> { let conf = Config { link_path: PathBuf::from("/home/jana/current-course"), files_root: PathBuf::from("/home/jana/uni"), note_extensions: vec![String::from("tex"), String::from("md"), String::from("org")], }; let args = Cli::from_args(); match args.cmd { Some(subcommand) => match subcommand { Command::ListCourses(options) => { for c in Course::list(&conf, &options)?.iter() { println!("{}", c); } } Command::ListLectures(options) => { for l in list_lectures(&conf, options)? { println!("{:?}", l); } } Command::AddCourse(course) => add(course, &conf, args.debug)?, Command::AddLecture => (), Command::SelectCourse(options) => rofi::select_course(&conf, &options)?, Command::SelectLecture => (), Command::Init => (), Command::Check(list_options) => Course::check(&conf, &list_options)?, Command::Meetings(options) => rofi::select_meeting(&conf, &options)?, }, None => println!("No command given"), }; Ok(()) } fn add(course: Course, conf: &Config, debug: bool) -> Result<()> { let toml = toml::to_string(&course)?; println!("{}", toml); let target_dir = [ conf.files_root .canonicalize()? .to_str() .expect("I hate paths"), // TODO "/", util::to_snail_case(&course.name).as_ref(), ] .concat(); let toml_path = PathBuf::from([&target_dir, "/course.toml"].concat()); if !(cfg!(debug_assertions) || debug) { if !conf.files_root.is_dir() { std::fs::create_dir_all(conf.files_root.canonicalize().with_context(|| { format!("The root path {:?} seems malformed", conf.files_root) })?)?; } if !PathBuf::from(&target_dir).is_dir() { std::fs::create_dir_all(&target_dir)?; } else { panic!("Folder {} already exists!", &target_dir); } let mut toml_handle = File::create(&toml_path).unwrap(); write!(toml_handle, "{}", toml).unwrap(); println!("Created files!") } println!("Path was {:?}", &toml_path); Ok(()) } fn list_lectures(conf: &Config, options: ListOptions) -> Result> { let lectures = util::glob_paths( if options.all { &conf.files_root } else { &conf.link_path }, "/**/lecture*", ); let lectures = lectures? .into_iter() .filter_map(|path| { if let Some(ext) = path.extension() { let ext = ext.to_owned().to_string_lossy().into(); if conf.note_extensions.contains(&ext) { Some(path) } else { None } } else { None } }) .collect(); Ok(lectures) }