| 187 | /// - User interaction errors |
| 188 | #[tokio::main] |
| 189 | async fn main() -> Result<()> { |
| 190 | let args = CliArgs::parse(); |
| 191 | |
| 192 | let cmd = &mut <CliArgs as clap::CommandFactory>::command(); |
| 193 | if let Some(generator) = args.generator { |
| 194 | clap_complete::generate(generator, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); |
| 195 | return Ok(()); |
| 196 | } |
| 197 | |
| 198 | // Handle the command, using TUI as default when enabled |
| 199 | let command = args.command.clone().unwrap_or_else(|| { |
| 200 | #[cfg(feature = "tui")] |
| 201 | return Commands::Tui; |
| 202 | |
| 203 | #[cfg(not(feature = "tui"))] |
| 204 | { |
| 205 | println!("Please specify a command. Use --help for usage information."); |
| 206 | std::process::exit(1); |
| 207 | } |
| 208 | }); |
| 209 | |
| 210 | #[cfg(not(target_os = "windows"))] |
| 211 | if let Commands::Daemon { .. } = command { |
| 212 | } else { |
| 213 | setup_logging(args.verbose); |
| 214 | } |
| 215 | #[cfg(target_os = "windows")] |
| 216 | setup_logging(args.verbose); |
| 217 | |
| 218 | let mut cli = Cli { args, learner: None }; |
| 219 | // Initialize learner unless it's an init command |
| 220 | if !matches!(command, Commands::Init(_)) { |
| 221 | cli.learner = Some(Learner::from_path(Config::default_path()?).await?); |
| 222 | } |
| 223 | |
| 224 | match command { |
| 225 | Commands::Init(init_options) => init(&mut cli, init_options).await, |
| 226 | Commands::Add(add_options) => { |
| 227 | add(&mut cli, add_options).await?; |
| 228 | Ok(()) |
| 229 | }, |
| 230 | Commands::Remove(remove_options) => remove(&mut cli, remove_options).await, |
| 231 | Commands::Search(search_options) => search(&mut cli, search_options).await, |
| 232 | #[cfg(not(target_os = "windows"))] |
| 233 | Commands::Daemon { cmd } => daemon(cmd).await, |
| 234 | #[cfg(feature = "tui")] |
| 235 | Commands::Tui => |
| 236 | if let Some(learner) = cli.learner.take() { |
| 237 | tui::run(learner).await |
| 238 | } else { |
| 239 | Err(LearnerdError::from(LearnerError::Config("Failed to initialize learner".to_string()))) |
| 240 | }, |
| 241 | } |
| 242 | } |