Skip to content

Commit

Permalink
Fix apps shown in multi select when no app-id is passed (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught authored Feb 14, 2023
1 parent 686a9c0 commit 2ba24a6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 45 deletions.
75 changes: 32 additions & 43 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func useAppCmd(cli *cli) *cobra.Command {
inputs.ID = ""
} else {
if len(args) == 0 {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions)
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions())
if err != nil {
return err
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func showAppCmd(cli *cli) *cobra.Command {
auth0 apps show <app-id> -r --json`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions)
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions())
if err != nil {
return err
}
Expand Down Expand Up @@ -319,7 +319,7 @@ func deleteAppCmd(cli *cli) *cobra.Command {
auth0 apps delete <app-id> --force`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions)
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions())
if err != nil {
return err
}
Expand Down Expand Up @@ -536,7 +536,7 @@ func updateAppCmd(cli *cli) *cobra.Command {
var current *management.Client

if len(args) == 0 {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions)
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions())
if err != nil {
return err
}
Expand Down Expand Up @@ -727,7 +727,7 @@ func openAppCmd(cli *cli) *cobra.Command {
auth0 apps open <app-id>`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions)
err := appID.Pick(cmd, &inputs.ID, cli.appPickerOptions())
if err != nil {
return err
}
Expand Down Expand Up @@ -868,48 +868,37 @@ func stringSliceToPtr(s []string) *[]string {
return &s
}

func (c *cli) appPickerOptions() (pickerOptions, error) {
list, err := c.api.Client.List()
if err != nil {
return nil, err
}

tenant, err := c.getTenant()
if err != nil {
return nil, err
}

// NOTE(cyx): To keep the contract for this simple, we'll rely on the
// implicit knowledge that the default value for the picker is the
// first option. With that in mind, we'll use the state in
// tenant.DefaultAppID to determine which should be chosen as the
// default.
var (
priorityOpts, opts pickerOptions
)
for _, c := range list.Clients {
// Empty type means the default client that we shouldn't display.
// TODO(sergiught): We only need to exclude generic app types for
// the auth0 qs download command. Fix this in separate PR.
if c.GetAppType() == "" {
continue
func (c *cli) appPickerOptions(requestOpts ...management.RequestOption) pickerOptionsFunc {
requestOpts = append(requestOpts, management.Parameter("is_global", "false"))

return func() (pickerOptions, error) {
clientList, err := c.api.Client.List(requestOpts...)
if err != nil {
return nil, err
}

tenant, err := c.getTenant()
if err != nil {
return nil, err
}

value := c.GetClientID()
label := fmt.Sprintf("%s %s", c.GetName(), ansi.Faint("("+value+")"))
opt := pickerOption{value: value, label: label}
var priorityOpts, opts pickerOptions
for _, client := range clientList.Clients {
value := client.GetClientID()
label := fmt.Sprintf("%s %s", client.GetName(), ansi.Faint("("+value+")"))
option := pickerOption{value: value, label: label}

// check if this is currently the default application.
if tenant.DefaultAppID == c.GetClientID() {
priorityOpts = append(priorityOpts, opt)
} else {
opts = append(opts, opt)
if tenant.DefaultAppID == client.GetClientID() {
priorityOpts = append(priorityOpts, option)
} else {
opts = append(opts, option)
}
}
}

if len(opts)+len(priorityOpts) == 0 {
return nil, errNoApps
}
if len(opts)+len(priorityOpts) == 0 {
return nil, errNoApps
}

return append(priorityOpts, opts...), nil
return append(priorityOpts, opts...), nil
}
}
6 changes: 5 additions & 1 deletion internal/cli/quickstarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,11 @@ func (i *qsInputs) fromArgs(cmd *cobra.Command, args []string, cli *cli) error {
}

if len(args) == 0 {
if err := qsClientID.Pick(cmd, &i.ClientID, cli.appPickerOptions); err != nil {
if err := qsClientID.Pick(
cmd,
&i.ClientID,
cli.appPickerOptions(management.Parameter("app_type", "native,spa,regular_web,non_interactive")),
); err != nil {
return err
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (c *cli) customDomainPickerOptions() (pickerOptions, error) {
}

func (c *cli) appPickerWithCreateOption() (pickerOptions, error) {
options, err := c.appPickerOptions()
options, err := c.appPickerOptions()()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2ba24a6

Please sign in to comment.