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

Allow access token to be piped on test login [CLI-186] #317

Merged
merged 5 commits into from
Jun 22, 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
4 changes: 0 additions & 4 deletions internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ auth0 test token --client-id <id> --audience <audience> --scopes <scope1,scope2>
if err != nil {
return fmt.Errorf("An unexpected error occurred while logging in to machine-to-machine client %s: %w", inputs.ClientID, err)
}

fmt.Fprint(cli.renderer.MessageWriter, "\n")
cli.renderer.GetToken(client, tokenResponse)
return nil
}
Expand All @@ -278,8 +276,6 @@ auth0 test token --client-id <id> --audience <audience> --scopes <scope1,scope2>
if err != nil {
return fmt.Errorf("An unexpected error occurred when logging in to client %s: %w", inputs.ClientID, err)
}

fmt.Fprint(cli.renderer.MessageWriter, "\n")
cli.renderer.GetToken(client, tokenResponse)
return nil
},
Expand Down
14 changes: 14 additions & 0 deletions internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/charmbracelet/glamour"
"github.com/olekukonko/tablewriter"
)
Expand Down Expand Up @@ -274,3 +275,16 @@ func boolean(v bool) string {
}
return ansi.Red("✗")
}

func isOutputPiped() bool {
fi, err := os.Stdout.Stat()
if err != nil {
panic(auth0.Error(err, "failed to get the FileInfo struct of stdout"))
}

if (fi.Mode() & os.ModeCharDevice) == 0 {
return true
}

return false
}
20 changes: 13 additions & 7 deletions internal/display/get_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (
"fmt"
"strconv"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth/authutil"
"github.com/auth0/auth0-cli/internal/auth0"
"gopkg.in/auth0.v5/management"
)

func (r *Renderer) GetToken(c *management.Client, t *authutil.TokenResponse) {
r.Heading(fmt.Sprintf("tokens for %s", auth0.StringValue(c.Name)))
// pass the access token to the pipe and exit
if isOutputPiped() {
fmt.Fprint(r.ResultWriter, t.AccessToken)
return
}

fmt.Fprint(r.ResultWriter, "\n")
r.Heading(fmt.Sprintf("token for %s", auth0.StringValue(c.Name)))

switch r.Format {
case OutputFormatJSON:
Expand All @@ -26,24 +32,24 @@ func (r *Renderer) GetToken(c *management.Client, t *authutil.TokenResponse) {
rows := make([][]string, 0)

if isNotZero(t.AccessToken) {
rows = append(rows, []string{ansi.Faint("AccessToken"), t.AccessToken})
rows = append(rows, []string{"ACCESS TOKEN", t.AccessToken})
}
if isNotZero(t.RefreshToken) {
rows = append(rows, []string{ansi.Faint("RefreshToken"), t.RefreshToken})
rows = append(rows, []string{"REFRESH TOKEN", t.RefreshToken})
}
// TODO: This is a long string and it messes up formatting when printed
// to the table, so need to come back to this one and fix it later.
// if isNotZero(t.IDToken) {
// rows = append(rows, []string{ansi.Faint("IDToken"), t.IDToken})
// }
if isNotZero(t.TokenType) {
rows = append(rows, []string{ansi.Faint("TokenType"), t.TokenType})
rows = append(rows, []string{"TOKEN TYPE", t.TokenType})
}
if isNotZero(t.ExpiresIn) {
rows = append(rows, []string{ansi.Faint("ExpiresIn"), strconv.FormatInt(t.ExpiresIn, 10)})
rows = append(rows, []string{"EXPIRES IN", strconv.FormatInt(t.ExpiresIn, 10)})
}

tableHeader := []string{"Field", "Value"}
tableHeader := []string{"", ""}
writeTable(r.ResultWriter, tableHeader, rows)
}
}