Skip to content

Commit 057452e

Browse files
author
Andrew
committed
Stage 2
1 parent 3493ca5 commit 057452e

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ impl ResourceDiscovery for CommandDiscovery {
280280
}
281281

282282
self.adapted_resources = adapted_resources;
283-
save_adapted_resources_lookup_table(&self.adapted_resources);
284283

285284
Ok(())
286285
}
@@ -297,6 +296,11 @@ impl ResourceDiscovery for CommandDiscovery {
297296
} else {
298297
self.discover_resources("*")?;
299298
self.discover_adapted_resources(type_name_filter, adapter_name_filter)?;
299+
300+
// add found adapted resources to the lookup_table
301+
add_resources_to_lookup_table(&self.adapted_resources);
302+
303+
// note: in next line 'BTreeMap::append' will leave self.adapted_resources empty
300304
resources.append(&mut self.adapted_resources);
301305
}
302306

@@ -356,6 +360,8 @@ impl ResourceDiscovery for CommandDiscovery {
356360
}
357361

358362
self.discover_adapted_resources("*", &adapter_name)?;
363+
// add found adapted resources to the lookup_table
364+
add_resources_to_lookup_table(&self.adapted_resources);
359365

360366
// now go through the adapter resources and add them to the list of resources
361367
for (adapted_name, adapted_resource) in &self.adapted_resources {
@@ -500,28 +506,47 @@ fn load_manifest(path: &Path) -> Result<DscResource, DscError> {
500506
Ok(resource)
501507
}
502508

503-
fn save_adapted_resources_lookup_table(adapted_resources: &BTreeMap<String, Vec<DscResource>>)
509+
fn add_resources_to_lookup_table(adapted_resources: &BTreeMap<String, Vec<DscResource>>)
504510
{
505-
let mut lookup_table: HashMap<String, String> = HashMap::new();
511+
let mut lookup_table:HashMap<String, String> = load_adapted_resources_lookup_table();
512+
506513
for (resource_name, res_vec) in adapted_resources {
507514
let adapter_name = &res_vec[0].require_adapter.as_ref().unwrap();
508-
lookup_table.insert(resource_name.to_string(), adapter_name.to_string());
509-
}
515+
lookup_table.insert(resource_name.to_string().to_lowercase(), adapter_name.to_string());
516+
};
510517

511-
debug!("============ABC==========");
518+
save_adapted_resources_lookup_table(&lookup_table);
519+
}
512520

521+
fn save_adapted_resources_lookup_table(lookup_table: &HashMap<String, String>)
522+
{
513523
match serde_json::to_string_pretty(&lookup_table) {
514524
Ok(lookup_table_json) => {
515-
debug!("{:?}", lookup_table_json);
516525

517526
let file_path = get_lookup_table_file_path();
518-
debug!("{:?}", file_path);
527+
debug!("Saving lookup table with {} items to {:?}", lookup_table.len(), file_path);
519528

520529
fs::write(file_path, lookup_table_json).expect("Unable to write lookup_table file");
521530
},
522531
Err(_) => {}
523532
}
533+
}
534+
535+
fn load_adapted_resources_lookup_table() -> HashMap<String, String>
536+
{
537+
let file_path = get_lookup_table_file_path();
524538

539+
let lookup_table: HashMap<String, String> = match fs::read(file_path.clone()){
540+
Ok(data) => { match serde_json::from_slice(&data) {
541+
Ok(lt) => { lt },
542+
Err(_) => { HashMap::new() }
543+
}
544+
},
545+
Err(_) => { HashMap::new() }
546+
};
547+
548+
debug!("Read {} items into lookup table from {:?}", lookup_table.len(), file_path);
549+
lookup_table
525550
}
526551

527552
#[cfg(target_os = "windows")]

0 commit comments

Comments
 (0)