Skip to content

Commit c4bc91e

Browse files
committed
remove use of static, add parameter, rename function
1 parent af60e6c commit c4bc91e

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
lines changed

dsc/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn main() {
7676
exit(util::EXIT_JSON_ERROR);
7777
}
7878
};
79-
util::write_output(&json, output_format.as_ref());
79+
util::write_object(&json, output_format.as_ref(), false);
8080
},
8181
}
8282

dsc/src/resource_command.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33

44
use crate::args::OutputFormat;
5-
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_RESOURCE_NOT_FOUND, add_type_name_to_json, write_output};
5+
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_RESOURCE_NOT_FOUND, add_type_name_to_json, write_object};
66
use dsc_lib::configure::config_doc::{Configuration, ExecutionKind};
77
use dsc_lib::configure::add_resource_export_results_to_configuration;
88
use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}};
@@ -48,7 +48,7 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: Opt
4848
exit(EXIT_JSON_ERROR);
4949
}
5050
};
51-
write_output(&json, format);
51+
write_object(&json, format, false);
5252
}
5353
Err(err) => {
5454
error!("Error: {err}");
@@ -88,6 +88,7 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
8888
}
8989
};
9090

91+
let mut first = true;
9192
for instance in export_result.actual_state
9293
{
9394
let get_result = GetResult::Resource(ResourceGetResponse {
@@ -101,7 +102,8 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
101102
exit(EXIT_JSON_ERROR);
102103
}
103104
};
104-
write_output(&json, format);
105+
write_object(&json, format, !first);
106+
first = false;
105107
}
106108
}
107109

@@ -142,7 +144,7 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: Opt
142144
exit(EXIT_JSON_ERROR);
143145
}
144146
};
145-
write_output(&json, format);
147+
write_object(&json, format, false);
146148
}
147149
Err(err) => {
148150
error!("Error: {err}");
@@ -188,7 +190,7 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: Op
188190
exit(EXIT_JSON_ERROR);
189191
}
190192
};
191-
write_output(&json, format);
193+
write_object(&json, format, false);
192194
}
193195
Err(err) => {
194196
error!("{err}");
@@ -248,7 +250,7 @@ pub fn schema(dsc: &DscManager, resource_type: &str, format: Option<&OutputForma
248250
exit(EXIT_JSON_ERROR);
249251
}
250252
};
251-
write_output(&json, format);
253+
write_object(&json, format, false);
252254
}
253255
Err(err) => {
254256
error!("Error: {err}");
@@ -294,7 +296,7 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, format: Option<&OutputF
294296
exit(EXIT_JSON_ERROR);
295297
}
296298
};
297-
write_output(&json, format);
299+
write_object(&json, format, false);
298300
}
299301

300302
#[must_use]

dsc/src/subcommand.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::args::{ConfigSubCommand, DscType, OutputFormat, ResourceSubCommand};
55
use crate::resolve::{get_contents, Include};
66
use crate::resource_command::{get_resource, self};
77
use crate::tablewriter::Table;
8-
use crate::util::{DSC_CONFIG_ROOT, EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_INVALID_INPUT, EXIT_JSON_ERROR, get_schema, write_output, get_input, set_dscconfigroot, validate_json};
8+
use crate::util::{DSC_CONFIG_ROOT, EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_INVALID_INPUT, EXIT_JSON_ERROR, get_schema, write_object, get_input, set_dscconfigroot, validate_json};
99
use dsc_lib::{
1010
configure::{
1111
config_doc::{
@@ -48,7 +48,7 @@ pub fn config_get(configurator: &mut Configurator, format: Option<&OutputFormat>
4848
exit(EXIT_JSON_ERROR);
4949
}
5050
};
51-
write_output(&json, format);
51+
write_object(&json, format, false);
5252
}
5353
else {
5454
let json = match serde_json::to_string(&result) {
@@ -58,7 +58,7 @@ pub fn config_get(configurator: &mut Configurator, format: Option<&OutputFormat>
5858
exit(EXIT_JSON_ERROR);
5959
}
6060
};
61-
write_output(&json, format);
61+
write_object(&json, format, false);
6262
if result.had_errors {
6363
exit(EXIT_DSC_ERROR);
6464
}
@@ -83,7 +83,7 @@ pub fn config_set(configurator: &mut Configurator, format: Option<&OutputFormat>
8383
exit(EXIT_JSON_ERROR);
8484
}
8585
};
86-
write_output(&json, format);
86+
write_object(&json, format, false);
8787
}
8888
else {
8989
let json = match serde_json::to_string(&result) {
@@ -93,7 +93,7 @@ pub fn config_set(configurator: &mut Configurator, format: Option<&OutputFormat>
9393
exit(EXIT_JSON_ERROR);
9494
}
9595
};
96-
write_output(&json, format);
96+
write_object(&json, format, false);
9797
if result.had_errors {
9898
exit(EXIT_DSC_ERROR);
9999
}
@@ -169,7 +169,7 @@ pub fn config_test(configurator: &mut Configurator, format: Option<&OutputFormat
169169
}
170170
}
171171
};
172-
write_output(&json, format);
172+
write_object(&json, format, false);
173173
}
174174
else {
175175
let json = match serde_json::to_string(&result) {
@@ -179,7 +179,7 @@ pub fn config_test(configurator: &mut Configurator, format: Option<&OutputFormat
179179
exit(EXIT_JSON_ERROR);
180180
}
181181
};
182-
write_output(&json, format);
182+
write_object(&json, format, false);
183183
if result.had_errors {
184184
exit(EXIT_DSC_ERROR);
185185
}
@@ -203,7 +203,7 @@ pub fn config_export(configurator: &mut Configurator, format: Option<&OutputForm
203203
exit(EXIT_JSON_ERROR);
204204
}
205205
};
206-
write_output(&json, format);
206+
write_object(&json, format, false);
207207
if result.had_errors {
208208

209209
for msg in result.messages
@@ -401,7 +401,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
401401
exit(EXIT_JSON_ERROR);
402402
};
403403

