@@ -12,8 +12,9 @@ use self::config_doc::{Configuration, DataType};
12
12
use self :: depends_on:: get_resource_invocation_order;
13
13
use self :: config_result:: { ConfigurationGetResult , ConfigurationSetResult , ConfigurationTestResult , ConfigurationExportResult } ;
14
14
use self :: contraints:: { check_length, check_number_limits, check_allowed_values} ;
15
+ use indicatif:: { ProgressBar , ProgressStyle } ;
15
16
use serde_json:: { Map , Value } ;
16
- use std:: collections:: { HashMap , HashSet } ;
17
+ use std:: collections:: HashMap ;
17
18
use tracing:: debug;
18
19
19
20
pub mod context;
@@ -130,6 +131,14 @@ fn escape_property_values(properties: &Map<String, Value>) -> Result<Option<Map<
130
131
Ok ( Some ( result) )
131
132
}
132
133
134
+ fn get_progress_bar ( len : u64 ) -> Result < ProgressBar , DscError > {
135
+ let pb = ProgressBar :: new ( len) ;
136
+ pb. set_style ( ProgressStyle :: with_template (
137
+ "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos:>7}/{len:7} {msg}"
138
+ ) ?) ;
139
+ Ok ( pb)
140
+ }
141
+
133
142
impl Configurator {
134
143
/// Create a new `Configurator` instance.
135
144
///
@@ -163,7 +172,11 @@ impl Configurator {
163
172
pub fn invoke_get ( & mut self , _error_action : ErrorAction , _progress_callback : impl Fn ( ) + ' static ) -> Result < ConfigurationGetResult , DscError > {
164
173
let config = self . validate_config ( ) ?;
165
174
let mut result = ConfigurationGetResult :: new ( ) ;
166
- for resource in get_resource_invocation_order ( & config, & mut self . statement_parser , & self . context ) ? {
175
+ let resources = get_resource_invocation_order ( & config, & mut self . statement_parser , & self . context ) ?;
176
+ let pb = get_progress_bar ( resources. len ( ) as u64 ) ?;
177
+ for resource in resources {
178
+ pb. inc ( 1 ) ;
179
+ pb. set_message ( format ! ( "Get {}" , resource. name) ) ;
167
180
let properties = self . invoke_property_expressions ( & resource. properties ) ?;
168
181
let Some ( dsc_resource) = self . discovery . find_resource ( & resource. resource_type . to_lowercase ( ) ) else {
169
182
return Err ( DscError :: ResourceNotFound ( resource. resource_type ) ) ;
@@ -179,6 +192,7 @@ impl Configurator {
179
192
result. results . push ( resource_result) ;
180
193
}
181
194
195
+ pb. finish_and_clear ( ) ;
182
196
Ok ( result)
183
197
}
184
198
@@ -195,7 +209,11 @@ impl Configurator {
195
209
pub fn invoke_set ( & mut self , skip_test : bool , _error_action : ErrorAction , _progress_callback : impl Fn ( ) + ' static ) -> Result < ConfigurationSetResult , DscError > {
196
210
let config = self . validate_config ( ) ?;
197
211
let mut result = ConfigurationSetResult :: new ( ) ;
198
- for resource in get_resource_invocation_order ( & config, & mut self . statement_parser , & self . context ) ? {
212
+ let resources = get_resource_invocation_order ( & config, & mut self . statement_parser , & self . context ) ?;
213
+ let pb = get_progress_bar ( resources. len ( ) as u64 ) ?;
214
+ for resource in resources {
215
+ pb. inc ( 1 ) ;
216
+ pb. set_message ( format ! ( "Set {}" , resource. name) ) ;
199
217
let properties = self . invoke_property_expressions ( & resource. properties ) ?;
200
218
let Some ( dsc_resource) = self . discovery . find_resource ( & resource. resource_type . to_lowercase ( ) ) else {
201
219
return Err ( DscError :: ResourceNotFound ( resource. resource_type ) ) ;
@@ -211,6 +229,7 @@ impl Configurator {
211
229
result. results . push ( resource_result) ;
212
230
}
213
231
232
+ pb. finish_and_clear ( ) ;
214
233
Ok ( result)
215
234
}
216
235
@@ -227,7 +246,11 @@ impl Configurator {
227
246
pub fn invoke_test ( & mut self , _error_action : ErrorAction , _progress_callback : impl Fn ( ) + ' static ) -> Result < ConfigurationTestResult , DscError > {
228
247
let config = self . validate_config ( ) ?;
229
248
let mut result = ConfigurationTestResult :: new ( ) ;
230
- for resource in get_resource_invocation_order ( & config, & mut self . statement_parser , & self . context ) ? {
249
+ let resources = get_resource_invocation_order ( & config, & mut self . statement_parser , & self . context ) ?;
250
+ let pb = get_progress_bar ( resources. len ( ) as u64 ) ?;
251
+ for resource in resources {
252
+ pb. inc ( 1 ) ;
253
+ pb. set_message ( format ! ( "Test {}" , resource. name) ) ;
231
254
let properties = self . invoke_property_expressions ( & resource. properties ) ?;
232
255
let Some ( dsc_resource) = self . discovery . find_resource ( & resource. resource_type . to_lowercase ( ) ) else {
233
256
return Err ( DscError :: ResourceNotFound ( resource. resource_type ) ) ;
@@ -243,6 +266,7 @@ impl Configurator {
243
266
result. results . push ( resource_result) ;
244
267
}
245
268
269
+ pb. finish_and_clear ( ) ;
246
270
Ok ( result)
247
271
}
248
272
@@ -263,17 +287,13 @@ impl Configurator {
263
287
pub fn invoke_export ( & mut self , _error_action : ErrorAction , _progress_callback : impl Fn ( ) + ' static ) -> Result < ConfigurationExportResult , DscError > {
264
288
let config = self . validate_config ( ) ?;
265
289
266
- let duplicates = Self :: find_duplicate_resource_types ( & config) ;
267
- if !duplicates. is_empty ( )
268
- {
269
- let duplicates_string = & duplicates. join ( "," ) ;
270
- return Err ( DscError :: Validation ( format ! ( "Resource(s) {duplicates_string} specified multiple times" ) ) ) ;
271
- }
272
-
273
290
let mut result = ConfigurationExportResult :: new ( ) ;
274
291
let mut conf = config_doc:: Configuration :: new ( ) ;
275
292
293
+ let pb = get_progress_bar ( config. resources . len ( ) as u64 ) ?;
276
294
for resource in & config. resources {
295
+ pb. inc ( 1 ) ;
296
+ pb. set_message ( format ! ( "Export {}" , resource. name) ) ;
277
297
let Some ( dsc_resource) = self . discovery . find_resource ( & resource. resource_type . to_lowercase ( ) ) else {
278
298
return Err ( DscError :: ResourceNotFound ( resource. resource_type . clone ( ) ) ) ;
279
299
} ;
@@ -283,7 +303,7 @@ impl Configurator {
283
303
}
284
304
285
305
result. result = Some ( conf) ;
286
-
306
+ pb . finish_and_clear ( ) ;
287
307
Ok ( result)
288
308
}
289
309
@@ -367,27 +387,6 @@ impl Configurator {
367
387
Ok ( ( ) )
368
388
}
369
389
370
- fn find_duplicate_resource_types ( config : & Configuration ) -> Vec < String >
371
- {
372
- let mut map: HashMap < & String , i32 > = HashMap :: new ( ) ;
373
- let mut result: HashSet < String > = HashSet :: new ( ) ;
374
- let resource_list = & config. resources ;
375
- if resource_list. is_empty ( ) {
376
- return Vec :: new ( ) ;
377
- }
378
-
379
- for r in resource_list
380
- {
381
- let v = map. entry ( & r. resource_type ) . or_insert ( 0 ) ;
382
- * v += 1 ;
383
- if * v > 1 {
384
- result. insert ( r. resource_type . clone ( ) ) ;
385
- }
386
- }
387
-
388
- result. into_iter ( ) . collect ( )
389
- }
390
-
391
390
fn validate_config ( & mut self ) -> Result < Configuration , DscError > {
392
391
let config: Configuration = serde_json:: from_str ( self . config . as_str ( ) ) ?;
393
392
0 commit comments