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

DXCDT-589: Enable description prompt in interactive mode when updating roles #915

Merged
merged 1 commit into from
Nov 21, 2023
Merged
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
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
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to provide no name and no description on update? If so, might we detect that and prevent an API request that will inevitably fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API doesn't fail if we do not provide any name or description, it will simply behave as a GET.

// 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
Loading