Skip to content

feat(cli): add --output={text,json} to version cmd #7010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 5, 2023
Prev Previous commit
Next Next commit
feat(cliui): add TextFormat
  • Loading branch information
johnstcn committed Apr 5, 2023
commit f218bcf67d79778bc6bfabe12d3804e594104e31
21 changes: 21 additions & 0 deletions cli/cliui/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cliui
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"

Expand Down Expand Up @@ -171,3 +172,23 @@ func (jsonFormat) Format(_ context.Context, data any) (string, error) {

return string(outBytes), nil
}

type textFormat struct{}

var _ OutputFormat = textFormat{}

// TextFormat is a formatter that just outputs unstructured text.
// It uses fmt.Sprintf under the hood.
func TextFormat() OutputFormat {
return textFormat{}
}

func (textFormat) ID() string {
return "text"
}

func (textFormat) AttachOptions(_ *clibase.OptionSet) {}

func (textFormat) Format(_ context.Context, data any) (string, error) {
return fmt.Sprintf("%s", data), nil
}
3 changes: 3 additions & 0 deletions cli/cliui/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func Test_OutputFormatter(t *testing.T) {
require.Panics(t, func() {
cliui.NewOutputFormatter(cliui.JSONFormat())
})
require.NotPanics(t, func() {
cliui.NewOutputFormatter(cliui.JSONFormat(), cliui.TextFormat())
})
})

t.Run("NoMissingFormatID", func(t *testing.T) {
Expand Down