44
55//! `omdb db sitrep` subcommands
66
7+ use crate :: Omdb ;
78use crate :: db:: DbFetchOptions ;
89use crate :: db:: check_limit;
910use crate :: helpers:: const_max_len;
1011use crate :: helpers:: datetime_opt_rfc3339_concise;
12+ use crate :: helpers:: should_colorize;
1113use anyhow:: Context ;
1214use async_bb8_diesel:: AsyncRunQueryDsl ;
1315use chrono:: { DateTime , Utc } ;
@@ -239,17 +241,23 @@ impl SitrepSelector {
239241}
240242
241243pub ( super ) async fn cmd_db_sitrep (
244+ omdb : & Omdb ,
242245 opctx : & OpContext ,
243246 datastore : & DataStore ,
244247 fetch_opts : & DbFetchOptions ,
245248 args : & SitrepArgs ,
246249) -> anyhow:: Result < ( ) > {
250+ let colored =
251+ should_colorize ( omdb. output . color , supports_color:: Stream :: Stdout ) ;
247252 match args. command {
248253 Commands :: History ( ref args) => {
249254 cmd_db_sitrep_history ( opctx, datastore, fetch_opts, args) . await
250255 }
251256 Commands :: Info { sitrep, opts : ref args } => {
252- cmd_db_sitrep_show ( opctx, datastore, fetch_opts, args, sitrep) . await
257+ cmd_db_sitrep_show (
258+ opctx, datastore, fetch_opts, args, sitrep, colored,
259+ )
260+ . await
253261 }
254262 Commands :: Current ( ref args) => {
255263 cmd_db_sitrep_show (
@@ -258,12 +266,15 @@ pub(super) async fn cmd_db_sitrep(
258266 fetch_opts,
259267 args,
260268 SitrepSelector :: Current ,
269+ colored,
261270 )
262271 . await
263272 }
264273 Commands :: AnalysisReport ( ref args) => {
265- cmd_db_sitrep_analysis_report ( opctx, datastore, fetch_opts, args)
266- . await
274+ cmd_db_sitrep_analysis_report (
275+ opctx, datastore, fetch_opts, args, colored,
276+ )
277+ . await
267278 }
268279 }
269280}
@@ -347,6 +358,7 @@ async fn cmd_db_sitrep_show(
347358 _fetch_opts : & DbFetchOptions ,
348359 opts : & ShowOptions ,
349360 sitrep_selector : SitrepSelector ,
361+ colored : bool ,
350362) -> anyhow:: Result < ( ) > {
351363 let current_version = datastore
352364 . fm_current_sitrep_version ( & opctx)
@@ -499,7 +511,7 @@ async fn cmd_db_sitrep_show(
499511 if !cases. is_empty ( ) {
500512 println ! ( "\n {:=<80}\n " , "== CASES " ) ;
501513 for case in cases {
502- println ! ( "{}" , case. display_indented( 4 , Some ( id) ) ) ;
514+ println ! ( "{}" , case. display_indented( 4 , Some ( id) ) . colored ( colored ) ) ;
503515 }
504516 }
505517
@@ -511,6 +523,7 @@ async fn cmd_db_sitrep_analysis_report(
511523 datastore : & DataStore ,
512524 _fetch_opts : & DbFetchOptions ,
513525 args : & AnalysisReportArgs ,
526+ colored : bool ,
514527) -> anyhow:: Result < ( ) > {
515528 let & AnalysisReportArgs { sitrep, ref opts } = args;
516529 let ( _, id) = sitrep. resolve ( & datastore, & opctx) . await ?;
@@ -519,14 +532,15 @@ async fn cmd_db_sitrep_analysis_report(
519532 load_analysis_report ( datastore, id) . await . with_context ( || {
520533 format ! ( "failed to load analysis report for {err_ctx}" , )
521534 } ) ?;
522- print_analysis_report ( & report, opts. json ) ?;
535+ print_analysis_report ( & report, opts. json , colored ) ?;
523536
524537 Ok ( ( ) )
525538}
526539
527540fn print_analysis_report (
528541 report : & model:: fm:: SitrepAnalysisReport ,
529542 json : bool ,
543+ colored : bool ,
530544) -> anyhow:: Result < ( ) > {
531545 use nexus_types:: fm:: analysis_reports:: { AnalysisReport , InputReport } ;
532546
@@ -566,27 +580,33 @@ fn print_analysis_report(
566580
567581 println ! ( "\n {:=<80}" , "== ANALYSIS INPUT REPORT " ) ;
568582 match serde_json:: from_value :: < InputReport > ( input_report. clone ( ) ) {
569- Ok ( report) => println ! ( "{}" , report. display_multiline( 0 ) ) ,
583+ Ok ( report) => {
584+ println ! ( "{}" , report. display_multiline( 0 ) . colored( colored) )
585+ }
570586 Err ( e) => {
571587 eprintln ! (
572588 "WARNING: failed to parse input report; falling back to \
573589 less structured output: {e}"
574590 ) ;
575- let displayer = nexus_types:: fm:: display:: Json :: new ( & input_report) ;
591+ let displayer = nexus_types:: fm:: display:: Json :: new ( & input_report)
592+ . colored ( colored) ;
576593 println ! ( "{displayer}" ) ;
577594 }
578595 }
579596
580597 println ! ( "\n {:=<80}" , "== ANALYSIS REPORT " ) ;
581598 match serde_json:: from_value :: < AnalysisReport > ( analysis_report. clone ( ) ) {
582- Ok ( report) => println ! ( "{}" , report. display_multiline( 0 ) ) ,
599+ Ok ( report) => {
600+ println ! ( "{}" , report. display_multiline( 0 ) . colored( colored) )
601+ }
583602 Err ( e) => {
584603 eprintln ! (
585604 "WARNING: failed to parse analysis report; falling back to \
586605 less structured output: {e}"
587606 ) ;
588607 let displayer =
589- nexus_types:: fm:: display:: Json :: new ( & analysis_report) ;
608+ nexus_types:: fm:: display:: Json :: new ( & analysis_report)
609+ . colored ( colored) ;
590610 println ! ( "{displayer}" ) ;
591611 }
592612 }
0 commit comments