@@ -5,7 +5,7 @@ use crate::discovery::discovery_trait::{ResourceDiscovery};
55use crate :: dscresources:: dscresource:: { DscResource , ImplementedAs } ;
66use crate :: dscresources:: resource_manifest:: ResourceManifest ;
77use crate :: dscresources:: command_resource:: invoke_command;
8- use crate :: dscerror:: DscError ;
8+ use crate :: dscerror:: { DscError , StreamMessage , StreamMessageType } ;
99use std:: collections:: BTreeMap ;
1010use std:: env;
1111use std:: fs:: File ;
@@ -15,6 +15,7 @@ use std::path::Path;
1515pub struct CommandDiscovery {
1616 pub resources : BTreeMap < String , DscResource > ,
1717 provider_resources : Vec < String > ,
18+ discovery_messages : Vec < StreamMessage > , // this will later be used to display only messages for resources used in specific operation
1819 initialized : bool ,
1920}
2021
@@ -23,6 +24,7 @@ impl CommandDiscovery {
2324 CommandDiscovery {
2425 resources : BTreeMap :: new ( ) ,
2526 provider_resources : Vec :: new ( ) ,
27+ discovery_messages : Vec :: new ( ) ,
2628 initialized : false ,
2729 }
2830 }
@@ -77,31 +79,51 @@ impl ResourceDiscovery for CommandDiscovery {
7779 // now go through the provider resources and add them to the list of resources
7880 for provider in & self . provider_resources {
7981 let provider_resource = self . resources . get ( provider) . unwrap ( ) ;
82+ let provider_type_name = provider_resource. type_name . clone ( ) ;
83+ let provider_path = provider_resource. path . clone ( ) ;
8084 let manifest = serde_json:: from_value :: < ResourceManifest > ( provider_resource. manifest . clone ( ) . unwrap ( ) ) ?;
8185 // invoke the list command
8286 let list_command = manifest. provider . unwrap ( ) . list ;
8387 let ( exit_code, stdout, stderr) = match invoke_command ( & list_command. executable , list_command. args , None , Some ( & provider_resource. directory ) )
8488 {
8589 Ok ( ( exit_code, stdout, stderr) ) => ( exit_code, stdout, stderr) ,
86- Err ( _e) => {
87- //TODO: add to debug stream: println!("Could not start {}: {}", list_command.executable, e);
90+ Err ( e) => {
91+ self . discovery_messages . push ( StreamMessage :: new_error (
92+ format ! ( "Could not start {}: {}" , list_command. executable, e) ,
93+ Some ( provider_type_name. clone ( ) ) ,
94+ Some ( provider_path. clone ( ) ) ) ) ;
95+
8896 continue ;
8997 } ,
9098 } ;
9199
92100 if exit_code != 0 {
93- return Err ( DscError :: Operation ( format ! ( "Failed to list resources for provider {provider}: {exit_code} {stderr}" ) ) ) ;
101+ self . discovery_messages . push ( StreamMessage :: new_error (
102+ format ! ( "Provider failed to list resources with exit code {exit_code}: {stderr}" ) ,
103+ Some ( provider_type_name. clone ( ) ) ,
104+ Some ( provider_path. clone ( ) ) ) ) ;
94105 }
106+
95107 for line in stdout. lines ( ) {
96108 match serde_json:: from_str :: < DscResource > ( line) {
97109 Result :: Ok ( resource) => {
98110 if resource. requires . is_none ( ) {
99- return Err ( DscError :: MissingRequires ( provider. clone ( ) , resource. type_name ) ) ;
111+ self . discovery_messages . push ( StreamMessage :: new_error (
112+ DscError :: MissingRequires ( provider. clone ( ) , resource. type_name . clone ( ) ) . to_string ( ) ,
113+ Some ( resource. type_name . clone ( ) ) ,
114+ Some ( resource. path . clone ( ) ) ) ) ;
115+
116+ continue ;
100117 }
101118 self . resources . insert ( resource. type_name . clone ( ) , resource) ;
102119 } ,
103120 Result :: Err ( err) => {
104- return Err ( DscError :: Operation ( format ! ( "Failed to parse resource from provider {provider}: {line} -> {err}" ) ) ) ;
121+ self . discovery_messages . push ( StreamMessage :: new_error (
122+ format ! ( "Failed to parse resource: {line} -> {err}" ) ,
123+ Some ( provider_type_name. clone ( ) ) ,
124+ Some ( provider_path. clone ( ) ) ) ) ;
125+
126+ continue ;
105127 }
106128 } ;
107129 }
@@ -110,6 +132,14 @@ impl ResourceDiscovery for CommandDiscovery {
110132 self . initialized = true ;
111133 Ok ( ( ) )
112134 }
135+
136+ fn print_initialization_messages ( & mut self , error_format : StreamMessageType , warning_format : StreamMessageType ) -> Result < ( ) , DscError > {
137+ for msg in & self . discovery_messages {
138+ msg. print ( & error_format, & warning_format) ?;
139+ }
140+
141+ Ok ( ( ) )
142+ }
113143}
114144
115145fn import_manifest ( path : & Path ) -> Result < DscResource , DscError > {
0 commit comments