From 4efb1c14ef327ce77eba7f081f6e8aa904fb8b3d Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 21 Jun 2021 13:59:41 -0300 Subject: [PATCH 1/4] Allow access token to be piped on test commands --- internal/cli/test.go | 4 ---- internal/display/get_token.go | 26 +++++++++++++++++++------- 2 files changed, 19 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/get_token.go b/internal/display/get_token.go index 5e4132229..4468bb309 100644 --- a/internal/display/get_token.go +++ b/internal/display/get_token.go @@ -3,16 +3,28 @@ package display import ( "encoding/json" "fmt" + "os" "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))) + fi, err := os.Stdout.Stat() + if err != nil { + panic(auth0.Error(err, "failed to get the FileInfo struct of stdout")) + } + + // pass the access token to the pipe and exit + if (fi.Mode() & os.ModeCharDevice) == 0 { + 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 +38,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 +49,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) } } From 5feb71e32dae7bd107dfc11ef4b4adcba93a86fd Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 21 Jun 2021 14:16:14 -0300 Subject: [PATCH 2/4] Extract helper method --- internal/display/display.go | 14 ++++++++++++++ internal/display/get_token.go | 8 +------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/internal/display/display.go b/internal/display/display.go index b8352959b..709e8089c 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" ) @@ -267,3 +268,16 @@ func indent(text, indent string) string { } return result[:len(result)-1] } + +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 4468bb309..57386e158 100644 --- a/internal/display/get_token.go +++ b/internal/display/get_token.go @@ -3,7 +3,6 @@ package display import ( "encoding/json" "fmt" - "os" "strconv" "github.com/auth0/auth0-cli/internal/auth/authutil" @@ -12,13 +11,8 @@ import ( ) func (r *Renderer) GetToken(c *management.Client, t *authutil.TokenResponse) { - fi, err := os.Stdout.Stat() - if err != nil { - panic(auth0.Error(err, "failed to get the FileInfo struct of stdout")) - } - // pass the access token to the pipe and exit - if (fi.Mode() & os.ModeCharDevice) == 0 { + if isOutputPiped() { fmt.Fprint(r.ResultWriter, t.AccessToken) return } From d03263789cdef018241d8fba29934969ff7719dd Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 21 Jun 2021 14:17:31 -0300 Subject: [PATCH 3/4] Run gofmt --- internal/display/get_token.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/display/get_token.go b/internal/display/get_token.go index 57386e158..faf6ed204 100644 --- a/internal/display/get_token.go +++ b/internal/display/get_token.go @@ -12,7 +12,7 @@ import ( func (r *Renderer) GetToken(c *management.Client, t *authutil.TokenResponse) { // pass the access token to the pipe and exit - if isOutputPiped() { + if isOutputPiped() { fmt.Fprint(r.ResultWriter, t.AccessToken) return } From 64dbfecfb8811ae216f567f50253ea8e17fbf923 Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 21 Jun 2021 14:24:30 -0300 Subject: [PATCH 4/4] Run gofmt --- internal/display/display.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/display/display.go b/internal/display/display.go index 709e8089c..ba20dbdd5 100644 --- a/internal/display/display.go +++ b/internal/display/display.go @@ -272,10 +272,10 @@ func indent(text, indent string) string { func isOutputPiped() bool { fi, err := os.Stdout.Stat() if err != nil { - panic(auth0.Error(err, "failed to get the FileInfo struct of stdout")) - } + panic(auth0.Error(err, "failed to get the FileInfo struct of stdout")) + } - if (fi.Mode() & os.ModeCharDevice) == 0 { + if (fi.Mode() & os.ModeCharDevice) == 0 { return true }