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

Method import

lib/dsc-lib/src/extensions/import.rs:64–114  ·  view source on GitHub ↗

Import a file based on the extension. # Arguments `file` - The file to import. # Returns A result containing the imported file content or an error. # Errors This function will return an error if the import fails or if the extension does not support the import capability.

(&self, file: &Path)

Source from the content-addressed store, hash-verified

62 ///
63 /// This function will return an error if the import fails or if the extension does not support the import capability.
64 pub fn import(&self, file: &Path) -> Result<String, DscError> {
65 if let Some(import) = &self.import {
66 let file_extension = file.extension().and_then(|s| s.to_str()).unwrap_or_default().to_string();
67 if import.file_extensions.contains(&file_extension) {
68 debug!("{}", t!("extensions.dscextension.importingFile", file = file.display(), extension = self.type_name));
69 } else {
70 return Err(DscError::NotSupported(
71 t!("extensions.dscextension.importNotSupported", file = file.display(), extension = self.type_name).to_string(),
72 ));
73 }
74
75 let extension = match serde_json::from_value::<ExtensionManifest>(self.manifest.clone()) {
76 Ok(manifest) => manifest,
77 Err(err) => {
78 return Err(DscError::Manifest(self.type_name.to_string(), err));
79 }
80 };
81 let Some(import) = extension.import else {
82 return Err(DscError::UnsupportedCapability(self.type_name.to_string(), Capability::Import.to_string()));
83 };
84 let args = process_import_args(import.args.as_ref(), file)?;
85 if let Some(deprecation_message) = extension.deprecation_message.as_ref() {
86 warn!("{}", t!("extensions.dscextension.deprecationMessage", extension = self.type_name, message = deprecation_message));
87 }
88 let (_exit_code, stdout, _stderr) = invoke_command(
89 &import.executable,
90 args,
91 None,
92 Some(&self.directory),
93 None,
94 extension.exit_codes.as_ref(),
95 )?;
96 if stdout.is_empty() {
97 info!("{}", t!("extensions.dscextension.importNoResults", extension = self.type_name));
98 } else {
99 if let Some(output) = &import.output {
100 debug!("{}", t!("extensions.dscextension.importProcessingOutput", extension = self.type_name));
101 let mut parser = Statement::new()?;
102 let mut context = Context::new();
103 context.stdout = Some(stdout);
104 let processed_output = parser.parse_and_execute(output, &context)?;
105 return Ok(processed_output.to_string());
106 }
107 return Ok(stdout);
108 }
109 }
110 Err(DscError::UnsupportedCapability(
111 self.type_name.to_string(),
112 Capability::Import.to_string()
113 ))
114 }
115}
116
117fn process_import_args(args: Option<&Vec<ImportArgKind>>, file: &Path) -> Result<Option<Vec<String>>, DscError> {

Callers 1

get_inputFunction · 0.80

Calls 6

process_import_argsFunction · 0.85
parse_and_executeMethod · 0.80
invoke_commandFunction · 0.50
newFunction · 0.50
as_refMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected