@@ -11,7 +11,7 @@ use std::env;
1111use std:: error:: Error ;
1212use std:: ffi:: OsStr ;
1313use std:: fmt:: { Display , Formatter } ;
14- use std:: io;
14+ use std:: io:: { self , Write } ;
1515use std:: process;
1616
1717use maximus_checks:: {
@@ -20,9 +20,9 @@ use maximus_checks::{
2020} ;
2121use maximus_core:: {
2222 apply_fixes, find_ignore_root, load_ignore_file_pattern_sources, load_maximus_config,
23- preview_fixes, scope_ignore_patterns, select_fix_plans, select_planned_fixes , AuditResult ,
24- EnvTemplateRenderOptions , FailOnLevel , FixPlan , FixSelector , LoadConfigError , MaximusConfig ,
25- PlannedFix ,
23+ prepare_text_write , preview_fixes, scope_ignore_patterns, select_fix_plans,
24+ select_planned_fixes , write_text , AuditResult , EnvTemplateRenderOptions , FailOnLevel , FixPlan ,
25+ FixSelector , LoadConfigError , MaximusConfig , PlannedFix ,
2626} ;
2727
2828use crate :: args:: { parse_args, ArgsError , Flags , OutputFormat } ;
@@ -192,6 +192,9 @@ fn run_fix_command(
192192 }
193193 let selected_fixes = select_fix_plans ( & initial. result . fixes , & selector) ;
194194 let selected_initial = result_with_selected_fixes ( & initial. result , selected_fixes. clone ( ) ) ;
195+ if !flags. dry_run {
196+ prepare_report_output ( flags) ?;
197+ }
195198 let previewed = if flags. dry_run && flags. diff {
196199 Some ( preview_fixes ( & planned) ?)
197200 } else {
@@ -232,41 +235,25 @@ fn run_fix_command(
232235}
233236
234237fn print_audit_report ( flags : & Flags , result : & AuditResult ) -> Result < ( ) , CliError > {
235- match flags. output_format {
236- crate :: args:: OutputFormat :: Text => {
237- println ! ( "{}" , report_text:: format_audit_report( result) ) ;
238- }
239- crate :: args:: OutputFormat :: Json => {
240- println ! ( "{}" , report_json:: render_audit_result( result) ?) ;
241- }
242- crate :: args:: OutputFormat :: Markdown => {
243- println ! ( "{}" , report_markdown:: format_audit_report( result) ) ;
244- }
245- crate :: args:: OutputFormat :: Sarif => {
246- println ! ( "{}" , report_sarif:: render_audit_result( result) ?) ;
247- }
248- }
238+ let report = match flags. output_format {
239+ crate :: args:: OutputFormat :: Text => report_text:: format_audit_report ( result) ,
240+ crate :: args:: OutputFormat :: Json => report_json:: render_audit_result ( result) ?,
241+ crate :: args:: OutputFormat :: Markdown => report_markdown:: format_audit_report ( result) ,
242+ crate :: args:: OutputFormat :: Sarif => report_sarif:: render_audit_result ( result) ?,
243+ } ;
249244
250- Ok ( ( ) )
245+ write_report_output ( flags , & report )
251246}
252247
253248fn print_doctor_report ( flags : & Flags , result : & AuditResult ) -> Result < ( ) , CliError > {
254- match flags. output_format {
255- crate :: args:: OutputFormat :: Text => {
256- println ! ( "{}" , report_text:: format_doctor_report( result) ) ;
257- }
258- crate :: args:: OutputFormat :: Json => {
259- println ! ( "{}" , report_json:: render_audit_result( result) ?) ;
260- }
261- crate :: args:: OutputFormat :: Markdown => {
262- println ! ( "{}" , report_markdown:: format_doctor_report( result) ) ;
263- }
264- crate :: args:: OutputFormat :: Sarif => {
265- println ! ( "{}" , report_sarif:: render_doctor_result( result) ?) ;
266- }
267- }
249+ let report = match flags. output_format {
250+ crate :: args:: OutputFormat :: Text => report_text:: format_doctor_report ( result) ,
251+ crate :: args:: OutputFormat :: Json => report_json:: render_audit_result ( result) ?,
252+ crate :: args:: OutputFormat :: Markdown => report_markdown:: format_doctor_report ( result) ,
253+ crate :: args:: OutputFormat :: Sarif => report_sarif:: render_doctor_result ( result) ?,
254+ } ;
268255
269- Ok ( ( ) )
256+ write_report_output ( flags , & report )
270257}
271258
272259#[ allow( clippy:: too_many_arguments) ]
@@ -280,66 +267,73 @@ fn print_fix_report(
280267 preview_report : Option < & str > ,
281268 previews : Option < & [ maximus_core:: PreviewedFix ] > ,
282269) -> Result < ( ) , CliError > {
283- match flags. output_format {
284- crate :: args:: OutputFormat :: Text => {
285- println ! (
286- "{}" ,
287- report_text:: format_fix_result(
288- flags. dry_run,
289- target_dir,
290- initial,
291- applied,
292- final_result,
293- selected_fixes,
294- preview_report,
295- )
296- ) ;
297- }
298- crate :: args:: OutputFormat :: Json => {
299- println ! (
300- "{}" ,
301- report_json:: render_fix_result(
302- flags. dry_run,
303- target_dir,
304- initial,
305- applied,
306- final_result,
307- previews,
308- ) ?
309- ) ;
310- }
311- crate :: args:: OutputFormat :: Markdown => {
312- println ! (
313- "{}" ,
314- report_markdown:: format_fix_result(
315- flags. dry_run,
316- target_dir,
317- initial,
318- applied,
319- final_result,
320- selected_fixes,
321- preview_report,
322- )
323- ) ;
270+ let report = match flags. output_format {
271+ crate :: args:: OutputFormat :: Text => report_text:: format_fix_result (
272+ flags. dry_run ,
273+ target_dir,
274+ initial,
275+ applied,
276+ final_result,
277+ selected_fixes,
278+ preview_report,
279+ ) ,
280+ crate :: args:: OutputFormat :: Json => report_json:: render_fix_result (
281+ flags. dry_run ,
282+ target_dir,
283+ initial,
284+ applied,
285+ final_result,
286+ previews,
287+ ) ?,
288+ crate :: args:: OutputFormat :: Markdown => report_markdown:: format_fix_result (
289+ flags. dry_run ,
290+ target_dir,
291+ initial,
292+ applied,
293+ final_result,
294+ selected_fixes,
295+ preview_report,
296+ ) ,
297+ crate :: args:: OutputFormat :: Sarif => report_sarif:: render_fix_result (
298+ flags. dry_run ,
299+ target_dir,
300+ initial,
301+ applied,
302+ final_result,
303+ previews,
304+ ) ?,
305+ } ;
306+
307+ write_report_output ( flags, & report)
308+ }
309+
310+ fn write_report_output ( flags : & Flags , report : & str ) -> Result < ( ) , CliError > {
311+ let content = format ! ( "{report}\n " ) ;
312+
313+ match flags. output_path . as_deref ( ) {
314+ Some ( output_path) if output_path != OsStr :: new ( "-" ) => {
315+ write_text ( std:: path:: Path :: new ( output_path) , & content) ?;
324316 }
325- crate :: args:: OutputFormat :: Sarif => {
326- println ! (
327- "{}" ,
328- report_sarif:: render_fix_result(
329- flags. dry_run,
330- target_dir,
331- initial,
332- applied,
333- final_result,
334- previews,
335- ) ?
336- ) ;
317+ _ => {
318+ io:: stdout ( ) . lock ( ) . write_all ( content. as_bytes ( ) ) ?;
337319 }
338320 }
339321
340322 Ok ( ( ) )
341323}
342324
325+ fn prepare_report_output ( flags : & Flags ) -> Result < ( ) , CliError > {
326+ if let Some ( output_path) = flags
327+ . output_path
328+ . as_deref ( )
329+ . filter ( |output_path| * output_path != OsStr :: new ( "-" ) )
330+ {
331+ prepare_text_write ( std:: path:: Path :: new ( output_path) ) ?;
332+ }
333+
334+ Ok ( ( ) )
335+ }
336+
343337fn resolve_effective_config (
344338 target_dir : & std:: path:: Path ,
345339 flags : & Flags ,
0 commit comments