Skip to content
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