|
1 | 1 | package branch
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 |
| - "fmt" |
6 |
| - "net/url" |
7 |
| - |
8 |
| - "github.com/planetscale/cli/internal/cmdutil" |
9 | 4 | "github.com/planetscale/cli/internal/config"
|
10 |
| - "github.com/planetscale/cli/internal/printer" |
11 |
| - ps "github.com/planetscale/planetscale-go/planetscale" |
12 | 5 |
|
13 |
| - "github.com/pkg/browser" |
14 | 6 | "github.com/spf13/cobra"
|
15 | 7 | )
|
16 | 8 |
|
17 | 9 | // BranchCmd handles the branching of a database.
|
18 | 10 | func BranchCmd(cfg *config.Config) *cobra.Command {
|
19 |
| - createReq := &ps.CreateDatabaseBranchRequest{ |
20 |
| - Branch: new(ps.DatabaseBranch), |
21 |
| - } |
22 |
| - |
23 | 11 | cmd := &cobra.Command{
|
24 |
| - Use: "branch <source-database> <branch> [options]", |
25 |
| - Short: "Branch a production database", |
26 |
| - Args: cmdutil.RequiredArgs("source-database", "branch"), |
27 |
| - Aliases: []string{"b"}, |
28 |
| - RunE: func(cmd *cobra.Command, args []string) error { |
29 |
| - ctx := context.Background() |
30 |
| - source := args[0] |
31 |
| - branch := args[1] |
32 |
| - |
33 |
| - // Simplest case, the names are equivalent |
34 |
| - if source == branch { |
35 |
| - return fmt.Errorf("A branch named '%s' already exists", branch) |
36 |
| - } |
37 |
| - |
38 |
| - createReq.Database = source |
39 |
| - createReq.Branch.Name = branch |
40 |
| - createReq.Organization = cfg.Organization |
41 |
| - |
42 |
| - web, err := cmd.Flags().GetBool("web") |
43 |
| - if err != nil { |
44 |
| - return err |
45 |
| - } |
46 |
| - |
47 |
| - if web { |
48 |
| - fmt.Println("🌐 Redirecting you to branch a database in your web browser.") |
49 |
| - err := browser.OpenURL(fmt.Sprintf("%s/%s/%s/branches?name=%s¬es=%s&showDialog=true", cmdutil.ApplicationURL, cfg.Organization, source, url.QueryEscape(createReq.Branch.Name), url.QueryEscape(createReq.Branch.Notes))) |
50 |
| - if err != nil { |
51 |
| - return err |
52 |
| - } |
53 |
| - return nil |
54 |
| - } |
55 |
| - |
56 |
| - client, err := cfg.NewClientFromConfig() |
57 |
| - if err != nil { |
58 |
| - return err |
59 |
| - } |
60 |
| - |
61 |
| - end := cmdutil.PrintProgress(fmt.Sprintf("Creating branch from %s...", cmdutil.BoldBlue(source))) |
62 |
| - defer end() |
63 |
| - dbBranch, err := client.DatabaseBranches.Create(ctx, createReq) |
64 |
| - if err != nil { |
65 |
| - return err |
66 |
| - } |
67 |
| - |
68 |
| - end() |
69 |
| - if cfg.OutputJSON { |
70 |
| - err := printer.PrintJSON(dbBranch) |
71 |
| - if err != nil { |
72 |
| - return err |
73 |
| - } |
74 |
| - } else { |
75 |
| - fmt.Printf("Branch %s was successfully created!\n", cmdutil.BoldBlue(dbBranch.Name)) |
76 |
| - } |
77 |
| - |
78 |
| - return nil |
79 |
| - }, |
| 12 | + Use: "branch <command>", |
| 13 | + Short: "Create, delete, and manage branches", |
80 | 14 | }
|
81 | 15 |
|
82 |
| - cmd.Flags().StringVar(&createReq.Branch.Notes, "notes", "", "notes for the database branch") |
83 |
| - cmd.Flags().StringVar(&createReq.Branch.ParentBranch, "from", "", "branch to be created from") |
84 |
| - cmd.Flags().BoolP("web", "w", false, "Create a branch in your web browser") |
85 |
| - |
86 |
| - cmd.PersistentFlags().StringVar(&cfg.Organization, "org", cfg.Organization, "The organization for the current user") |
| 16 | + cmd.PersistentFlags().StringVar(&cfg.Organization, "org", cfg.Organization, |
| 17 | + "The organization for the current user") |
87 | 18 | cmd.MarkPersistentFlagRequired("org") // nolint:errcheck
|
88 | 19 |
|
| 20 | + cmd.AddCommand(CreateCmd(cfg)) |
89 | 21 | cmd.AddCommand(ListCmd(cfg))
|
90 | 22 | cmd.AddCommand(StatusCmd(cfg))
|
91 | 23 | cmd.AddCommand(DeleteCmd(cfg))
|
|
0 commit comments