Skip to content

Commit

Permalink
Add spinners where missing in: connections, logs, rules, try-login
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisscott committed Jan 26, 2021
1 parent 33034a0 commit f2497a7
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 119 deletions.
10 changes: 9 additions & 1 deletion internal/cli/connections.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
Expand Down
44 changes: 26 additions & 18 deletions internal/cli/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"
"time"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"
)
Expand All @@ -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
}
Expand Down
89 changes: 51 additions & 38 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
},
}

Expand All @@ -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
},
}

Expand Down
134 changes: 72 additions & 62 deletions internal/cli/try_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit f2497a7

Please sign in to comment.