From a2860eed10e9aea9fcdb3e74dbeedc775e68029a Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Mon, 21 Nov 2022 22:49:53 +0530 Subject: [PATCH] Print version info to stdout instead of stderr (#1503) ## Description Previously we were printing the version information with `corso --version` to stderr. This changes it to stdout. ## Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Test - [ ] :computer: CI/Deployment - [x] :hamster: Trivial/Minor ## Issue(s) * fixes https://github.com/alcionai/corso/issues/1361 ## Test Plan - [x] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- src/cli/cli.go | 2 +- src/cli/print/print.go | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/cli/cli.go b/src/cli/cli.go index b161be6e8..378675003 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -40,7 +40,7 @@ var corsoCmd = &cobra.Command{ func handleCorsoCmd(cmd *cobra.Command, args []string) error { v, _ := cmd.Flags().GetBool("version") if v { - print.Infof(cmd.Context(), "Corso\nversion: "+version) + print.Outf(cmd.Context(), "Corso\nversion: "+version) return nil } diff --git a/src/cli/print/print.go b/src/cli/print/print.go index 7cbb9c80d..f417c8def 100644 --- a/src/cli/print/print.go +++ b/src/cli/print/print.go @@ -89,6 +89,38 @@ func err(w io.Writer, s ...any) { fmt.Fprint(w, msg...) } +// Out prints the params to cobra's output writer (stdOut by default) +// if s is nil, prints nothing. +func Out(ctx context.Context, s ...any) { + out(getRootCmd(ctx).OutOrStdout(), s...) +} + +// out is the testable core of Out() +func out(w io.Writer, s ...any) { + if len(s) == 0 { + return + } + + fmt.Fprint(w, s...) + fmt.Fprintf(w, "\n") +} + +// Out prints the formatted strings to cobra's output writer (stdOut by default) +// if t is empty, prints nothing. +func Outf(ctx context.Context, t string, s ...any) { + outf(getRootCmd(ctx).OutOrStdout(), t, s...) +} + +// outf is the testable core of Outf() +func outf(w io.Writer, t string, s ...any) { + if len(t) == 0 { + return + } + + fmt.Fprintf(w, t, s...) + fmt.Fprintf(w, "\n") +} + // Info prints the params to cobra's error writer (stdErr by default) // if s is nil, prints nothing. func Info(ctx context.Context, s ...any) { @@ -138,14 +170,12 @@ type Printable interface { // Item prints the printable, according to the caller's requested format. func Item(ctx context.Context, p Printable) { - print(getRootCmd(ctx).OutOrStdout(), p) + printItem(getRootCmd(ctx).OutOrStdout(), p) } // print prints the printable items, // according to the caller's requested format. -// -//revive:disable:redefines-builtin-id -func print(w io.Writer, p Printable) { +func printItem(w io.Writer, p Printable) { if outputAsJSON || outputAsJSONDebug { outputJSON(w, p, outputAsJSONDebug) return