Skip to content

Commit

Permalink
fix: result sent to sdtout
Browse files Browse the repository at this point in the history
  • Loading branch information
jfatta authored and cyx committed Jan 22, 2021
1 parent ea82282 commit ae45356
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
Binary file added auth0
Binary file not shown.
7 changes: 4 additions & 3 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ func (c *cli) init() error {
}

c.renderer = &display.Renderer{
Tenant: c.tenant,
Writer: os.Stdout,
Format: display.OutputFormat(format),
Tenant: c.tenant,
MessageWriter: os.Stderr,
ResultWriter: os.Stdout,
Format: display.OutputFormat(format),
}
})

Expand Down
36 changes: 22 additions & 14 deletions internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,52 @@ const (
type Renderer struct {
Tenant string

Writer io.Writer
// MessageWriter receives the renderer messages (typically os.Stderr)
MessageWriter io.Writer

// ResultWriter writes the final result of the commands (typically os.Stdout which can be piped to other commands)
ResultWriter io.Writer

// Format indicates how the results are rendered. Default (empty) will write as table
Format OutputFormat

initOnce sync.Once
}

func (r *Renderer) init() {
r.initOnce.Do(func() {
if r.Writer == nil {
r.Writer = os.Stdout
if r.MessageWriter == nil {
r.MessageWriter = os.Stderr
}
if r.ResultWriter == nil {
r.ResultWriter = os.Stdout
}
})
}

func (r *Renderer) Infof(format string, a ...interface{}) {
r.init()

fmt.Fprint(r.Writer, aurora.Green(" ▸ "))
fmt.Fprintf(r.Writer, format+"\n", a...)
fmt.Fprint(r.MessageWriter, aurora.Green(" ▸ "))
fmt.Fprintf(r.MessageWriter, format+"\n", a...)
}

func (r *Renderer) Warnf(format string, a ...interface{}) {
r.init()

fmt.Fprint(r.Writer, aurora.Yellow(" ▸ "))
fmt.Fprintf(r.Writer, format+"\n", a...)
fmt.Fprint(r.MessageWriter, aurora.Yellow(" ▸ "))
fmt.Fprintf(r.MessageWriter, format+"\n", a...)
}

func (r *Renderer) Errorf(format string, a ...interface{}) {
r.init()

fmt.Fprint(r.Writer, aurora.BrightRed(" ▸ "))
fmt.Fprintf(r.Writer, format+"\n", a...)
fmt.Fprint(r.MessageWriter, aurora.BrightRed(" ▸ "))
fmt.Fprintf(r.MessageWriter, format+"\n", a...)
}

func (r *Renderer) Heading(text ...string) {
fmt.Fprintf(r.Writer, "%s %s\n", ansi.Faint("==="), strings.Join(text, " "))
fmt.Fprintf(r.MessageWriter, "%s %s\n", ansi.Faint("==="), strings.Join(text, " "))
}

type View interface {
Expand All @@ -77,19 +85,19 @@ func (r *Renderer) Results(data []View) {
r.Errorf("couldn't marshal results as JSON: %v", err)
return
}
fmt.Fprint(r.Writer, string(b))
fmt.Fprint(r.ResultWriter, string(b))

default:
rows := make([][]string, len(data))
for i, d := range data {
rows[i] = d.AsTableRow()
}
r.table(data[0].AsTableHeader(), rows)
writeTable(r.ResultWriter, data[0].AsTableHeader(), rows)
}
}
}

func (r *Renderer) table(header []string, data [][]string) {
func writeTable(w io.Writer, header []string, data [][]string) {
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader(header)
Expand All @@ -109,7 +117,7 @@ func (r *Renderer) table(header []string, data [][]string) {
}

table.Render()
fmt.Fprint(r.Writer, tableString.String())
fmt.Fprint(w, tableString.String())
}

func timeAgo(ts time.Time) string {
Expand Down
14 changes: 14 additions & 0 deletions output
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== jorgehack21 clients

[
{
"Name": "Default App",
"Type": "generic",
"ClientID": "lD1UVQMVT2SFC4ccesQC1V3mbXjeGIKF"
},
{
"Name": "My App",
"Type": "machine to machine",
"ClientID": "W6SMb7pwBfSEfb4zZPWu1UgSvalHhvPh"
}
]

0 comments on commit ae45356

Please sign in to comment.