From cbcdc4fa1e230677ebf3665def1a598f0c2f177c Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Tue, 21 Sep 2021 17:02:06 -0300 Subject: [PATCH] Fix API fetching logic in roles permissions add/rm --- internal/cli/flags.go | 47 +++++++++++++++++++------------ internal/cli/roles_permissions.go | 28 +++++++++--------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/internal/cli/flags.go b/internal/cli/flags.go index 3836e18f7..8597340f4 100644 --- a/internal/cli/flags.go +++ b/internal/cli/flags.go @@ -70,25 +70,11 @@ func (f *Flag) SelectU(cmd *cobra.Command, value interface{}, options []string, } func (f *Flag) Pick(cmd *cobra.Command, result *string, fn pickerOptionsFunc) error { - var opts pickerOptions - err := ansi.Waiting(func() error { - var err error - opts, err = fn() - return err - }) - - if err != nil { - return err - } - - defaultLabel := opts.defaultLabel() - var val string - if err := selectFlag(cmd, f, &val, opts.labels(), &defaultLabel, false); err != nil { - return err - } + return pickFlag(cmd, f, result, fn, false) +} - *result = opts.getValue(val) - return nil +func (f *Flag) PickU(cmd *cobra.Command, result *string, fn pickerOptionsFunc) error { + return pickFlag(cmd, f, result, fn, true) } func (f *Flag) OpenEditor(cmd *cobra.Command, value *string, defaultValue, filename string, infoFn func()) error { @@ -191,6 +177,31 @@ func selectFlag(cmd *cobra.Command, f *Flag, value interface{}, options []string return nil } +func pickFlag(cmd *cobra.Command, f *Flag, result *string, fn pickerOptionsFunc, isUpdate bool) error { + if shouldAsk(cmd, f, isUpdate) { + var opts pickerOptions + err := ansi.Waiting(func() error { + var err error + opts, err = fn() + return err + }) + + if err != nil { + return err + } + + defaultLabel := opts.defaultLabel() + var val string + if err := selectFlag(cmd, f, &val, opts.labels(), &defaultLabel, isUpdate); err != nil { + return err + } + + *result = opts.getValue(val) + } + + return nil +} + func askPasswordFlag(cmd *cobra.Command, f *Flag, value *string, defaultValue *string, isUpdate bool) error { if shouldAsk(cmd, f, isUpdate) { if err := askPassword(cmd, f, value, defaultValue, isUpdate); err != nil { diff --git a/internal/cli/roles_permissions.go b/internal/cli/roles_permissions.go index 1c8f3fb5e..dfc84850f 100644 --- a/internal/cli/roles_permissions.go +++ b/internal/cli/roles_permissions.go @@ -118,10 +118,13 @@ auth0 roles permissions add`, } var rs *management.ResourceServer + rs, err := cli.api.ResourceServer.Read(url.PathEscape(inputs.APIIdentifier)) + if err != nil { + return err + } if len(inputs.Permissions) == 0 { - var err error - rs, err = cli.pickRolePermissions(inputs.APIIdentifier, &inputs.Permissions) + err := cli.pickRolePermissions(rs.Scopes, &inputs.Permissions) if err != nil { return err } @@ -180,10 +183,13 @@ auth0 roles permissions rm`, } var rs *management.ResourceServer + rs, err := cli.api.ResourceServer.Read(url.PathEscape(inputs.APIIdentifier)) + if err != nil { + return err + } if len(inputs.Permissions) == 0 { - var err error - rs, err = cli.pickRolePermissions(inputs.APIIdentifier, &inputs.Permissions) + err := cli.pickRolePermissions( rs.Scopes, &inputs.Permissions) if err != nil { return err } @@ -230,17 +236,11 @@ func (c *cli) apiPickerOptionsWithoutAuth0() (pickerOptions, error) { }) } -func (c *cli) pickRolePermissions(id string, permissions *[]string) (*management.ResourceServer, error) { +func (c *cli) pickRolePermissions(apiScopes []*management.ResourceServerScope, permissions *[]string) error { // NOTE(cyx): We're inlining this for now since we have no generic // usecase for this particular picker type yet. - var err error - rs, err := c.api.ResourceServer.Read(id) - if err != nil { - return nil, err - } - var options []string - for _, s := range rs.Scopes { + for _, s := range apiScopes { options = append(options, s.GetValue()) } @@ -250,10 +250,10 @@ func (c *cli) pickRolePermissions(id string, permissions *[]string) (*management } if err := survey.AskOne(p, permissions); err != nil { - return nil, err + return err } - return rs, nil + return nil } func makePermissions(id string, permissions []string) []*management.Permission {