Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed API fetching logic in roles permissions add/rm #357

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 14 additions & 14 deletions internal/cli/roles_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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())
}

Expand All @@ -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 {
Expand Down