Skip to content

Commit

Permalink
internal/cmd/branch: clean up demote
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Layher <mdlayher@planetscale.com>
  • Loading branch information
mdlayher committed Dec 22, 2023
1 parent 4fc8b05 commit 4ae1625
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions internal/cmd/branch/demote.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,32 @@ import (
)

func DemoteCmd(ch *cmdutil.Helper) *cobra.Command {
demoteReq := &ps.DemoteRequest{}

cmd := &cobra.Command{
return &cobra.Command{
Use: "demote <database> <branch> [options]",
Short: "Demote a production branch to development",
Args: cmdutil.RequiredArgs("database", "branch"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

database := args[0]
branch := args[1]

demoteReq.Database = database
demoteReq.Branch = branch
demoteReq.Organization = ch.Config.Organization
req := &ps.DemoteRequest{
Database: args[0],
Branch: args[1],
Organization: ch.Config.Organization,
}

client, err := ch.Client()
if err != nil {
return err
}

end := ch.Printer.PrintProgress(fmt.Sprintf("Demoting %s branch in %s to development...", printer.BoldBlue(branch), printer.BoldBlue(database)))
end := ch.Printer.PrintProgress(fmt.Sprintf("Demoting %s branch in %s to development...",
printer.BoldBlue(req.Branch), printer.BoldBlue(req.Database)))
defer end()

b, err := client.DatabaseBranches.Demote(ctx, demoteReq)
b, err := client.DatabaseBranches.Demote(cmd.Context(), req)
if err != nil {
switch cmdutil.ErrCode(err) {
case ps.ErrNotFound:
return fmt.Errorf("branch %s does not exist in database %s",
printer.BoldBlue(branch), printer.BoldBlue(database))
printer.BoldBlue(req.Branch), printer.BoldBlue(req.Database))
default:
return cmdutil.HandleError(err)
}
Expand All @@ -49,16 +45,11 @@ func DemoteCmd(ch *cmdutil.Helper) *cobra.Command {
end()

if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("%s branch was successfully demoted to development.\n", printer.BoldBlue(branch))
ch.Printer.Printf("%s branch was successfully demoted to development.\n", printer.BoldBlue(req.Branch))
return nil
} else {
if err != nil {
return cmdutil.HandleError(err)
}
return ch.Printer.PrintResource(ToDatabaseBranch(b))
}

return ch.Printer.PrintResource(ToDatabaseBranch(b))
},
}

return cmd
}

0 comments on commit 4ae1625

Please sign in to comment.