Skip to content

Commit

Permalink
Adapt turso db list for database groups
Browse files Browse the repository at this point in the history
- Only print locations if there are non-group DBs
- Add group column. Print it if:
  - There are DBs in more than group
  - There is at least one group and non-group DBs
  • Loading branch information
athoscouto committed Sep 19, 2023
1 parent 6d398b8 commit bae4320
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 23 deletions.
92 changes: 69 additions & 23 deletions internal/cmd/db_list.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cmd

import (
"fmt"
"sort"

"github.com/chiselstrike/iku-turso-cli/internal"
"github.com/chiselstrike/iku-turso-cli/internal/turso"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

func init() {
Expand All @@ -23,34 +23,80 @@ var listCmd = &cobra.Command{
if err != nil {
return err
}

databases, err := client.Databases.List()
if err != nil {
return err
}
setDatabasesCache(databases)
var data [][]string
var helps []string
for _, database := range databases {
row := []string{database.Name, getDatabaseLocations(database), getDatabaseUrl(&database)}
if len(database.Regions) == 0 {
help := fmt.Sprintf("🛠 Run %s to finish your database creation!", internal.Emph("turso db replicate "+database.Name))
helps = append(helps, help)
}
data = append(data, row)
}

sort.Slice(data, func(i, j int) bool {
return data[i][0] > data[j][0]
})
printDBListTable(databases)
return nil
},
}

printTable([]string{"Name", "Locations", "URL"}, data)
func printDBListTable(databases []turso.Database) {
headers, data := dbListTable(databases)
if !shouldPrintLocations(databases) {
headers, data = removeColumn(headers, data, "Locations")
}
if !shouldPrintGroups(databases) {
headers, data = removeColumn(headers, data, "Group")
}
printTable(headers, data)
}

if len(helps) > 0 {
fmt.Println()
for _, help := range helps {
fmt.Println(help)
}
func shouldPrintLocations(databases []turso.Database) bool {
for _, database := range databases {
if database.Group == "" {
return true
}
return nil
},
}
return false
}

func shouldPrintGroups(databases []turso.Database) bool {
mp := map[string]bool{}
for _, database := range databases {
mp[database.Group] = true
}
return len(mp) > 1
}

func dbListTable(databases []turso.Database) (headers []string, data [][]string) {
for _, database := range databases {
row := []string{database.Name, getDatabaseLocations(database), formatGroup(database.Group), getDatabaseUrl(&database)}
data = append(data, row)
}

sort.Slice(data, func(i, j int) bool {
return data[i][0] > data[j][0]
})

return []string{"Name", "Locations", "Group", "URL"}, data
}

func removeColumn(headers []string, data [][]string, column string) ([]string, [][]string) {
i := slices.Index(headers, column)
if i == -1 {
return headers, data
}

filtered := make([][]string, 0, len(data))
for _, row := range data {
filtered = append(filtered, removeIndex(row, i))
}

return removeIndex(headers, i), filtered
}

func removeIndex(arr []string, i int) []string {
return append(arr[:i], arr[i+1:]...)
}

func formatGroup(group string) string {
if group == "" {
return "-"
}
return group
}
1 change: 1 addition & 0 deletions internal/turso/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Database struct {
PrimaryRegion string
Hostname string
Version string
Group string
}

type DatabasesClient client
Expand Down

0 comments on commit bae4320

Please sign in to comment.