Skip to content

Commit

Permalink
apps picker: allow configurability of default app
Browse files Browse the repository at this point in the history
Closes #190
  • Loading branch information
cyx committed Mar 27, 2021
1 parent ae70133 commit d7d9aef
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
63 changes: 62 additions & 1 deletion internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ var (
Help: "Name of the application.",
IsRequired: true,
}
appNone = Flag{
Name: "None",
LongForm: "none",
ShortForm: "n",
Help: "Specify none of your apps",
}

appType = Flag{
Name: "Type",
LongForm: "type",
Expand Down Expand Up @@ -117,7 +124,57 @@ func appsCmd(cli *cli) *cobra.Command {
cmd.AddCommand(showAppCmd(cli))
cmd.AddCommand(updateAppCmd(cli))
cmd.AddCommand(deleteAppCmd(cli))
cmd.AddCommand(useAppCmd(cli))

return cmd
}

func useAppCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
None bool
}

cmd := &cobra.Command{
Use: "use",
Short: "Choose a default application",
Long: `auth0 apps use <client-id>
Specify your preferred application for interaction with the auth0 CLI
`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},

RunE: func(cmd *cobra.Command, args []string) error {
if inputs.None {
inputs.ID = ""
} else {
if len(args) == 0 {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions)
if err != nil {
return err
}
} else {
inputs.ID = args[0]
}
}

if err := cli.setDefaultAppID(inputs.ID); err != nil {
return err
}

if inputs.ID == "" {
cli.renderer.Infof("Successfully removed the default application")
} else {
cli.renderer.Infof("Successfully set the default application to %s", ansi.Faint(inputs.ID))
cli.renderer.Infof("%s: You might wanna try `auth0 quickstarts download`", ansi.Faint("Hint:"))
}

return nil
},
}

appNone.RegisterBool(cmd, &inputs.None, false)
return cmd
}

Expand Down Expand Up @@ -352,6 +409,10 @@ auth0 apps create --name myapp --type [native|spa|regular|m2m]
return fmt.Errorf("Unable to create application: %w", err)
}

if err := cli.setDefaultAppID(a.GetClientID()); err != nil {
return err
}

// Render result
// note: a is populated with the rest of the client fields by the API during creation.
revealClientSecret := auth0.StringValue(a.AppType) != "native" && auth0.StringValue(a.AppType) != "spa"
Expand Down Expand Up @@ -736,7 +797,7 @@ func (c *cli) appPickerOptions() (pickerOptions, error) {
opt := pickerOption{value: value, label: label}

// check if this is currently the default application.
if _, ok := tenant.Apps[c.GetClientID()]; ok {
if tenant.DefaultAppID == c.GetClientID() {
priorityOpts = append(priorityOpts, opt)
} else {
opts = append(opts, opt)
Expand Down
28 changes: 22 additions & 6 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ 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"`
Apps map[string]app `json:"apps,omitempty"`
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"`
DefaultAppID string `json:"default_app_id,omitempty"`
}

type app struct {
Expand Down Expand Up @@ -292,9 +293,24 @@ func (c *cli) isFirstCommandRun(clientID string, command string) (bool, error) {
return true, nil
}

func (c *cli) setFirstCommandRun(clientID string, command string) error {
func (c *cli) setDefaultAppID(id string) error {
tenant, err := c.getTenant()
if err != nil {
return err
}

tenant.DefaultAppID = id

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) setFirstCommandRun(clientID string, command string) error {
tenant, err := c.getTenant()
if err != nil {
return err
}
Expand Down

0 comments on commit d7d9aef

Please sign in to comment.