Skip to content

Commit

Permalink
Enable description prompt in interactive mode when updating roles
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Nov 17, 2023
1 parent ff7566f commit e04cabe
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions internal/cli/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ var (
IsRequired: true,
}
roleDescription = Flag{
Name: "Description",
LongForm: "description",
ShortForm: "d",
Help: "Description of the role.",
Name: "Description",
LongForm: "description",
ShortForm: "d",
Help: "Description of the role.",
IsRequired: true,
}
roleNumber = Flag{
Name: "Number",
Expand Down Expand Up @@ -229,46 +230,46 @@ func updateRoleCmd(cli *cli) *cobra.Command {
auth0 roles update <role-id> -n myrole -d "awesome role" --json`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
err := roleID.Pick(cmd, &inputs.ID, cli.rolePickerOptions)
if err != nil {
if err := roleID.Pick(cmd, &inputs.ID, cli.rolePickerOptions); err != nil {
return err
}
} else {
inputs.ID = args[0]
}

// Prompt for role name
if err := roleName.AskU(cmd, &inputs.Name, nil); err != nil {
var currentRole *management.Role
if err := ansi.Waiting(func() (err error) {
currentRole, err = cli.api.Role.Read(cmd.Context(), inputs.ID)
return err
}); err != nil {
return fmt.Errorf("failed to find role with ID %q: %v", inputs.ID, err)
}

// Prompt for role description
if err := roleDescription.AskU(cmd, &inputs.Description, nil); err != nil {
if err := roleName.AskU(cmd, &inputs.Name, currentRole.Name); err != nil {
return err
}

// Start with an empty role object. We'll conditionally
// hydrate it based on the provided parameters since
// we'll do PATCH semantics.
r := &management.Role{}
if err := roleDescription.AskU(cmd, &inputs.Description, currentRole.Description); err != nil {
return err
}

updatedRole := &management.Role{}

if inputs.Name != "" {
r.Name = &inputs.Name
updatedRole.Name = &inputs.Name
}

if inputs.Description != "" {
r.Description = &inputs.Description
updatedRole.Description = &inputs.Description
}

// Update role
if err := ansi.Waiting(func() error {
return cli.api.Role.Update(cmd.Context(), inputs.ID, r)
return cli.api.Role.Update(cmd.Context(), inputs.ID, updatedRole)
}); err != nil {
return fmt.Errorf("Unable to update role: %v", err)
return fmt.Errorf("failed to update role with ID %q: %v", inputs.ID, err)
}

// Render role creation specific view
cli.renderer.RoleUpdate(r)
cli.renderer.RoleUpdate(updatedRole)

return nil
},
}
Expand Down

0 comments on commit e04cabe

Please sign in to comment.