|
| 1 | +use clap::{Parser as ClapParser, Subcommand}; |
| 2 | +use reader::parser::Parser; |
| 3 | +use reader::parser::DefinitionError; |
| 4 | +use reader::parser::Feature; |
| 5 | +use tucana::shared::{RuntimeFunctionDefinition, DefinitionDataType, FlowType}; |
| 6 | +use notify::{Watcher, RecursiveMode, Event, EventKind, recommended_watcher}; |
| 7 | +use std::sync::mpsc::channel; |
| 8 | +use std::time::Duration; |
| 9 | +use colored::*; |
| 10 | +use crate::table::*; |
| 11 | + |
| 12 | +mod table; |
| 13 | + |
| 14 | +/// Top-level CLI for 'definition' |
| 15 | +#[derive(ClapParser)] |
| 16 | +#[command(name = "definition")] |
| 17 | +#[command(version = "1.0")] |
| 18 | +#[command(about = "Manage definitions, reports, and features")] |
| 19 | +struct Cli { |
| 20 | + #[command(subcommand)] |
| 21 | + command: Commands, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Subcommand)] |
| 25 | +enum Commands { |
| 26 | + /// Generate a general report. |
| 27 | + Report { |
| 28 | + /// Optional path to root directory of all definitions. |
| 29 | + #[arg(short, long)] |
| 30 | + path: Option<String>, |
| 31 | + }, |
| 32 | + /// Generate a report for a or all feature(s). |
| 33 | + Feature { |
| 34 | + /// Optional name of the definition set. |
| 35 | + #[arg(short, long)] |
| 36 | + featurename: Option<String>, |
| 37 | + /// Optional path to root directory of all definitions. |
| 38 | + #[arg(short, long)] |
| 39 | + path: Option<String>, |
| 40 | + }, |
| 41 | + /// Look up a specific definition. |
| 42 | + Definition { |
| 43 | + /// Required name of the definition. |
| 44 | + #[arg(short, long)] |
| 45 | + name: String, |
| 46 | + /// Optional path to root directory of all definitions. |
| 47 | + #[arg(short, long)] |
| 48 | + path: Option<String>, |
| 49 | + }, |
| 50 | + /// Watch for changes to and regenerate error reports. |
| 51 | + Watch { |
| 52 | + /// Optional path to root directory of all definitions. |
| 53 | + #[arg(short, long)] |
| 54 | + path: Option<String>, |
| 55 | + }, |
| 56 | +} |
| 57 | + |
1 | 58 | fn main() { |
2 | | - println!("Hello, world!"); |
| 59 | + let cli = Cli::parse(); |
| 60 | + |
| 61 | + match cli.command { |
| 62 | + Commands::Report { path } => { |
| 63 | + let dir_path = match path { |
| 64 | + Some(p) => p, |
| 65 | + None => "../definitions".to_string(), |
| 66 | + }; |
| 67 | + |
| 68 | + let parser = match Parser::from_path(dir_path.as_str()) { |
| 69 | + Some(reader) => reader, |
| 70 | + None => { |
| 71 | + panic!("Error reading definitions"); |
| 72 | + } |
| 73 | + }; |
| 74 | + error_table(&parser.features); |
| 75 | + summary_table(&parser.features); |
| 76 | + } |
| 77 | + Commands::Feature { featurename, path } => { |
| 78 | + let dir_path = match path { |
| 79 | + Some(p) => p, |
| 80 | + None => "../definitions".to_string(), |
| 81 | + }; |
| 82 | + |
| 83 | + let parser = match Parser::from_path(dir_path.as_str()) { |
| 84 | + Some(reader) => reader, |
| 85 | + None => { |
| 86 | + panic!("Error reading definitions"); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + if let Some(featurename) = featurename { |
| 91 | + let mut features_to_report = Vec::new(); |
| 92 | + for feature in &parser.features { |
| 93 | + if feature.name == featurename { |
| 94 | + feature_table(&feature); |
| 95 | + features_to_report.push(feature.clone()); |
| 96 | + } |
| 97 | + } |
| 98 | + summary_table(&features_to_report); |
| 99 | + } else { |
| 100 | + for feature in &parser.features { |
| 101 | + feature_table(&feature); |
| 102 | + } |
| 103 | + summary_table(&parser.features); |
| 104 | + } |
| 105 | + } |
| 106 | + Commands::Definition { name, path } => { |
| 107 | + println!("Handling definition with name: {}", name); |
| 108 | + let dir_path = match path { |
| 109 | + Some(p) => p, |
| 110 | + None => "../definitions".to_string(), |
| 111 | + }; |
| 112 | + |
| 113 | + todo!("Implement definition query and display command!"); |
| 114 | + } |
| 115 | + Commands::Watch { path } => { |
| 116 | + let dir_path = match path { |
| 117 | + Some(p) => p, |
| 118 | + None => "../definitions".to_string(), |
| 119 | + }; |
| 120 | + |
| 121 | + println!("{}", format!("Watching directory: {}", dir_path).bright_yellow().bold()); |
| 122 | + println!("{}", "Press Ctrl+C to stop watching...".dimmed()); |
| 123 | + |
| 124 | + { |
| 125 | + let parser = match Parser::from_path(dir_path.as_str()) { |
| 126 | + Some(reader) => reader, |
| 127 | + None => { |
| 128 | + panic!("Error reading definitions"); |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + error_table(&parser.features); |
| 133 | + } |
| 134 | + |
| 135 | + // Set up file watcher |
| 136 | + let (tx, rx) = channel(); |
| 137 | + let mut watcher = recommended_watcher(tx).unwrap(); |
| 138 | + watcher.watch(std::path::Path::new(&dir_path), RecursiveMode::Recursive).unwrap(); |
| 139 | + |
| 140 | + loop { |
| 141 | + match rx.recv() { |
| 142 | + Ok(event) => { |
| 143 | + match event { |
| 144 | + Ok(Event { kind: EventKind::Create(_), .. }) | |
| 145 | + Ok(Event { kind: EventKind::Modify(_), .. }) | |
| 146 | + Ok(Event { kind: EventKind::Remove(_), .. }) => { |
| 147 | + println!("\n{}", "Change detected! Regenerating report...".bright_yellow()); |
| 148 | + |
| 149 | + let parser = match Parser::from_path(dir_path.as_str()) { |
| 150 | + Some(reader) => reader, |
| 151 | + None => { |
| 152 | + panic!("Error reading definitions"); |
| 153 | + } |
| 154 | + }; |
| 155 | + |
| 156 | + error_table(&parser.features); |
| 157 | + } |
| 158 | + _ => {} |
| 159 | + } |
| 160 | + } |
| 161 | + Err(e) => println!("Watch error: {:?}", e), |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
3 | 166 | } |
0 commit comments