MCPcopy Create free account
hub / github.com/PowerShell/DSC / copy_the_resource

Function copy_the_resource

tools/dsctest/src/copy_resource.rs:14–33  ·  view source on GitHub ↗
(source_file: &str, type_name: &str)

Source from the content-addressed store, hash-verified

12}
13
14pub fn copy_the_resource(source_file: &str, type_name: &str) -> Result<(), String> {
15 // open the source_file, deserialize it from JSON, change the `type` property to type_name,
16 // serialize it back to JSON and save it to a new file named "<name>.dsc.resource.json"
17 let file_content = std::fs::read_to_string(source_file)
18 .map_err(|e| format!("Failed to read source file: {}", e))?;
19 let mut resource_json: serde_json::Value = serde_json::from_str(&file_content)
20 .map_err(|e| format!("Failed to parse JSON from source file: {}", e))?;
21 if let Some(obj) = resource_json.as_object_mut() {
22 obj.insert("type".to_string(), serde_json::Value::String(type_name.to_string()));
23 } else {
24 return Err("Source file not a resource manifest".to_string());
25 }
26 let name_part = type_name.split('/').next_back().unwrap_or(type_name);
27 let output_file = format!("{name_part}.dsc.resource.json");
28 let output_content = serde_json::to_string_pretty(&resource_json)
29 .map_err(|e| format!("Failed to serialize JSON: {e}"))?;
30 std::fs::write(&output_file, output_content)
31 .map_err(|e| format!("Failed to write output file: {e}"))?;
32 Ok(())
33}

Callers 1

mainFunction · 0.85

Calls 1

from_strFunction · 0.50

Tested by

no test coverage detected