Skip to content

Commit

Permalink
Show a hint the first time test login runs [CLI-72] (#199)
Browse files Browse the repository at this point in the history
* Show a hint the first time `test login` runs

* Fix wrong param

* Update internal/cli/cli.go

Co-authored-by: Jorge L. Fatta <[email protected]>

* Add omitempty

Co-authored-by: Jorge L. Fatta <[email protected]>
  • Loading branch information
Widcket and jfatta authored Mar 26, 2021
1 parent 4a5037d commit 5cbf783
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
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,omitempty"`
}

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

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

0 comments on commit 5cbf783

Please sign in to comment.