-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
137 lines (124 loc) · 5.77 KB
/
Copy pathmod.rs
File metadata and controls
137 lines (124 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::analyser::core::Analyser;
use crate::command::push::data_type_client_impl::SagittariusDataTypeServiceClient;
use crate::command::push::flow_type_client_impl::SagittariusFlowTypeServiceClient;
use crate::command::push::function_client_impl::SagittariusRuntimeFunctionServiceClient;
use crate::formatter::{default, info};
use notify::event::ModifyKind;
use notify::{EventKind, RecursiveMode, Watcher, recommended_watcher};
use std::sync::mpsc::channel;
use std::time::{Duration, Instant};
mod auth;
mod data_type_client_impl;
mod flow_type_client_impl;
mod function_client_impl;
pub async fn push(token: String, url: String, path: Option<String>) {
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
info(format!("Watching directory: {dir_path}"));
info(String::from("Press Ctrl+C to stop watching..."));
{
Analyser::new(dir_path.as_str()).report(false);
}
// Set up file watcher
let (tx, rx) = channel();
let mut watcher = recommended_watcher(tx).unwrap();
watcher
.watch(std::path::Path::new(&dir_path), RecursiveMode::Recursive)
.unwrap();
let mut last_run = Instant::now();
let mut data_type_client =
SagittariusDataTypeServiceClient::new(url.clone(), token.clone()).await;
let mut flow_type_client =
SagittariusFlowTypeServiceClient::new(url.clone(), token.clone()).await;
let mut function_client = SagittariusRuntimeFunctionServiceClient::new(url, token).await;
loop {
if let Ok(Ok(event)) = rx.recv() {
match event.kind {
EventKind::Modify(modify) => {
if let ModifyKind::Data(_) = modify
&& last_run.elapsed() > Duration::from_millis(500)
{
default(String::from(
"\n\n\n--------------------------------------------------------------------------\n\n",
));
info(String::from("Change detected! Regenerating report..."));
let mut analyzer = Analyser::new(dir_path.as_str());
// No errors when reporter is empty!
if analyzer.reporter.is_empty() {
data_type_client
.update_data_types(
analyzer
.data_types
.iter()
.map(|d| d.definition_data_type.clone())
.collect(),
)
.await;
flow_type_client
.update_flow_types(
analyzer
.flow_types
.iter()
.map(|d| d.flow_type.clone())
.collect(),
)
.await;
function_client
.update_runtime_function_definitions(
analyzer
.functions
.iter()
.map(|d| d.function.clone())
.collect(),
)
.await;
}
analyzer.report(false);
last_run = Instant::now();
}
}
EventKind::Remove(_) => {
if last_run.elapsed() > Duration::from_millis(500) {
default(String::from(
"\n\n\n--------------------------------------------------------------------------\n\n",
));
info(String::from("Change detected! Regenerating report..."));
let mut analyzer = Analyser::new(dir_path.as_str());
// No errors when reporter is empty!
if analyzer.reporter.is_empty() {
data_type_client
.update_data_types(
analyzer
.data_types
.iter()
.map(|d| d.definition_data_type.clone())
.collect(),
)
.await;
flow_type_client
.update_flow_types(
analyzer
.flow_types
.iter()
.map(|d| d.flow_type.clone())
.collect(),
)
.await;
function_client
.update_runtime_function_definitions(
analyzer
.functions
.iter()
.map(|d| d.function.clone())
.collect(),
)
.await;
}
analyzer.report(false);
last_run = Instant::now();
}
}
_ => {}
}
}
}
}