Skip to content

Commit 46ab0f5

Browse files
authored
pbm cli: fix 'follow logs' option to respect output format (json, json-pretty) (#932)
1 parent 83db267 commit 46ab0f5

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

cmd/pbm/main.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func main() {
431431
case cleanupCmd.FullCommand():
432432
out, err = doCleanup(ctx, conn, pbm, &cleanupOpts)
433433
case logsCmd.FullCommand():
434-
out, err = runLogs(ctx, conn, &logs)
434+
out, err = runLogs(ctx, conn, &logs, pbmOutF)
435435
case statusCmd.FullCommand():
436436
out, err = status(ctx, conn, pbm, *mURL, statusOpts, pbmOutF == outJSONpretty)
437437
case describeRestoreCmd.FullCommand():
@@ -496,7 +496,7 @@ func exitErr(e error, f outFormat) {
496496
os.Exit(1)
497497
}
498498

499-
func runLogs(ctx context.Context, conn connect.Client, l *logsOpts) (fmt.Stringer, error) {
499+
func runLogs(ctx context.Context, conn connect.Client, l *logsOpts, f outFormat) (fmt.Stringer, error) {
500500
r := &log.LogRequest{}
501501

502502
if l.node != "" {
@@ -535,7 +535,7 @@ func runLogs(ctx context.Context, conn connect.Client, l *logsOpts) (fmt.Stringe
535535
}
536536

537537
if l.follow {
538-
err := followLogs(ctx, conn, r, r.Node == "", l.extr)
538+
err := followLogs(ctx, conn, r, r.Node == "", l.extr, f)
539539
return nil, err
540540
}
541541

@@ -561,17 +561,32 @@ func runLogs(ctx context.Context, conn connect.Client, l *logsOpts) (fmt.Stringe
561561
return o, nil
562562
}
563563

564-
func followLogs(ctx context.Context, conn connect.Client, r *log.LogRequest, showNode, expr bool) error {
564+
func followLogs(ctx context.Context, conn connect.Client, r *log.LogRequest, showNode, expr bool, f outFormat) error {
565565
outC, errC := log.Follow(ctx, conn, r, false)
566566

567+
var enc *json.Encoder
568+
if f == outJSON {
569+
enc = json.NewEncoder(os.Stdout)
570+
} else if f == outJSONpretty {
571+
enc = json.NewEncoder(os.Stdout)
572+
enc.SetIndent("", " ")
573+
}
574+
567575
for {
568576
select {
569577
case entry, ok := <-outC:
570578
if !ok {
571579
return nil
572580
}
573581

574-
fmt.Println(entry.Stringify(tsUTC, showNode, expr))
582+
if f == outJSON || f == outJSONpretty {
583+
err := enc.Encode(entry)
584+
if err != nil {
585+
exitErr(errors.Wrap(err, "encode output"), f)
586+
}
587+
} else {
588+
fmt.Println(entry.Stringify(tsUTC, showNode, expr))
589+
}
575590
case err, ok := <-errC:
576591
if !ok {
577592
return nil

0 commit comments

Comments
 (0)