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

Show a hint the first time test login runs [CLI-72] #199

Merged
merged 6 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion internal/cli/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func selectArgument(cmd *cobra.Command, a *Argument, value interface{}, options

func askArgument(cmd *cobra.Command, i commandInput, value interface{}) error {
if canPrompt(cmd) {
return ask(cmd, i, value, nil, true)
return ask(cmd, i, value, nil, false)
}

return fmt.Errorf("Missing a required argument: %s", i.GetName())
Expand Down
61 changes: 56 additions & 5 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ type config struct {
// tenant is the cli's concept of an auth0 tenant. The fields are tailor fit
// specifically for interacting with the management API.
type tenant struct {
Name string `json:"name"`
Domain string `json:"domain"`
AccessToken string `json:"access_token,omitempty"`
ExpiresAt time.Time `json:"expires_at"`
Name string `json:"name"`
Domain string `json:"domain"`
AccessToken string `json:"access_token,omitempty"`
ExpiresAt time.Time `json:"expires_at"`
Apps map[string]app `json:"apps"`
}

type app struct {
FirstRuns map[string]bool `json:"first_runs"`
}

var errUnauthenticated = errors.New("Not yet configured. Try `auth0 login`.")
Expand Down Expand Up @@ -224,7 +229,7 @@ func (c *cli) addTenant(ten tenant) error {
c.config.Tenants[ten.Name] = ten

if err := c.persistConfig(); err != nil {
return fmt.Errorf("persisting config: %w", err)
return fmt.Errorf("Unexpected error persisting config: %w", err)
Widcket marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
Expand Down Expand Up @@ -271,6 +276,52 @@ func (c *cli) removeTenant(ten string) error {
return nil
}

func (c *cli) isFirstCommandRun(clientID string, command string) (bool, error) {
tenant, err := c.getTenant()

if err != nil {
return false, err
}

if a, found := tenant.Apps[clientID]; found {
if a.FirstRuns[command] {
return false, nil
}
}

return true, nil
}

func (c *cli) setFirstCommandRun(clientID string, command string) error {
tenant, err := c.getTenant()

if err != nil {
return err
}

if a, found := tenant.Apps[clientID]; found {
if a.FirstRuns == nil {
a.FirstRuns = map[string]bool{}
}
a.FirstRuns[command] = true
tenant.Apps[clientID] = a
} else {
tenant.Apps[clientID] = app{
FirstRuns: map[string]bool{
command: true,
},
}
}

c.config.Tenants[tenant.Name] = tenant

if err := c.persistConfig(); err != nil {
return fmt.Errorf("Unexpected error persisting config: %w", err)
}

return nil
}

func (c *cli) persistConfig() error {
dir := filepath.Dir(c.path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
Expand Down
17 changes: 17 additions & 0 deletions internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ If --client-id is not provided, the default client "CLI Login Testing" will be u
auth0 test login --client-id <id>
auth0 test login -c <id> --connection <connection>`,
RunE: func(cmd *cobra.Command, args []string) error {
const commandKey = "test_login"
var userInfo *authutil.UserInfo

tenant, err := cli.getTenant()
if err != nil {
return err
Expand Down Expand Up @@ -116,6 +118,21 @@ auth0 test login -c <id> --connection <connection>`,

fmt.Fprint(cli.renderer.MessageWriter, "\n")
cli.renderer.TryLogin(userInfo, tokenResponse)

isFirstRun, err := cli.isFirstCommandRun(inputs.ClientID, commandKey)
if err != nil {
return err
}

if isFirstRun {
cli.renderer.Infof("%s Login flow is working! Next, try downloading and running a Quickstart: 'auth0 quickstarts download %s'",
ansi.Faint("Hint:"), inputs.ClientID)

if err := cli.setFirstCommandRun(inputs.ClientID, commandKey); err != nil {
return err
}
}

return nil
},
}
Expand Down