|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/spf13/cobra" |
| 5 | + |
| 6 | + "github.com/render-oss/cli/pkg/client" |
| 7 | + "github.com/render-oss/cli/pkg/command" |
| 8 | + "github.com/render-oss/cli/pkg/dependencies" |
| 9 | + "github.com/render-oss/cli/pkg/postgres" |
| 10 | + "github.com/render-oss/cli/pkg/text" |
| 11 | + "github.com/render-oss/cli/pkg/types" |
| 12 | +) |
| 13 | + |
| 14 | +type pgDeleteInput struct { |
| 15 | + IDOrName string `cli:"arg:0"` |
| 16 | + ProjectIDOrName *string `cli:"project"` |
| 17 | + EnvironmentIDOrName *string `cli:"environment"` |
| 18 | +} |
| 19 | + |
| 20 | +func normalizePGDeleteInput(input pgDeleteInput) pgDeleteInput { |
| 21 | + input.ProjectIDOrName = types.OptionalNonZeroString(input.ProjectIDOrName) |
| 22 | + input.EnvironmentIDOrName = types.OptionalNonZeroString(input.EnvironmentIDOrName) |
| 23 | + return input |
| 24 | +} |
| 25 | + |
| 26 | +type pgDeleteResult struct { |
| 27 | + Postgres *client.PostgresDetail `json:"postgres"` |
| 28 | + Deleted bool `json:"deleted"` |
| 29 | +} |
| 30 | + |
| 31 | +func newPgDeleteCmd(deps *dependencies.Dependencies) *cobra.Command { |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "delete <postgresID|postgresName>", |
| 34 | + Short: "Delete a Postgres database", |
| 35 | + Args: cobra.ExactArgs(1), |
| 36 | + SilenceUsage: true, |
| 37 | + Long: `Delete a Postgres database on Render. |
| 38 | +
|
| 39 | +Without --confirm, this command previews what would be deleted and makes no |
| 40 | +changes. Pass --confirm to actually delete the database. |
| 41 | +
|
| 42 | +The positional argument accepts either a Postgres ID (dpg-...) or a name. |
| 43 | +If the name matches more than one database, narrow the search with |
| 44 | +--project <id|name>, --environment <id|name>, or pass the Postgres ID directly. |
| 45 | +
|
| 46 | +Name lookup is scoped to your active workspace. If a name isn't found, switch |
| 47 | +workspaces with 'render workspace set <name|ID>' and try again, or pass the |
| 48 | +Postgres ID instead (which works across workspaces).`, |
| 49 | + Example: ` # Preview deletion (no changes made) |
| 50 | + render ea pg delete dpg-abc123def456ghi789jkl0 |
| 51 | +
|
| 52 | + # Delete by ID |
| 53 | + render ea pg delete dpg-abc123def456ghi789jkl0 --confirm |
| 54 | +
|
| 55 | + # Delete by name |
| 56 | + render ea pg delete my-db --confirm |
| 57 | +
|
| 58 | + # Disambiguate a name that exists in multiple environments |
| 59 | + render ea pg delete my-db --environment production --confirm |
| 60 | +
|
| 61 | + # Disambiguate a name that exists in multiple projects |
| 62 | + render ea pg delete my-db --project analytics --confirm |
| 63 | +
|
| 64 | + # JSON output |
| 65 | + render ea pg delete dpg-abc123def456ghi789jkl0 --confirm --output json`, |
| 66 | + } |
| 67 | + |
| 68 | + cmd.Flags().String("project", "", |
| 69 | + "Project ID or name (optional). Narrows name lookup when the same Postgres database name exists in multiple projects.") |
| 70 | + cmd.Flags().String("environment", "", |
| 71 | + "Environment ID or name (optional). Narrows name lookup when the same Postgres database name exists in multiple environments.") |
| 72 | + |
| 73 | + cmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 74 | + command.DefaultFormatNonInteractive(cmd) |
| 75 | + |
| 76 | + var input pgDeleteInput |
| 77 | + if err := command.ParseCommand(cmd, args, &input); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + input = normalizePGDeleteInput(input) |
| 81 | + confirm := command.GetConfirmFromContext(cmd.Context()) |
| 82 | + |
| 83 | + loadData := func() (*pgDeleteResult, error) { |
| 84 | + pg, err := deps.PostgresService().Resolve(cmd.Context(), postgres.ResolveInput{ |
| 85 | + IDOrName: input.IDOrName, |
| 86 | + ProjectIDOrName: input.ProjectIDOrName, |
| 87 | + EnvironmentIDOrName: input.EnvironmentIDOrName, |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + if confirm { |
| 93 | + if err := deps.PostgresService().Delete(cmd.Context(), pg.Id); err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + } |
| 97 | + return &pgDeleteResult{Postgres: pg, Deleted: confirm}, nil |
| 98 | + } |
| 99 | + |
| 100 | + _, err := command.NonInteractive(cmd, |
| 101 | + loadData, |
| 102 | + pgDeleteTextOutput, |
| 103 | + ) |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + return cmd |
| 108 | +} |
| 109 | + |
| 110 | +func pgDeleteTextOutput(r *pgDeleteResult) string { |
| 111 | + if r.Deleted { |
| 112 | + return "Deleted this Postgres database:\n\n" + text.PostgresDetail(r.Postgres) + "\n" |
| 113 | + } |
| 114 | + return "This command would delete this Postgres database:\n\n" + |
| 115 | + text.PostgresDetail(r.Postgres) + |
| 116 | + "\n\nRe-run with --confirm to proceed\n" |
| 117 | +} |
0 commit comments