use anyhow::{Context, Result}; use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; use structopt::StructOpt; use ibis::config::{ListOptions, CONFIG}; use ibis::courses::{Course, Meeting}; use ibis::{rofi, util}; #[derive(StructOpt)] struct Cli { #[structopt(short, long)] debug: bool, #[structopt(subcommand)] cmd: Command, } #[derive(StructOpt)] enum Command { ShowConfig, #[structopt(name = "show")] ShowCurrentCourse, ListCourses(ListOptions), ListLectures(ListOptions), SelectCourse(ListOptions), SelectLecture, AddCourse(Course), AddMeeting(Meeting), AddLecture, Init, Check(ListOptions), Meetings(ListOptions), } #[derive(StructOpt)] struct NewMeeting {} // #[derive(StructOpt)] // struct ObjectStruct { // #[structopt(subcommand)] // object: Option, // } #[derive(StructOpt, Debug)] enum Object { Courses, Lectures, } fn main() -> Result<()> { let args = Cli::from_args(); match args.cmd { Command::ListCourses(options) => { for c in Course::list(&options)?.iter() { println!("{}", c); } } Command::ListLectures(options) => { for l in list_lectures(options)? { println!("{:?}", l); } } Command::AddCourse(course) => add(course, args.debug)?, Command::AddLecture => (), Command::SelectCourse(options) => rofi::select_course(&options)?, Command::SelectLecture => (), Command::Init => (), Command::Check(list_options) => Course::check(&list_options)?, Command::Meetings(options) => rofi::select_meeting(&options)?, Command::ShowConfig => println!("{:?}", *CONFIG), Command::AddMeeting(_meeting) => (), Command::ShowCurrentCourse => println!("{:?}", Course::current()), //Course::current().meetings.push(meeting), }; Ok(()) } fn add(mut course: Course, debug: bool) -> Result<()> { if course.semesters().is_empty() { course.semesters_mut().push(util::get_current_semester()) } let toml = toml::to_string(&course)?; println!("{}", toml); let target_dir = [ CONFIG .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 !CONFIG.files_root.is_dir() { std::fs::create_dir_all(CONFIG.files_root.canonicalize().with_context(|| { format!("The root path {:?} seems malformed", CONFIG.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(options: ListOptions) -> Result> { let lectures = util::glob_paths( if options.all { &CONFIG.files_root } else { &CONFIG.link_path }, "/**/lecture*", ); let lectures = lectures? .into_iter() .filter(|path| { if let Some(ext) = path.extension() { let ext = ext.to_owned().to_string_lossy().into(); CONFIG.note_extensions.contains(&ext) } else { false } }) .collect(); Ok(lectures) }