@@ -72,10 +72,7 @@ func (s *CometBFTServer[T]) StatusCommand() *cobra.Command {
7272 return err
7373 }
7474
75- cmd .Println (string (output ))
76-
77- // TODO: figure out yaml and json output
78- return nil
75+ return printOutput (cmd , output )
7976 },
8077 }
8178
@@ -215,15 +212,12 @@ for. Each module documents its respective events under 'xx_events.md'.
215212 return err
216213 }
217214
218- // return clientCtx.PrintProto(blocks) // TODO: previously we had this, but I think it can be replaced with a simple json marshal.
219- // We are missing YAML output tho.
220215 bz , err := protojson .Marshal (blocks )
221216 if err != nil {
222217 return err
223218 }
224219
225- _ , err = cmd .OutOrStdout ().Write (bz )
226- return err
220+ return printOutput (cmd , bz )
227221 },
228222 }
229223
@@ -282,15 +276,12 @@ $ %s query block --%s=%s <hash>
282276 return fmt .Errorf ("no block found with height %s" , args [0 ])
283277 }
284278
285- // return clientCtx.PrintProto(output)
286-
287279 bz , err := json .Marshal (output )
288280 if err != nil {
289281 return err
290282 }
291283
292- _ , err = cmd .OutOrStdout ().Write (bz )
293- return err
284+ return printOutput (cmd , bz )
294285
295286 case auth .TypeHash :
296287
@@ -308,14 +299,12 @@ $ %s query block --%s=%s <hash>
308299 return fmt .Errorf ("no block found with hash %s" , args [0 ])
309300 }
310301
311- // return clientCtx.PrintProto(output)
312302 bz , err := json .Marshal (output )
313303 if err != nil {
314304 return err
315305 }
316306
317- _ , err = cmd .OutOrStdout ().Write (bz )
318- return err
307+ return printOutput (cmd , bz )
319308
320309 default :
321310 return fmt .Errorf ("unknown --%s value %s" , auth .FlagType , typ )
@@ -371,10 +360,7 @@ func (s *CometBFTServer[T]) QueryBlockResultsCmd() *cobra.Command {
371360 return err
372361 }
373362
374- cmd .Println (string (blockResStr ))
375-
376- // TODO: figure out yaml and json output
377- return nil
363+ return printOutput (cmd , blockResStr )
378364 },
379365 }
380366
@@ -425,3 +411,33 @@ func (s *CometBFTServer[T]) BootstrapStateCmd() *cobra.Command {
425411
426412 return cmd
427413}
414+
415+ func printOutput (cmd * cobra.Command , out []byte ) error {
416+ // Get flags output
417+ outFlag , err := cmd .Flags ().GetString (flags .FlagOutput )
418+ if err != nil {
419+ return err
420+ }
421+
422+ if outFlag == "text" {
423+ out , err = yaml .JSONToYAML (out )
424+ if err != nil {
425+ return err
426+ }
427+ }
428+
429+ writer := cmd .OutOrStdout ()
430+ _ , err = writer .Write (out )
431+ if err != nil {
432+ return err
433+ }
434+
435+ if outFlag != "text" {
436+ // append new-line for formats besides YAML
437+ _ , err = writer .Write ([]byte ("\n " ))
438+ if err != nil {
439+ return err
440+ }
441+ }
442+ return nil
443+ }
0 commit comments