Skip to content

Commit

Permalink
Add promote command.
Browse files Browse the repository at this point in the history
  • Loading branch information
iheanyi committed Aug 11, 2021
1 parent 7d47ec2 commit fc76d85
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/cmd/branch/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func BranchCmd(ch *cmdutil.Helper) *cobra.Command {
cmd.AddCommand(DiffCmd(ch))
cmd.AddCommand(SchemaCmd(ch))
cmd.AddCommand(RefreshSchemaCmd(ch))
cmd.AddCommand(PromoteCmd(ch))

return cmd
}
Expand Down
98 changes: 98 additions & 0 deletions internal/cmd/branch/promote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package branch

import (
"fmt"

"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"
"github.com/spf13/cobra"

ps "github.com/planetscale/planetscale-go/planetscale"
)

func PromoteCmd(ch *cmdutil.Helper) *cobra.Command {
promoteReq := &ps.PromoteBranchRequest{}

cmd := &cobra.Command{
Use: "promote <database> <branch> [options]",
Short: "Promote a new branch from a database",
Args: cmdutil.RequiredArgs("source-database", "branch"),
Aliases: []string{"b"},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

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

org := ch.Config.Organization // --org flag
if org == "" {
cfg, err := ch.ConfigFS.DefaultConfig()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

org = cfg.Organization
}

databases, err := client.Databases.List(cmd.Context(), &ps.ListDatabasesRequest{
Organization: org,
})
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

candidates := make([]string, 0, len(databases))
for _, db := range databases {
candidates = append(candidates, db.Name)
}

return candidates, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
source := args[0]
branch := args[1]

// Simplest case, the names are equivalent
if source == branch {
return fmt.Errorf("a branch named '%s' already exists", branch)
}

promoteReq.Database = source
promoteReq.Organization = ch.Config.Organization
promoteReq.Branch = branch

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

end := ch.Printer.PrintProgress(fmt.Sprintf("Creating branch from %s...", printer.BoldBlue(source)))
defer end()
dbBranch, err := client.DatabaseBranches.Promote(cmd.Context(), promoteReq)
if err != nil {
switch cmdutil.ErrCode(err) {
case ps.ErrNotFound:
return fmt.Errorf("source database %s does not exist in organization %s",
printer.BoldBlue(source), printer.BoldBlue(ch.Config.Organization))
default:
return cmdutil.HandleError(err)
}
}

end()

if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Branch %s was successfully promoted.\n", printer.BoldBlue(dbBranch.Name))
return nil
}

return ch.Printer.PrintResource(toDatabaseBranch(dbBranch))
},
}

return cmd
}

0 comments on commit fc76d85

Please sign in to comment.