Skip to content

Commit a8fe8a1

Browse files
committed
ref: moved commands into speperate file
1 parent 2e34025 commit a8fe8a1

File tree

6 files changed

+241
-215
lines changed

6 files changed

+241
-215
lines changed

cli/src/command/definition.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
use code0_definition_reader::parser::Parser;
2+
use colored::Colorize;
3+
4+
pub fn search_definition(name: String, path: Option<String>) {
5+
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
6+
7+
let parser = match Parser::from_path(dir_path.as_str()) {
8+
Some(reader) => reader,
9+
None => {
10+
panic!("Error reading definitions");
11+
}
12+
};
13+
14+
search_and_display_definitions(&name, &parser);
15+
}
16+
17+
fn search_and_display_definitions(search_name: &str, parser: &Parser) {
18+
let mut found_any = false;
19+
let mut total_matches = 0;
20+
println!(
21+
"{}",
22+
format!("Searching for definitions matching: '{search_name}'")
23+
.bright_yellow()
24+
.bold()
25+
);
26+
println!("{}", "─".repeat(60).dimmed());
27+
28+
for feature in &parser.features {
29+
// Search FlowTypes
30+
for flow_type in &feature.flow_types {
31+
if flow_type.identifier == search_name {
32+
total_matches += 1;
33+
if !found_any {
34+
found_any = true;
35+
}
36+
37+
println!("\n{}", "FlowType".bright_cyan().bold());
38+
match serde_json::to_string_pretty(flow_type) {
39+
Ok(json) => {
40+
for line in json.lines() {
41+
println!("{}", line.bright_green());
42+
}
43+
}
44+
Err(_) => println!("{}", "Error serializing FlowType".red()),
45+
}
46+
}
47+
}
48+
49+
// Search DataTypes
50+
for data_type in &feature.data_types {
51+
if data_type.identifier == search_name {
52+
total_matches += 1;
53+
if !found_any {
54+
found_any = true;
55+
}
56+
57+
println!("\n{}", "DataType".bright_cyan().bold());
58+
match serde_json::to_string_pretty(data_type) {
59+
Ok(json) => {
60+
for line in json.lines() {
61+
println!("{}", line.bright_green());
62+
}
63+
}
64+
Err(_) => println!("{}", "Error serializing DataType".red()),
65+
}
66+
}
67+
}
68+
69+
// Search RuntimeFunctions
70+
for runtime_func in &feature.runtime_functions {
71+
if runtime_func.runtime_name == search_name {
72+
total_matches += 1;
73+
if !found_any {
74+
found_any = true;
75+
}
76+
77+
println!("\n{}", "RuntimeFunction".bright_cyan().bold());
78+
match serde_json::to_string_pretty(runtime_func) {
79+
Ok(json) => {
80+
let mut index = 0;
81+
for line in json.lines() {
82+
index += 1;
83+
println!(
84+
"{} {}",
85+
format!("{index}:").bright_blue(),
86+
line.bright_green()
87+
);
88+
}
89+
}
90+
Err(_) => println!("{}", "Error serializing RuntimeFunction".red()),
91+
}
92+
}
93+
}
94+
}
95+
96+
if !found_any {
97+
println!(
98+
"\n{}",
99+
format!("No definitions found matching '{search_name}'")
100+
.red()
101+
.bold()
102+
);
103+
} else {
104+
println!("\n{}", "─".repeat(60).dimmed());
105+
println!(
106+
"{}",
107+
format!("Found {total_matches} matching definition(s)").bright_yellow()
108+
);
109+
}
110+
}

cli/src/command/feature.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::table::{feature_table, summary_table};
2+
use code0_definition_reader::parser::Parser;
3+
4+
pub fn search_feature(name: Option<String>, path: Option<String>) {
5+
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
6+
7+
let parser = match Parser::from_path(dir_path.as_str()) {
8+
Some(reader) => reader,
9+
None => {
10+
panic!("Error reading definitions");
11+
}
12+
};
13+
14+
if let Some(feature_name) = name {
15+
let mut features_to_report = Vec::new();
16+
for feature in &parser.features {
17+
if feature.name == feature_name {
18+
feature_table(feature);
19+
features_to_report.push(feature.clone());
20+
}
21+
}
22+
summary_table(&features_to_report);
23+
} else {
24+
for feature in &parser.features {
25+
feature_table(feature);
26+
}
27+
summary_table(&parser.features);
28+
}
29+
}

cli/src/command/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod definition;
2+
pub mod download;
3+
pub mod feature;
4+
pub mod report;
5+
pub mod watch;

cli/src/command/report.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use crate::table::{error_table, summary_table};
2+
use code0_definition_reader::parser::Parser;
3+
4+
pub fn report_errors(path: Option<String>) {
5+
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
6+
7+
let parser = match Parser::from_path(dir_path.as_str()) {
8+
Some(reader) => reader,
9+
None => {
10+
panic!("Error reading definitions");
11+
}
12+
};
13+
error_table(&parser.features);
14+
summary_table(&parser.features);
15+
}

cli/src/command/watch.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use crate::table::error_table;
2+
use code0_definition_reader::parser::Parser;
3+
use colored::Colorize;
4+
use notify::{Event, EventKind, RecursiveMode, Watcher, recommended_watcher};
5+
use std::sync::mpsc::channel;
6+
7+
pub async fn watch_for_changes(path: Option<String>) {
8+
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
9+
10+
println!(
11+
"{}",
12+
format!("Watching directory: {dir_path}")
13+
.bright_yellow()
14+
.bold()
15+
);
16+
println!("{}", "Press Ctrl+C to stop watching...".dimmed());
17+
18+
{
19+
let parser = match Parser::from_path(dir_path.as_str()) {
20+
Some(reader) => reader,
21+
None => {
22+
panic!("Error reading definitions");
23+
}
24+
};
25+
26+
error_table(&parser.features);
27+
}
28+
29+
// Set up file watcher
30+
let (tx, rx) = channel();
31+
let mut watcher = recommended_watcher(tx).unwrap();
32+
watcher
33+
.watch(std::path::Path::new(&dir_path), RecursiveMode::Recursive)
34+
.unwrap();
35+
36+
loop {
37+
match rx.recv() {
38+
Ok(event) => match event {
39+
Ok(Event {
40+
kind: EventKind::Create(_),
41+
..
42+
})
43+
| Ok(Event {
44+
kind: EventKind::Modify(_),
45+
..
46+
})
47+
| Ok(Event {
48+
kind: EventKind::Remove(_),
49+
..
50+
}) => {
51+
println!(
52+
"\n{}",
53+
"Change detected! Regenerating report...".bright_yellow()
54+
);
55+
56+
let parser = match Parser::from_path(dir_path.as_str()) {
57+
Some(reader) => reader,
58+
None => {
59+
panic!("Error reading definitions");
60+
}
61+
};
62+
63+
error_table(&parser.features);
64+
}
65+
_ => {}
66+
},
67+
Err(e) => println!("Watch error: {e:?}"),
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)