404-
write_output(&json, output_format.as_ref());
404+
write_object(&json, output_format.as_ref(), false);
405405
},
406406
ConfigSubCommand::Export { output_format, .. } => {
407407
config_export(&mut configurator, output_format.as_ref());
@@ -435,7 +435,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
435435
exit(EXIT_JSON_ERROR);
436436
}
437437
};
438-
write_output(&json_string, output_format.as_ref());
438+
write_object(&json_string, output_format.as_ref(), false);
439439
},
440440
}
441441
}
@@ -591,6 +591,7 @@ fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_
591591
// write as table if format is not specified and interactive
592592
write_table = true;
593593
}
594+
let mut first = true;
594595
for resource in dsc.list_available_resources(resource_name.unwrap_or(&String::from("*")), adapter_name.unwrap_or(&String::new()), progress_format) {
595596
let mut capabilities = "--------".to_string();
596597
let capability_types = [
@@ -667,7 +668,8 @@ fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_
667668
exit(EXIT_JSON_ERROR);
668669
}
669670
};
670-
write_output(&json, format);
671+
write_object(&json, format, !first);
672+
first = false;
671673
// insert newline separating instances if writing to console
672674
if io::stdout().is_terminal() { println!(); }
673675
}

dsc/src/util.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,14 @@ pub fn get_schema(dsc_type: DscType) -> RootSchema {
208208
}
209209
}
210210

211-
static mut FIRST_WRITE: bool = true;
212-
213-
/// Write the output to the console
211+
/// Write the JSON object to the console
214212
///
215213
/// # Arguments
216214
///
217215
/// * `json` - The JSON to write
218216
/// * `format` - The format to use
219-
pub fn write_output(json: &str, format: Option<&OutputFormat>) {
217+
/// * `include_separator` - Whether to include a separator for YAML before the object
218+
pub fn write_object(json: &str, format: Option<&OutputFormat>, include_separator: bool) {
220219
let mut is_json = true;
221220
let mut output_format = format;
222221
let mut syntax_color = false;
@@ -250,8 +249,7 @@ pub fn write_output(json: &str, format: Option<&OutputFormat>) {
250249
},
251250
Some(OutputFormat::Yaml) | None => {
252251
is_json = false;
253-
if !unsafe { FIRST_WRITE } {
254-
// include YAML document separator for subsequent outputs
252+
if include_separator {
255253
println!("---");
256254
}
257255

@@ -297,10 +295,6 @@ pub fn write_output(json: &str, format: Option<&OutputFormat>) {
297295
else {
298296
println!("{output}");
299297
}
300-
301-
unsafe {
302-
FIRST_WRITE = false;
303-
}
304298
}
305299

306300
#[allow(clippy::too_many_lines)]

0 commit comments

Comments
 (0)