|
| 1 | +package role |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/AlecAivazis/survey/v2" |
| 9 | + "github.com/AlecAivazis/survey/v2/terminal" |
| 10 | + "github.com/planetscale/cli/internal/cmdutil" |
| 11 | + "github.com/planetscale/cli/internal/printer" |
| 12 | + ps "github.com/planetscale/planetscale-go/planetscale" |
| 13 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +func ReassignCmd(ch *cmdutil.Helper) *cobra.Command { |
| 18 | + var flags struct { |
| 19 | + force bool |
| 20 | + successor string |
| 21 | + } |
| 22 | + |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "reassign <database> <branch> <role-id>", |
| 25 | + Short: "Reassign objects owned by a role to another role", |
| 26 | + Args: cmdutil.RequiredArgs("database", "branch", "role-id"), |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + ctx := cmd.Context() |
| 29 | + database := args[0] |
| 30 | + branch := args[1] |
| 31 | + roleID := args[2] |
| 32 | + |
| 33 | + if flags.successor == "" { |
| 34 | + return fmt.Errorf("--successor flag is required") |
| 35 | + } |
| 36 | + |
| 37 | + client, err := ch.Client() |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + if !flags.force { |
| 43 | + if ch.Printer.Format() != printer.Human { |
| 44 | + return fmt.Errorf("cannot reassign role objects with the output format %q (run with --force to override)", ch.Printer.Format()) |
| 45 | + } |
| 46 | + |
| 47 | + confirmationName := fmt.Sprintf("%s/%s/%s", database, branch, roleID) |
| 48 | + if !printer.IsTTY { |
| 49 | + return fmt.Errorf("cannot confirm object reassignment for role %q (run with --force to override)", confirmationName) |
| 50 | + } |
| 51 | + |
| 52 | + confirmationMessage := fmt.Sprintf("%s %s %s", printer.Bold("Please type"), |
| 53 | + printer.BoldBlue(confirmationName), printer.Bold("to confirm:")) |
| 54 | + |
| 55 | + prompt := &survey.Input{ |
| 56 | + Message: confirmationMessage, |
| 57 | + } |
| 58 | + |
| 59 | + var userInput string |
| 60 | + err := survey.AskOne(prompt, &userInput) |
| 61 | + if err != nil { |
| 62 | + if err == terminal.InterruptErr { |
| 63 | + os.Exit(0) |
| 64 | + } else { |
| 65 | + return err |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // If the confirmations don't match up, let's return an error. |
| 70 | + if userInput != confirmationName { |
| 71 | + return errors.New("incorrect role identifier entered, skipping object reassignment") |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + end := ch.Printer.PrintProgress(fmt.Sprintf("Reassigning objects from role %s to %s in %s/%s...", |
| 76 | + printer.BoldBlue(roleID), printer.BoldBlue(flags.successor), printer.BoldBlue(database), printer.BoldBlue(branch))) |
| 77 | + defer end() |
| 78 | + |
| 79 | + err = client.PostgresRoles.ReassignObjects(ctx, &ps.ReassignPostgresRoleObjectsRequest{ |
| 80 | + Organization: ch.Config.Organization, |
| 81 | + Database: database, |
| 82 | + Branch: branch, |
| 83 | + RoleId: roleID, |
| 84 | + Successor: flags.successor, |
| 85 | + }) |
| 86 | + if err != nil { |
| 87 | + switch cmdutil.ErrCode(err) { |
| 88 | + case ps.ErrNotFound: |
| 89 | + return fmt.Errorf("role %s does not exist in branch %s of database %s (organization: %s)", |
| 90 | + printer.BoldBlue(roleID), printer.BoldBlue(branch), printer.BoldBlue(database), printer.BoldBlue(ch.Config.Organization)) |
| 91 | + default: |
| 92 | + return cmdutil.HandleError(err) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + end() |
| 97 | + |
| 98 | + if ch.Printer.Format() == printer.Human { |
| 99 | + ch.Printer.Printf("Objects owned by role %s were successfully reassigned to %s in %s/%s.\n", |
| 100 | + printer.BoldBlue(roleID), printer.BoldBlue(flags.successor), printer.BoldBlue(database), printer.BoldBlue(branch)) |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + return ch.Printer.PrintResource( |
| 105 | + map[string]string{ |
| 106 | + "result": "objects reassigned", |
| 107 | + "role_id": roleID, |
| 108 | + "successor": flags.successor, |
| 109 | + "branch": branch, |
| 110 | + }, |
| 111 | + ) |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + cmd.Flags().BoolVar(&flags.force, "force", false, "Reassign objects without confirmation") |
| 116 | + cmd.Flags().StringVar(&flags.successor, "successor", "", "Role to transfer ownership to (required)") |
| 117 | + cmd.MarkFlagRequired("successor") // nolint:errcheck |
| 118 | + |
| 119 | + return cmd |
| 120 | +} |
0 commit comments