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 }