From f2497a7959245320f3deb00b1bdd107f04acc9b5 Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Tue, 26 Jan 2021 13:29:06 -0500 Subject: [PATCH 1/3] Add spinners where missing in: connections, logs, rules, try-login --- internal/cli/connections.go | 10 ++- internal/cli/logs.go | 44 +++++++----- internal/cli/rules.go | 89 ++++++++++++++---------- internal/cli/try_login.go | 134 +++++++++++++++++++----------------- 4 files changed, 158 insertions(+), 119 deletions(-) diff --git a/internal/cli/connections.go b/internal/cli/connections.go index 6090c3838..a0201be50 100644 --- a/internal/cli/connections.go +++ b/internal/cli/connections.go @@ -1,7 +1,9 @@ package cli import ( + "github.com/auth0/auth0-cli/internal/ansi" "github.com/spf13/cobra" + "gopkg.in/auth0.v5/management" ) func connectionsCmd(cli *cli) *cobra.Command { @@ -26,7 +28,13 @@ Lists your existing connections. To create one try: $ auth0 connections create `, RunE: func(cmd *cobra.Command, args []string) error { - list, err := cli.api.Connection.List() + var list *management.ConnectionList + err := ansi.Spinner("Getting connections", func() error { + var err error + list, err = cli.api.Connection.List() + return err + }) + if err != nil { return err } diff --git a/internal/cli/logs.go b/internal/cli/logs.go index 7e63422c8..8efb09be2 100644 --- a/internal/cli/logs.go +++ b/internal/cli/logs.go @@ -5,6 +5,7 @@ import ( "sort" "time" + "github.com/auth0/auth0-cli/internal/ansi" "github.com/spf13/cobra" "gopkg.in/auth0.v5/management" ) @@ -22,26 +23,33 @@ func getLatestLogs(cli *cli, n int) (result []*management.Log, err error) { if perPage > count { perPage = count } - for count > len(result) { - list, err = cli.api.Log.List( - management.Parameter("sort", "date:-1"), - management.Parameter("page", fmt.Sprintf("%d", page)), - management.Parameter("per_page", fmt.Sprintf("%d", perPage)), - ) - if err != nil { - return - } - sort.Slice(list, func(i, j int) bool { - return list[i].GetDate().Before(list[j].GetDate()) - }) - result = append(list, result...) - if len(list) < perPage { - // We've got all - break + err = ansi.Spinner("Getting logs", func() error { + for count > len(result) { + var err error + list, err = cli.api.Log.List( + management.Parameter("sort", "date:-1"), + management.Parameter("page", fmt.Sprintf("%d", page)), + management.Parameter("per_page", fmt.Sprintf("%d", perPage)), + ) + if err != nil { + return err + } + + sort.Slice(list, func(i, j int) bool { + return list[i].GetDate().Before(list[j].GetDate()) + }) + result = append(list, result...) + if len(list) < perPage { + // We've got all + break + } + page++ + + return nil } - page++ - } + return err + }) return } diff --git a/internal/cli/rules.go b/internal/cli/rules.go index b04a9cf48..035ad6be1 100644 --- a/internal/cli/rules.go +++ b/internal/cli/rules.go @@ -32,7 +32,12 @@ func listRulesCmd(cli *cli) *cobra.Command { Short: "Lists your rules", Long: `Lists the rules in your current tenant.`, RunE: func(cmd *cobra.Command, args []string) error { - rules, err := getRules(cli) + var rules *management.RuleList + err := ansi.Spinner("Getting rules", func() error { + var err error + rules, err = getRules(cli) + return err + }) if err != nil { return err @@ -50,33 +55,37 @@ func enableRuleCmd(cli *cli) *cobra.Command { var name string cmd := &cobra.Command{ Use: "enable", - Short: "enable rule(s)", + Short: "enable rule", RunE: func(cmd *cobra.Command, args []string) error { - data, err := getRules(cli) - if err != nil { - return err - } - - rule := findRuleByName(name, data.Rules) - if rule != nil { - err := enableRule(rule, cli) + err := ansi.Spinner("Enabling rule", func() error { + var err error + data, err := getRules(cli) if err != nil { return err } - } else { - return fmt.Errorf("No rule found with name: %q", name) - } - // @TODO Only display modified rules - rules, err := getRules(cli) + rule := findRuleByName(name, data.Rules) + if rule != nil { + err := enableRule(rule, cli) + if err != nil { + return err + } + } else { + return fmt.Errorf("No rule found with name: %q", name) + } - if err != nil { - return err - } + // @TODO Only display modified rules + rules, err := getRules(cli) - cli.renderer.RulesList(rules) + if err != nil { + return err + } - return nil + cli.renderer.RulesList(rules) + + return nil + }) + return err }, } @@ -91,30 +100,34 @@ func disableRuleCmd(cli *cli) *cobra.Command { Use: "disable", Short: "disable rule", RunE: func(cmd *cobra.Command, args []string) error { - data, err := getRules(cli) - if err != nil { - return err - } - - rule := findRuleByName(name, data.Rules) - if rule != nil { - if err := disableRule(rule, cli); err != nil { + err := ansi.Spinner("Disabling rule", func() error { + var err error + data, err := getRules(cli) + if err != nil { return err } - } else { - return fmt.Errorf("No rule found with name: %q", name) - } - // @TODO Only display modified rules - rules, err := getRules(cli) + rule := findRuleByName(name, data.Rules) + if rule != nil { + if err := disableRule(rule, cli); err != nil { + return err + } + } else { + return fmt.Errorf("No rule found with name: %q", name) + } - if err != nil { - return err - } + // @TODO Only display modified rules + rules, err := getRules(cli) - cli.renderer.RulesList(rules) + if err != nil { + return err + } - return nil + cli.renderer.RulesList(rules) + + return nil + }) + return err }, } diff --git a/internal/cli/try_login.go b/internal/cli/try_login.go index cf9eee731..3fffae734 100644 --- a/internal/cli/try_login.go +++ b/internal/cli/try_login.go @@ -7,6 +7,7 @@ import ( "net/url" "time" + "github.com/auth0/auth0-cli/internal/ansi" "github.com/auth0/auth0-cli/internal/auth" "github.com/auth0/auth0-cli/internal/auth0" "github.com/auth0/auth0-cli/internal/open" @@ -35,81 +36,90 @@ func tryLoginCmd(cli *cli) *cobra.Command { Launch a browser to try out your universal login box for the given client. `, RunE: func(cmd *cobra.Command, args []string) error { - tenant, err := cli.getTenant() - if err != nil { - return err - } + var userInfo *auth.UserInfo + var tokenResponse *auth.TokenResponse - // use the client ID as passed in by the user, or default to the - // "CLI Login Testing" client if none passed. This client is only - // used for testing login from the CLI and will be created if it - // does not exist. - if clientID == "" { - client, err := getOrCreateCLITesterClient(cli.api.Client) + err := ansi.Spinner("Trying login", func() error { + var err error + tenant, err := cli.getTenant() if err != nil { return err } - clientID = client.GetClientID() - } - client, err := cli.api.Client.Read(clientID) - if err != nil { - return err - } + // use the client ID as passed in by the user, or default to the + // "CLI Login Testing" client if none passed. This client is only + // used for testing login from the CLI and will be created if it + // does not exist. + if clientID == "" { + client, err := getOrCreateCLITesterClient(cli.api.Client) + if err != nil { + return err + } + clientID = client.GetClientID() + } - // check if the client's initiate_login_uri matches the one for our - // "CLI Login Testing" app. If so, then initiate the login via the - // `/authorize` endpoint, if not, open a browser at the client's - // configured URL. If none is specified, return an error to the - // caller explaining the problem. - if client.GetInitiateLoginURI() == "" { - return fmt.Errorf( - "client %s does not specify a URL with which to initiate login", - client.GetClientID(), - ) - } + client, err := cli.api.Client.Read(clientID) + if err != nil { + return err + } - if client.GetInitiateLoginURI() != cliLoginTestingInitiateLoginURI { - if connectionName != "" { - cli.renderer.Warnf("Specific connections are not supported when using a non-default client, ignoring.") - cli.renderer.Warnf("You should ensure the connection you wish to test is enabled for the client you want to use in the Auth0 Dashboard.") + // check if the client's initiate_login_uri matches the one for our + // "CLI Login Testing" app. If so, then initiate the login via the + // `/authorize` endpoint, if not, open a browser at the client's + // configured URL. If none is specified, return an error to the + // caller explaining the problem. + if client.GetInitiateLoginURI() == "" { + return fmt.Errorf( + "client %s does not specify a URL with which to initiate login", + client.GetClientID(), + ) } - return open.URL(client.GetInitiateLoginURI()) - } - // Build a login URL and initiate login in a browser window. - loginURL, err := buildInitiateLoginURL(tenant.Domain, client.GetClientID(), connectionName) - if err != nil { - return err - } + if client.GetInitiateLoginURI() != cliLoginTestingInitiateLoginURI { + if connectionName != "" { + cli.renderer.Warnf("Specific connections are not supported when using a non-default client, ignoring.") + cli.renderer.Warnf("You should ensure the connection you wish to test is enabled for the client you want to use in the Auth0 Dashboard.") + } + return open.URL(client.GetInitiateLoginURI()) + } - if err := open.URL(loginURL); err != nil { - return err - } + // Build a login URL and initiate login in a browser window. + loginURL, err := buildInitiateLoginURL(tenant.Domain, client.GetClientID(), connectionName) + if err != nil { + return err + } - // launch a HTTP server to wait for the callback to capture the auth - // code. - authCode, err := waitForBrowserCallback() - if err != nil { - return err - } + if err := open.URL(loginURL); err != nil { + return err + } - // once the callback is received, exchange the code for an access - // token. - tokenResponse, err := auth.ExchangeCodeForToken( - tenant.Domain, - client.GetClientID(), - client.GetClientSecret(), - authCode, - cliLoginTestingCallbackURL, - ) - if err != nil { - return fmt.Errorf("%w", err) - } + // launch a HTTP server to wait for the callback to capture the auth + // code. + authCode, err := waitForBrowserCallback() + if err != nil { + return err + } + + // once the callback is received, exchange the code for an access + // token. + tokenResponse, err = auth.ExchangeCodeForToken( + tenant.Domain, + client.GetClientID(), + client.GetClientSecret(), + authCode, + cliLoginTestingCallbackURL, + ) + if err != nil { + return fmt.Errorf("%w", err) + } + + // Use the access token to fetch user information from the /userinfo + // endpoint. + userInfo, err = auth.FetchUserInfo(tenant.Domain, tokenResponse.AccessToken) + + return err + }) - // Use the access token to fetch user information from the /userinfo - // endpoint. - userInfo, err := auth.FetchUserInfo(tenant.Domain, tokenResponse.AccessToken) if err != nil { return err } From f1c32bed498459a8f91657bc64d211349c40ff30 Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Tue, 26 Jan 2021 13:33:45 -0500 Subject: [PATCH 2/3] Finish spinner after enable/disable --- internal/cli/rules.go | 44 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/internal/cli/rules.go b/internal/cli/rules.go index 035ad6be1..4ef5528f2 100644 --- a/internal/cli/rules.go +++ b/internal/cli/rules.go @@ -73,19 +73,23 @@ func enableRuleCmd(cli *cli) *cobra.Command { } else { return fmt.Errorf("No rule found with name: %q", name) } + return nil + }) - // @TODO Only display modified rules - rules, err := getRules(cli) + if err != nil { + return err + } - if err != nil { - return err - } + // @TODO Only display modified rules + rules, err := getRules(cli) - cli.renderer.RulesList(rules) + if err != nil { + return err + } - return nil - }) - return err + cli.renderer.RulesList(rules) + + return nil }, } @@ -115,19 +119,23 @@ func disableRuleCmd(cli *cli) *cobra.Command { } else { return fmt.Errorf("No rule found with name: %q", name) } + return nil + }) - // @TODO Only display modified rules - rules, err := getRules(cli) + if err != nil { + return err + } - if err != nil { - return err - } + // @TODO Only display modified rules + rules, err := getRules(cli) - cli.renderer.RulesList(rules) + if err != nil { + return err + } - return nil - }) - return err + cli.renderer.RulesList(rules) + + return nil }, } From 36853616f78db6939b1d44a8f201e8264e6bc6c1 Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Tue, 26 Jan 2021 13:36:57 -0500 Subject: [PATCH 3/3] Fix unconditional termination of loop --- internal/cli/logs.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/cli/logs.go b/internal/cli/logs.go index 8efb09be2..4dcaba830 100644 --- a/internal/cli/logs.go +++ b/internal/cli/logs.go @@ -45,8 +45,6 @@ func getLatestLogs(cli *cli, n int) (result []*management.Log, err error) { break } page++ - - return nil } return err })