Skip to content

Commit 238e876

Browse files
notrabAthos
andauthored
refactor db list options (#901)
* refactor db list options * Update internal/turso/databases.go Co-authored-by: Athos <athos@turso.tech> * Update internal/turso/databases.go Co-authored-by: Athos <athos@turso.tech> * move client up --------- Co-authored-by: Athos <athos@turso.tech>
1 parent 4c3142f commit 238e876

4 files changed

Lines changed: 23 additions & 17 deletions

File tree

internal/cmd/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/tursodatabase/turso-cli/internal"
1818
"github.com/tursodatabase/turso-cli/internal/flags"
1919
"github.com/tursodatabase/turso-cli/internal/settings"
20+
"github.com/tursodatabase/turso-cli/internal/turso"
2021
)
2122

2223
//go:embed login.html
@@ -275,7 +276,7 @@ func signupHint(config *settings.Settings) {
275276
return
276277
}
277278

278-
dbs, err := client.Databases.List(nil)
279+
dbs, err := client.Databases.List(turso.DatabaseListOptions{})
279280
if err != nil || len(dbs) != 0 {
280281
return
281282
}

internal/cmd/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func getDatabases(client *turso.Client, fresh ...bool) ([]turso.Database, error)
5959
if cachedNames := getDatabasesCache(); !skipCache && cachedNames != nil {
6060
return cachedNames, nil
6161
}
62-
databases, err := client.Databases.List(nil)
62+
databases, err := client.Databases.List(turso.DatabaseListOptions{})
6363
if err != nil {
6464
return nil, err
6565
}

internal/cmd/db_list.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ var listCmd = &cobra.Command{
2929
return err
3030
}
3131

32-
options := make(map[string]string)
33-
if groupFilter != "" {
34-
options["group"] = groupFilter
32+
options := turso.DatabaseListOptions{
33+
Group: groupFilter,
34+
Schema: schemaFilter,
3535
}
36-
if schemaFilter != "" {
37-
options["schema"] = schemaFilter
38-
}
39-
4036
databases, err := client.Databases.List(options)
4137
if err != nil {
4238
return err

internal/turso/databases.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,27 @@ type Database struct {
2727

2828
type DatabasesClient client
2929

30-
func (d *DatabasesClient) List(options map[string]string) ([]Database, error) {
31-
path := d.URL("")
30+
type DatabaseListOptions struct {
31+
Group string
32+
Schema string
33+
}
3234

35+
func (o DatabaseListOptions) Encode() string {
3336
query := url.Values{}
34-
for key, value := range options {
35-
if value != "" {
36-
query.Add(key, value)
37-
}
37+
if o.Group != "" {
38+
query.Set("group", o.Group)
39+
}
40+
if o.Schema != "" {
41+
query.Set("schema", o.Schema)
3842
}
43+
return query.Encode()
44+
}
45+
46+
func (d *DatabasesClient) List(options DatabaseListOptions) ([]Database, error) {
47+
path := d.URL("")
3948

40-
if len(query) > 0 {
41-
path += "?" + query.Encode()
49+
if options := options.Encode(); options != "" {
50+
path += "?" + options
4251
}
4352

4453
r, err := d.client.Get(path, nil)

0 commit comments

Comments
 (0)