Skip to content

Commit 16f09f6

Browse files
authored
Merge pull request #48 from gaulliath/dev
Merging : config.toml / new modules / keystores
2 parents e11ca60 + 5bc5a40 commit 16f09f6

38 files changed

+1037
-130
lines changed

config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[keystore]
2+
dehashed = "" # email:apikey

opf-cli/src/main.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ async fn main() -> Result<()> {
6666
let (node_tx, node) = Node::new(tx).await;
6767
let workspace = Arc::new(RwLock::new(String::from("")));
6868

69+
let configuration =
70+
opf_models::Config::from_file("config.toml").expect("can't parse configuration file");
71+
72+
send_event_to(
73+
&node_tx,
74+
(Domain::Data, Event::LoadKeystore(configuration.keystore)),
75+
)
76+
.await
77+
.expect("can't send configuration file to data controller.");
78+
6979
let config = Config::builder()
7080
.history_ignore_space(true)
7181
.completion_type(CompletionType::List)
@@ -93,10 +103,10 @@ async fn main() -> Result<()> {
93103
for event in rx.iter() {
94104
match event {
95105
Event::ResponseSimple(res) => {
96-
let _ = external_printer.print(format!("INF {}", res));
106+
let _ = external_printer.print(format!("{} {}", "INF".blue(), res));
97107
}
98108
Event::ResponseError(res) => {
99-
let _ = external_printer.print(format!("ERR {}", res));
109+
let _ = external_printer.print(format!("{} {}", "ERR".red(), res));
100110
}
101111
Event::ResponseTable((headers, rows)) => {
102112
let mut table = Table::new();
@@ -107,7 +117,6 @@ async fn main() -> Result<()> {
107117
Event::SetWorkspace(new_workspace) => {
108118
let mut workspace = workspace_clone.write().unwrap();
109119
*workspace = new_workspace.clone();
110-
let _ = external_printer.print(format!("workspace => {}", new_workspace));
111120
}
112121
_ => {}
113122
}

opf-db/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ serde = { version = "1.0.145", features = ["derive"] }
1515
uuid = "1.3.1"
1616
chrono = "0.4.24"
1717
rand = "0.8.5"
18-
petgraph = "0.6.3"
18+
petgraph = "0.6.3"
19+
itertools = "0.10.5"

opf-db/src/lib.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
use std::collections::HashMap;
2+
use std::sync::Arc;
23
use std::time::SystemTime;
34

45
use log::info;
56
use rand::{distributions::Alphanumeric, Rng};
67
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
8+
use tokio::sync::RwLock;
79

810
use opf_models::event::Event::{ResponseError, ResponseSimple};
911
use opf_models::event::{send_event_to, Domain, Event};
10-
use opf_models::Workspace;
12+
use opf_models::{KeyStore, Workspace};
1113

1214
use crate::store::DB;
1315

1416
mod store;
1517
mod store_export;
18+
mod store_group;
19+
mod store_keystore;
1620
mod store_link;
1721
mod store_module;
1822
mod store_target;
@@ -21,6 +25,7 @@ mod store_workspace;
2125
#[derive(Debug)]
2226
pub struct DBStore {
2327
dbs: HashMap<i32, DB>,
28+
keystore: Arc<RwLock<KeyStore>>,
2429
workspaces: HashMap<i32, Workspace>,
2530
pub current_workspace: i32,
2631
pub self_tx: UnboundedSender<Event>,
@@ -64,6 +69,7 @@ pub async fn new(node_tx: UnboundedSender<(Domain, Event)>) -> (UnboundedSender<
6469
(
6570
tx,
6671
DBStore {
72+
keystore: Arc::new(RwLock::new(KeyStore(HashMap::new()))),
6773
dbs,
6874
workspaces,
6975
current_workspace: 0,
@@ -99,13 +105,24 @@ impl DBStore {
99105
let _ = send_event_to(&self.node_tx, (Domain::CLI, ResponseError(e.to_string()))).await;
100106
}
101107
},
108+
Event::CommandGroup(command) => {
109+
if let Err(e) = db.on_group_command(command).await {
110+
let _ = send_event_to(&self.node_tx, (Domain::CLI, ResponseError(e.to_string()))).await;
111+
}
112+
},
102113
Event::PrepareModule((module_name, command)) => {
103-
if let Err(e) = db.on_prepare_module(module_name, command).await {
114+
let keystore = self.keystore.read().await;
115+
if let Err(e) = db.on_prepare_module(&keystore, module_name, command).await {
116+
let _ = send_event_to(&self.node_tx, (Domain::CLI, ResponseError(e.to_string()))).await;
117+
}
118+
}
119+
Event::UpdateTargetMeta((target_id, meta)) => {
120+
if let Err(e) = db.on_target_meta_update(target_id, meta).await {
104121
let _ = send_event_to(&self.node_tx, (Domain::CLI, ResponseError(e.to_string()))).await;
105122
}
106123
}
107-
Event::ResultsModule(targets) => {
108-
if let Err(e) = db.on_results_targets(targets).await {
124+
Event::ResultsModule(group_id, targets) => {
125+
if let Err(e) = db.on_results_targets(group_id, targets).await {
109126
let _ = send_event_to(&self.node_tx, (Domain::CLI, ResponseError(e.to_string()))).await;
110127
}
111128
}
@@ -122,6 +139,12 @@ impl DBStore {
122139
let _ = send_event_to(&self.node_tx, (Domain::CLI, ResponseError(e.to_string()))).await;
123140
}
124141
}
142+
Event::CommandKeystore(command) => {
143+
if let Err(e) = self.on_keystore_command(command).await {
144+
let _ = send_event_to(&self.node_tx, (Domain::CLI, ResponseError(e.to_string()))).await;
145+
}
146+
}
147+
Event::LoadKeystore(keystore) => self.load_keystore(keystore).await,
125148
_ => {
126149
if let Err(e) = send_event_to(&self.node_tx, (Domain::CLI, event)).await {
127150
let _ = send_event_to(&self.node_tx, (Domain::CLI, ResponseError(e.to_string()))).await;
@@ -132,4 +155,8 @@ impl DBStore {
132155
}
133156
}
134157
}
158+
159+
pub async fn load_keystore(&mut self, keystore: KeyStore) {
160+
self.keystore = Arc::new(RwLock::new(keystore));
161+
}
135162
}

opf-db/src/store.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use tokio::sync::mpsc::UnboundedSender;
44
use tokio::sync::RwLock;
55

66
use opf_models::event::Event;
7-
use opf_models::{Link, Target};
7+
use opf_models::{Group, Link, Target};
88

99
#[derive(Debug)]
1010
pub struct DB {
1111
pub db_tx: UnboundedSender<Event>,
1212
pub db_id: i32,
1313
pub targets: RwLock<HashMap<i32, Target>>,
14+
pub groups: RwLock<HashMap<i32, Group>>,
1415
pub links: RwLock<HashMap<i32, Link>>,
1516
}
1617

@@ -20,6 +21,7 @@ impl DB {
2021
db_tx,
2122
db_id: 0,
2223
targets: RwLock::new(HashMap::new()),
24+
groups: RwLock::new(HashMap::new()),
2325
links: RwLock::new(HashMap::new()),
2426
}
2527
}

opf-db/src/store_export.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ impl DB {
1919
let mut index = HashMap::new();
2020
let targets = self.targets.read().await;
2121
for (target_id, target) in targets.iter() {
22-
if target.target_parent.is_some() {
23-
continue;
24-
}
2522
let id = graph.add_node(target.target_name.clone());
2623
index.insert(target_id, id.clone());
2724
}
@@ -30,16 +27,15 @@ impl DB {
3027
if target.target_parent.is_none() {
3128
continue;
3229
}
33-
let id = graph.add_node(target.target_name.clone());
34-
index.insert(target_id, id.clone());
35-
3630
if let Some(parent) = target.target_parent {
3731
if let Some(parent_id) = index.get(&parent) {
38-
graph.add_edge(
39-
parent_id.clone(),
40-
id,
41-
target.target_type.to_string().clone(),
42-
);
32+
if let Some(id) = index.get(&target_id) {
33+
graph.add_edge(
34+
parent_id.clone(),
35+
*id,
36+
target.target_type.to_string().clone(),
37+
);
38+
}
4339
}
4440
}
4541
}

opf-db/src/store_group.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use opf_models::event::{send_event, Event};
2+
use opf_models::{self, error::ErrorKind, group, Command, CommandAction};
3+
4+
use crate::store::DB;
5+
6+
impl DB {
7+
pub async fn on_group_command(&mut self, command: Command) -> Result<(), ErrorKind> {
8+
match command.action {
9+
CommandAction::List => self.list_groups(command).await,
10+
_ => Err(ErrorKind::ActionNotAvailable),
11+
}
12+
}
13+
14+
async fn list_groups(&self, _cmd: Command) -> Result<(), ErrorKind> {
15+
let groups = &self.groups.read().await;
16+
17+
let mut headers = vec![
18+
group::ID.to_string(),
19+
group::NAME.to_string(),
20+
group::TARGETS.to_string(),
21+
];
22+
let mut header_meta = vec![];
23+
let mut rows = vec![];
24+
25+
for (_, group) in groups.iter() {
26+
let fields = vec![
27+
group.group_id.to_string(),
28+
group.group_name.to_string(),
29+
group.targets.len().to_string(),
30+
];
31+
rows.push(fields);
32+
}
33+
34+
headers.append(&mut header_meta);
35+
send_event(&self.db_tx, Event::ResponseTable((headers, rows))).await
36+
}
37+
}

opf-db/src/store_keystore.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::DBStore;
2+
use opf_models::event::{send_event, Event};
3+
use opf_models::{self, error::ErrorKind, keystore, Command, CommandAction};
4+
5+
impl DBStore {
6+
pub async fn on_keystore_command(&mut self, command: Command) -> Result<(), ErrorKind> {
7+
match command.action {
8+
CommandAction::List => self.list_keystore(command).await,
9+
_ => Err(ErrorKind::ActionNotAvailable),
10+
}
11+
}
12+
13+
async fn list_keystore(&self, _cmd: Command) -> Result<(), ErrorKind> {
14+
let keystore = &self.keystore.read().await;
15+
16+
let mut headers = vec![keystore::NAME.to_string(), keystore::VALUE.to_string()];
17+
18+
let mut header_meta = vec![];
19+
let mut rows = vec![];
20+
21+
for (name, value) in &keystore.0 {
22+
let fields = vec![name.clone(), value.clone()];
23+
rows.push(fields);
24+
}
25+
26+
headers.append(&mut header_meta);
27+
send_event(&self.self_tx, Event::ResponseTable((headers, rows))).await
28+
}
29+
}

0 commit comments

Comments
 (0)