use std::{ env::current_dir, fs::{File, create_dir_all}, io::Write, }; use color_eyre::eyre::{Context, Result}; use fil_core::{Group, Kasten, Priority, Task, Zettel}; use tower_lsp::{LspService, Server}; use crate::{ cli::{Commands, ZettelSubcommand}, config::{Config, get_config_dir}, lsp::Backend, }; impl Commands { pub async fn process(self) -> Result<()> { match self { Self::Init { name } => { let dir = current_dir() .context("Failed to get current directory")? .join(&name); Kasten::initialize(dir.clone()).await?; // Write config that sets the filaments directory to current Dir! let config_str = dbg! {Config::generate(&dir)}?; // create the config Dir let config_dir = get_config_dir(); create_dir_all(config_dir).expect("creating the config dir should not error"); let mut config_file = File::create(get_config_dir().join("config.ron")) .context("Failed to create config file")?; write!(config_file, "{config_str}")?; println!("wrote config to {config_file:#?}"); println!("Initialized successfully!"); } Self::Zettel(zettel_sub_command) => { let conf = Config::parse()?; let mut kt = Kasten::instansiate(conf.fil_dir).await?; match zettel_sub_command { ZettelSubcommand::New { title } => { let zettel = Zettel::new(title, &mut kt, vec![]).await?; println!("Zettel Created! {zettel:#?}"); } ZettelSubcommand::List { by_tag: _by_tag } => { unimplemented!() } } } Self::Lsp => { // parse a Kasten let conf = Config::parse().with_context(|| "Failed to parse the config!!")?; let kt = Kasten::instansiate(conf.fil_dir) .await .with_context(|| "Failed to initialize a kasten!!")?; // get the in and out let stdin = tokio::io::stdin(); let stdout = tokio::io::stdout(); // start the LSP let (service, socket) = LspService::new(|client| Backend::new(client, kt)); Server::new(stdin, stdout, socket).serve(service).await; } Self::Todo(command) => { let conf = Config::parse()?; let mut kt = Kasten::instansiate(conf.fil_dir).await?; match command { super::TodoSubcommand::Group { name, parent_id } => { let group = Group::new(name, parent_id, &mut kt).await?; println!("created group {group:#?}"); } super::TodoSubcommand::Task { name, parent_id } => { //TODO: need to expose the due date and priority through the CLI let task = Task::new(name, parent_id, &mut kt, None, Priority::default()).await?; println!("created task {task:#?}"); } } } } Ok(()) } }