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

Function get_contents

dsc/src/resolve.rs:58–149  ·  view source on GitHub ↗

Read the file specified in the Include input and return the content as a JSON string. # Arguments `input` - The Include input as a JSON string. # Returns A tuple containing the contents of the parameters file as JSON and the configuration content as a JSON string. # Errors This function will return an error if the Include input is not valid JSON, if the file specified in the Include input ca

(input: &str)

Source from the content-addressed store, hash-verified

56/// specified in the Include input cannot be read, or if the content of the file cannot be
57/// deserialized as YAML or JSON.
58pub fn get_contents(input: &str) -> Result<(Option<String>, String), String> {
59 debug!("{}", t!("resolve.processingInclude"));
60
61 // deserialize the Include input
62 let include = match serde_json::from_str::<Include>(input) {
63 Ok(include) => include,
64 Err(err) => {
65 return Err(format!("{}: {err}", t!("resolve.invalidInclude")));
66 }
67 };
68
69 let config_json = match include.configuration {
70 IncludeKind::ConfigurationFile(file_path) => {
71 let include_path = normalize_path(Path::new(&file_path))?;
72
73 // read the file specified in the Include input
74 let mut buffer: Vec<u8> = Vec::new();
75 match File::open(&include_path) {
76 Ok(mut file) => {
77 match file.read_to_end(&mut buffer) {
78 Ok(_) => (),
79 Err(err) => {
80 return Err(t!("resolve.failedToReadFile", path = include_path.to_string_lossy(), error = err.to_string()).to_string());
81 }
82 }
83 },
84 Err(err) => {
85 return Err(t!("resolve.failedToOpenFile", path = include_path.to_string_lossy(), error = err.to_string()).to_string());
86 }
87 }
88 // convert the buffer to a string
89 let include_content = match String::from_utf8(buffer) {
90 Ok(input) => input,
91 Err(err) => {
92 return Err(t!("resolve.invalidFileContent", path = include_path.to_string_lossy(), error = err.to_string()).to_string());
93 }
94 };
95
96 match parse_input_to_json(&include_content) {
97 Ok(json) => json,
98 Err(err) => {
99 return Err(t!("resolve.invalidFile", path = include_path.to_string_lossy(), error = err.to_string()).to_string());
100 }
101 }
102 },
103 IncludeKind::ConfigurationContent(text) => {
104 match parse_input_to_json(&text) {
105 Ok(json) => json,
106 Err(err) => {
107 return Err(t!("resolve.invalidContent", error = err.to_string()).to_string());
108 }
109 }
110 }
111 };
112
113 let parameters = match include.parameters {
114 Some(IncludeParametersKind::ParametersFile(file_path)) => {
115 // combine the path with DSC_CONFIG_ROOT

Callers 1

configFunction · 0.85

Calls 3

normalize_pathFunction · 0.85
parse_input_to_jsonFunction · 0.85
newFunction · 0.50

Tested by

no test coverage detected