Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spinners where missing in: connections, logs, rules, try-login #45

Merged
merged 3 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 24 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,31 @@ 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++
}
page++
}
return err
})

return
}
Expand Down
65 changes: 43 additions & 22 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,21 +55,29 @@ 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)

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)
}
return nil
})

if err != nil {
return err
}

// @TODO Only display modified rules
Expand All @@ -91,18 +104,26 @@ 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)

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)
}
return nil
})

if err != nil {
return err
}

// @TODO Only display modified rules
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