Skip to content

Commit

Permalink
Merge branch 'vmware-tanzu:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
mpanchajanya authored Jan 16, 2024
2 parents 0708666 + 8b70591 commit cdef98a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
17 changes: 17 additions & 0 deletions component/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
type OutputWriter interface {
SetKeys(headerKeys ...string)
AddRow(items ...interface{})
SetText(text string)
Render()
}

Expand All @@ -38,13 +39,16 @@ const (
JSONOutputType OutputType = "json"
// ListTableOutputType specified output should be in a list table format.
ListTableOutputType OutputType = "listtable"
// TextOutputType specifies output should be in text format.
Text OutputType = "text"
)

// outputwriter is our internal implementation.
type outputwriter struct {
out io.Writer
keys []string
values [][]interface{}
text string
outputFormat OutputType
autoStringifyFields bool
}
Expand Down Expand Up @@ -100,6 +104,11 @@ func (ow *outputwriter) SetKeys(headerKeys ...string) {
ow.keys = headerKeys
}

// SetText sets the text to be rendered.
func (ow *outputwriter) SetText(text string) {
ow.text = text
}

func stringify(items []interface{}) []interface{} {
var results []interface{}
for i := range items {
Expand Down Expand Up @@ -131,6 +140,8 @@ func (ow *outputwriter) Render() {
renderYAML(ow.out, ow.dataStruct())
case ListTableOutputType:
renderListTable(ow)
case Text:
fmt.Fprint(ow.out, ow.text)
default:
renderTable(ow)
}
Expand Down Expand Up @@ -187,6 +198,12 @@ func (obw *objectwriter) AddRow(_ ...interface{}) {
fmt.Fprintln(obw.out, "Programming error, attempt to add rows to object output")
}

// SetText sets the text to be rendered.
func (obw *objectwriter) SetText(_ string) {
// Object writer does not have the concept of text
fmt.Fprintln(obw.out, "Programming error, attempt to add text to object output")
}

// Render emits the generated table to the output once ready
func (obw *objectwriter) Render() {
switch obw.outputFormat {
Expand Down
4 changes: 2 additions & 2 deletions component/output_spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewOutputWriterSpinnerWithOptions(output io.Writer, outputFormat, spinnerTe
func (ows *outputwriterspinner) RenderWithSpinner() {
if ows.spinner != nil && ows.spinner.Active() {
ows.spinner.Stop()
fmt.Fprintln(ows.out)
fmt.Fprint(ows.out)
}
ows.Render()
}
Expand All @@ -77,6 +77,6 @@ func (ows *outputwriterspinner) RenderWithSpinner() {
func (ows *outputwriterspinner) StopSpinner() {
if ows.spinner != nil && ows.spinner.Active() {
ows.spinner.Stop()
fmt.Fprintln(ows.out)
fmt.Fprint(ows.out)
}
}
11 changes: 11 additions & 0 deletions component/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,17 @@ func TestObjectWriterYAML(t *testing.T) {
require.Contains(t, lines[1], "spacename: Jupiter")
}

// TestNewOutputWriterText verifies that text output is rendered as-is.
func TestNewOutputWriterText(t *testing.T) {
var b bytes.Buffer
msg := "this is a test"
txt := NewOutputWriterWithOptions(&b, string(Text), nil)
require.NotNil(t, txt)
txt.SetText(msg)
txt.Render()
require.Equal(t, msg, b.String())
}

type testStruct struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Namespace string `json:"spacename,omitempty" yaml:"spacename,omitempty"`
Expand Down

0 comments on commit cdef98a

Please sign in to comment.