Skip to content

Commit

Permalink
feat: support JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
jfatta authored and cyx committed Jan 22, 2021
1 parent dbc1462 commit ea82282
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
9 changes: 9 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path"
"strings"
"sync"

"github.com/auth0/auth0-cli/internal/display"
Expand Down Expand Up @@ -33,6 +34,7 @@ type cli struct {

verbose bool
tenant string
format string

initOnce sync.Once
path string
Expand Down Expand Up @@ -96,9 +98,16 @@ func (c *cli) init() error {
c.tenant = c.data.DefaultTenant
}

format := strings.ToLower(c.format)
if format != "" && format != string(display.OutputFormatJSON) {
err = fmt.Errorf("Invalid format. Use `--format=json` or ommit this option to use the default format.")
return
}

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

Expand Down
5 changes: 4 additions & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ func Execute() {

rootCmd.SetUsageTemplate(namespaceUsageTemplate())
rootCmd.PersistentFlags().StringVar(&cli.tenant,
"tenant", "", "Specific tenant to use")
"tenant", "", "Specific tenant to use.")

rootCmd.PersistentFlags().BoolVar(&cli.verbose,
"verbose", false, "Enable verbose mode.")

rootCmd.PersistentFlags().StringVar(&cli.format,
"format", "", "Command output format. Options: json.")

rootCmd.AddCommand(clientsCmd(cli))
rootCmd.AddCommand(logsCmd(cli))

Expand Down
28 changes: 22 additions & 6 deletions internal/display/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,37 @@ import (
"gopkg.in/auth0.v5/management"
)

type clientView struct {
Name string
Type string
ClientID string
}

func (v *clientView) AsTableHeader() []string {
return []string{"Name", "Type", "ClientID"}
}

func (v *clientView) AsTableRow() []string {
return []string{v.Name, v.Type, ansi.Faint(v.ClientID)}
}

func (r *Renderer) ClientList(clients []*management.Client) {
r.Heading(ansi.Bold(r.Tenant), "clients\n")

var rows [][]string
var res []View
for _, c := range clients {
if auth0.StringValue(c.Name) == deprecatedAppName {
continue
}
rows = append(rows, []string{
auth0.StringValue(c.Name),
appTypeFor(c.AppType),
ansi.Faint(auth0.StringValue(c.ClientID)),
res = append(res, &clientView{
Name: auth0.StringValue(c.Name),
Type: appTypeFor(c.AppType),
ClientID: auth0.StringValue(c.ClientID),
})

}
r.Table([]string{"Name", "Type", "ClientID"}, rows)

r.Results(res)
}

// TODO(cyx): determine if there's a better way to filter this out.
Expand Down
37 changes: 36 additions & 1 deletion internal/display/display.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package display

import (
"encoding/json"
"fmt"
"io"
"os"
Expand All @@ -13,11 +14,19 @@ import (
"github.com/olekukonko/tablewriter"
)

type OutputFormat string

const (
OutputFormatJSON OutputFormat = "json"
)

type Renderer struct {
Tenant string

Writer io.Writer

Format OutputFormat

initOnce sync.Once
}

Expand Down Expand Up @@ -54,7 +63,33 @@ func (r *Renderer) Heading(text ...string) {
fmt.Fprintf(r.Writer, "%s %s\n", ansi.Faint("==="), strings.Join(text, " "))
}

func (r *Renderer) Table(header []string, data [][]string) {
type View interface {
AsTableHeader() []string
AsTableRow() []string
}

func (r *Renderer) Results(data []View) {
if len(data) > 0 {
switch r.Format {
case OutputFormatJSON:
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
r.Errorf("couldn't marshal results as JSON: %v", err)
return
}
fmt.Fprint(r.Writer, string(b))

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

func (r *Renderer) table(header []string, data [][]string) {
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader(header)
Expand Down

0 comments on commit ea82282

Please sign in to comment.