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

Show scopes in apis subcommands instead of count #174

Merged
merged 14 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,13 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210305023407-0d6cb8bd5a4b h1:zQ+/dCJWTuLZNCt92+rfDzgYfIWkoCRrcMAPBiQ6bt4=
golang.org/x/sys v0.0.0-20210305023407-0d6cb8bd5a4b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d h1:jbzgAvDZn8aEnytae+4ou0J0GwFZoHR0hOrTg4qH8GA=
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210317153231-de623e64d2a6 h1:EC6+IGYTjPpRfv9a2b/6Puw0W+hLtAhkV1tPsXhutqs=
golang.org/x/term v0.0.0-20210317153231-de623e64d2a6/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ auth0 apis show <id>
}

cli.renderer.ApiShow(api)
cli.renderer.Newline()
cli.renderer.Infof("Scopes may be truncated. To see the full list, run %s", ansi.Faint(fmt.Sprintf("apis scopes list %s", inputs.ID)))
return nil
},
}
Expand Down
91 changes: 84 additions & 7 deletions internal/display/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package display

import (
"fmt"
"os"
"strconv"
"strings"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"golang.org/x/term"
"gopkg.in/auth0.v5/management"
)

type apiView struct {
ID string
Name string
Identifier string
Scopes int
Scopes string

raw interface{}
}
Expand All @@ -28,24 +31,54 @@ func (v *apiView) AsTableRow() []string {

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

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

type apiTableView struct {
ID string
Name string
Identifier string
Scopes int

raw interface{}
}

func (v *apiTableView) AsTableHeader() []string {
return []string{"ID", "Name", "Identifier", "Scopes"}
}

func (v *apiTableView) AsTableRow() []string {
return []string{ansi.Faint(v.ID), v.Name, v.Identifier, fmt.Sprint(v.Scopes)}
}

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

chrisscott marked this conversation as resolved.
Show resolved Hide resolved
func (v *apiTableView) Object() interface{} {
return v.raw
}

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

results := []View{}

for _, api := range apis {
results = append(results, makeApiView(api))
results = append(results, makeApiTableView(api))
}

r.Results(results)
Expand All @@ -67,9 +100,21 @@ func (r *Renderer) ApiUpdate(api *management.ResourceServer) {
}

func makeApiView(api *management.ResourceServer) *apiView {
scopes := len(api.Scopes)

return &apiView{
ID: auth0.StringValue(api.ID),
Name: auth0.StringValue(api.Name),
Identifier: auth0.StringValue(api.Identifier),
Scopes: auth0.StringValue(truncateScopes(api.Scopes)),
chrisscott marked this conversation as resolved.
Show resolved Hide resolved

raw: api,
}
}

func makeApiTableView(api *management.ResourceServer) *apiTableView {
scopes := len(api.Scopes)

return &apiTableView{
ID: auth0.StringValue(api.ID),
Name: auth0.StringValue(api.Name),
Identifier: auth0.StringValue(api.Identifier),
Expand Down Expand Up @@ -110,3 +155,35 @@ func makeScopeView(scope *management.ResourceServerScope) *scopeView {
Description: auth0.StringValue(scope.Description),
}
}

func truncateScopes(scopes []*management.ResourceServerScope) *string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen Shot 2021-03-22 at 13 51 50

Scopes that fit shouldn't be truncated.
Also let's not display the hint if the scopes are not truncated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 good catch. Refactoring this now to bring the hint into the display logic instead of the cli logic.

ellipsis := "..."
padding := 16 // the longest apiView key plus two spaces before and after in the label column
width, _, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
width = 80
}

var results []string
charactersNeeded := width - len(ellipsis) - padding

for _, scope := range scopes {
runeScope := []rune(*scope.Value)
characterLength := len(runeScope)
if charactersNeeded < characterLength {
break
}
charactersNeeded -= characterLength + 1 // add one for the space between them
results = append(results, *scope.Value)
}

var truncatedScopes string

if len(results) == 0 {
truncatedScopes = "n/a"
} else {
truncatedScopes = fmt.Sprintf("%s...", strings.NewReplacer("[", "", "]", "").Replace(fmt.Sprintf("%v", results)))
}

return &truncatedScopes
}