-
Notifications
You must be signed in to change notification settings - Fork 52
/
list.go
83 lines (68 loc) · 2.13 KB
/
list.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package database
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"
)
// ListCmd is the command for listing all databases for an authenticated user.
func ListCmd(ch *cmdutil.Helper) *cobra.Command {
var flags struct {
page int
perPage int
}
cmd := &cobra.Command{
Use: "list",
Short: "List databases",
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
web, err := cmd.Flags().GetBool("web")
if err != nil {
return err
}
if web {
ch.Printer.Println("🌐 Redirecting you to your databases list in your web browser.")
err := browser.OpenURL(fmt.Sprintf("%s/%s", cmdutil.ApplicationURL, ch.Config.Organization))
if err != nil {
return err
}
return nil
}
client, err := ch.Client()
if err != nil {
return err
}
end := ch.Printer.PrintProgress("Fetching databases...")
defer end()
databases, err := client.Databases.List(ctx, &planetscale.ListDatabasesRequest{
Organization: ch.Config.Organization,
}, planetscale.WithPage(flags.page), planetscale.WithPerPage(flags.perPage))
if err != nil {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("organization %s does not exist or your account is not authorized to access it", printer.BoldBlue(ch.Config.Organization))
default:
return cmdutil.HandleError(err)
}
}
end()
if len(databases) == 0 && ch.Printer.Format() == printer.Human {
if flags.page == 0 {
ch.Printer.Println("No databases have been created yet.")
} else {
ch.Printer.Println("No databases found on this page.")
}
return nil
}
return ch.Printer.PrintResource(toDatabases(databases))
},
TraverseChildren: true,
}
cmd.Flags().BoolP("web", "w", false, "Open in your web browser")
cmd.Flags().IntVar(&flags.page, "page", 0, "Page number to fetch")
cmd.Flags().IntVar(&flags.perPage, "per-page", 100, "Number of results per page")
return cmd
}