diff --git a/internal/cli/apps.go b/internal/cli/apps.go index d43c4ccbc..b17b910d9 100644 --- a/internal/cli/apps.go +++ b/internal/cli/apps.go @@ -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", @@ -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 +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 } @@ -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" @@ -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) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index b258b9e2b..3c480c5e5 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -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 { @@ -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 }