From 331bd862115090dc1bb52a367f73d03dd4677c4f Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Tue, 22 Jun 2021 18:33:30 -0300 Subject: [PATCH] Allow access token to be piped on `test login` [CLI-186] (#317) --- internal/cli/test.go | 4 ---- internal/display/display.go | 14 ++++++++++++++ internal/display/get_token.go | 20 +++++++++++++------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/internal/cli/test.go b/internal/cli/test.go index 58bfa1e1d..e6dc68618 100644 --- a/internal/cli/test.go +++ b/internal/cli/test.go @@ -255,8 +255,6 @@ auth0 test token --client-id --audience --scopes 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 } @@ -278,8 +276,6 @@ auth0 test token --client-id --audience --scopes 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 }, diff --git a/internal/display/display.go b/internal/display/display.go index 74e239807..f1030e311 100644 --- a/internal/display/display.go +++ b/internal/display/display.go @@ -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" ) @@ -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 +} diff --git a/internal/display/get_token.go b/internal/display/get_token.go index 5e4132229..faf6ed204 100644 --- a/internal/display/get_token.go +++ b/internal/display/get_token.go @@ -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: @@ -26,10 +32,10 @@ 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. @@ -37,13 +43,13 @@ func (r *Renderer) GetToken(c *management.Client, t *authutil.TokenResponse) { // 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) } }