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

api show: use proper formatting #95

Merged
merged 1 commit into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion internal/display/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package display

import (
"fmt"
"strconv"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
Expand All @@ -13,6 +14,8 @@ type apiView struct {
Name string
Identifier string
Scopes int

raw interface{}
}

func (v *apiView) AsTableHeader() []string {
Expand All @@ -23,6 +26,19 @@ func (v *apiView) AsTableRow() []string {
return []string{ansi.Faint(v.ID), v.Name, v.Identifier, fmt.Sprint(v.Scopes)}
}

func (v *apiView) KeyValues() [][]string {
return [][]string{
[]string{"ID", v.ID},
[]string{"NAME", v.Name},
[]string{"IDENTIFIER", v.Identifier},
[]string{"SCOPES", strconv.Itoa(v.Scopes)},
}
}

func (v *apiView) Object() interface{} {
return v.raw
}

func (r *Renderer) ApiList(apis []*management.ResourceServer) {
r.Heading(ansi.Bold(r.Tenant), "APIs\n")

Expand All @@ -37,7 +53,7 @@ func (r *Renderer) ApiList(apis []*management.ResourceServer) {

func (r *Renderer) ApiShow(api *management.ResourceServer) {
r.Heading(ansi.Bold(r.Tenant), "API\n")
r.Results([]View{makeApiView(api)})
r.Result(makeApiView(api))
}

func (r *Renderer) ApiCreate(api *management.ResourceServer) {
Expand All @@ -58,6 +74,8 @@ func makeApiView(api *management.ResourceServer) *apiView {
Name: auth0.StringValue(api.Name),
Identifier: auth0.StringValue(api.Identifier),
Scopes: auth0.IntValue(&scopes),

raw: api,
}
}

Expand Down
22 changes: 22 additions & 0 deletions internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ func (r *Renderer) Results(data []View) {
}
}

func (r *Renderer) Result(data View) {
switch r.Format {
case OutputFormatJSON:
// TODO(cyx): we're type asserting on the fly to prevent too
// many changes in other places. In the future we should
// enforce `Object` on all `View` types.
if v, ok := data.(interface{ Object() interface{} }); ok {
r.JSONResult(v.Object())
} else {
r.JSONResult(data)
}

default:
// TODO(cyx): we're type asserting on the fly to prevent too
// many changes in other places. In the future we should
// enforce `KeyValues` on all `View` types.
if v, ok := data.(interface{ KeyValues() [][]string }); ok {
writeTable(r.ResultWriter, nil, v.KeyValues())
}
}
}

func (r *Renderer) Stream(data []View, ch <-chan View) {
w := r.ResultWriter

Expand Down