use std::{ env::current_dir, fs::{File, create_dir_all}, io::Write, }; use color_eyre::eyre::{Context, Result}; use tower_lsp::{LspService, Server}; use crate::{ cli::{Commands, ZettelSubcommand}, config::{Config, get_config_dir}, lsp::Backend, types::{Group, Kasten, Priority, Task, Zettel}, }; impl Commands { pub async fn process(self) -> Result<()> { match self { Self::Init { name } => { // create the directory 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:#?}"); // report status! 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 } => {} } } Self::Lsp => { 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!!")?; let stdin = tokio::io::stdin(); let stdout = tokio::io::stdout(); 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 } => { // lets create a tag for this first group first let group = Group::new(name, parent_id, &mut kt).await?; println!("created group {group:#?}"); } super::TodoSubcommand::Task { name, parent_id } => { let task = Task::new(name, parent_id, &mut kt, None, Priority::default()).await?; println!("created task {task:#?}"); } } } Self::Test => { let conf = Config::parse()?; let kt = Kasten::instansiate(conf.fil_dir).await?; println!("kt: {kt:#?}"); } } Ok(()) } }