-
Notifications
You must be signed in to change notification settings - Fork 52
/
show.go
69 lines (57 loc) · 1.83 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package branch
import (
"fmt"
"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"
"github.com/planetscale/planetscale-go/planetscale"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
func ShowCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "show <source-database> <branch>",
Short: "Show a specific branch of a database",
Args: cmdutil.RequiredArgs("source-database", "branch"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
source := args[0]
branch := args[1]
web, err := cmd.Flags().GetBool("web")
if err != nil {
return err
}
if web {
ch.Printer.Println("🌐 Redirecting you to your database branch in your web browser.")
err := browser.OpenURL(fmt.Sprintf("%s/%s/%s/branches/%s", cmdutil.ApplicationURL, ch.Config.Organization, source, branch))
if err != nil {
return err
}
return nil
}
client, err := ch.Client()
if err != nil {
return err
}
end := ch.Printer.PrintProgress(fmt.Sprintf("Fetching branch %s for %s", printer.BoldBlue(branch), printer.BoldBlue(source)))
defer end()
b, err := client.DatabaseBranches.Get(ctx, &planetscale.GetDatabaseBranchRequest{
Organization: ch.Config.Organization,
Database: source,
Branch: branch,
})
if err != nil {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("branch %s does not exist in database %s (organization: %s)",
printer.BoldBlue(branch), printer.BoldBlue(source), printer.BoldBlue(ch.Config.Organization))
default:
return cmdutil.HandleError(err)
}
}
end()
return ch.Printer.PrintResource(ToDatabaseBranch(b))
},
}
cmd.Flags().BoolP("web", "w", false, "Show a database branch in your web browser.")
return cmd
}