-
Notifications
You must be signed in to change notification settings - Fork 33
/
db_show.go
114 lines (97 loc) · 3.36 KB
/
db_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package cmd
import (
"fmt"
"sort"
"strings"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/tursodatabase/turso-cli/internal"
)
func init() {
dbCmd.AddCommand(showCmd)
showCmd.Flags().BoolVar(&showUrlFlag, "url", false, "Show URL for the database HTTP API.")
showCmd.Flags().BoolVar(&showHttpUrlFlag, "http-url", false, "Show HTTP URL for the database HTTP API.")
showCmd.Flags().BoolVar(&showInstanceUrlsFlag, "instance-urls", false, "Show URL for the HTTP API of all existing instances")
showCmd.Flags().StringVar(&showInstanceUrlFlag, "instance-url", "", "Show URL for the HTTP API of a selected instance of a database. Instance is selected by instance name.")
showCmd.RegisterFlagCompletionFunc("instance-url", completeInstanceName)
showCmd.RegisterFlagCompletionFunc("instance-ws-url", completeInstanceName)
}
var showCmd = &cobra.Command{
Use: "show <database-name>",
Short: "Show information from a database.",
Args: cobra.ExactArgs(1),
ValidArgsFunction: dbNameArg,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
client, err := authedTursoClient()
if err != nil {
return err
}
db, err := getDatabase(client, args[0], true)
if err != nil {
return err
}
if showUrlFlag {
fmt.Println(getDatabaseUrl(&db))
return nil
}
if showHttpUrlFlag {
fmt.Println(getDatabaseHttpUrl(&db))
return nil
}
instances, dbUsage, err := instancesAndUsage(client, db.Name)
if err != nil {
return fmt.Errorf("could not get instances of database %s: %w", db.Name, err)
}
if showInstanceUrlFlag != "" {
for _, instance := range instances {
if instance.Name == showInstanceUrlFlag {
fmt.Println(getInstanceUrl(&db, &instance))
return nil
}
}
return fmt.Errorf("instance %s was not found for database %s. List known instances using %s", internal.Emph(showInstanceUrlFlag), internal.Emph(db.Name), internal.Emph("turso db show "+db.Name))
}
regions := make([]string, len(db.Regions))
copy(regions, db.Regions)
sort.Strings(regions)
headers := []string{"Name", "Type", "Location"}
if showInstanceUrlsFlag {
headers = append(headers, "URL")
}
data := [][]string{}
for _, instance := range instances {
row := []string{instance.Name, instance.Type, instance.Region}
if showInstanceUrlsFlag {
url := getInstanceUrl(&db, &instance)
row = append(row, url)
}
data = append(data, row)
}
fmt.Println("Name: ", db.Name)
fmt.Println("URL: ", getDatabaseUrl(&db))
fmt.Println("ID: ", db.ID)
if db.Group != "" {
fmt.Println("Group: ", db.Group)
}
if db.Version != "" {
fmt.Println("Version: ", db.Version)
}
fmt.Println("Locations: ", strings.Join(regions, ", "))
fmt.Println("Size: ", humanize.Bytes(dbUsage.Usage.StorageBytesUsed))
fmt.Println("Sleeping: ", formatBool(db.Sleeping))
fmt.Println("Bytes Synced: ", humanize.Bytes(dbUsage.Usage.BytesSynced))
fmt.Println("Is Schema: ", formatBool(db.IsSchema))
if db.Schema != "" {
fmt.Println("Schema: ", db.Schema)
}
fmt.Println()
if len(instances) == 0 {
fmt.Printf("🛠 Run %s to finish your database creation!\n", internal.Emph("turso db replicate "+db.Name))
return nil
}
fmt.Print("Database Instances:\n")
printTable(headers, data)
return nil
},
}