Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/tursodatabase/turso-cli/internal"
Expand Down Expand Up @@ -66,9 +67,22 @@ func getDatabase(client *turso.Client, name string, fresh ...bool) (turso.Databa
}
}

if suggestion := suggestDatabaseName(name, databases); suggestion != "" {
return turso.Database{}, fmt.Errorf("database %s not found. Did you mean %s? List known databases using %s", internal.Emph(name), internal.Emph(suggestion), internal.Emph("turso db list"))
}
return turso.Database{}, fmt.Errorf("database %s not found. List known databases using %s", internal.Emph(name), internal.Emph("turso db list"))
}

func suggestDatabaseName(name string, databases []turso.Database) string {
for _, database := range databases {
hostPrefix, _, _ := strings.Cut(database.Hostname, ".")
if hostPrefix == name {
return database.Name
}
}
return ""
}

func listDatabases(client *turso.Client) ([]turso.Database, error) {
if !flags.V3Api() {
return listDatabasesV2(client)
Expand Down
39 changes: 39 additions & 0 deletions internal/cmd/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"testing"

"github.com/tursodatabase/turso-cli/internal/turso"
)

func TestSuggestDatabaseNameFromHostnamePrefix(t *testing.T) {
databases := []turso.Database{
{
Name: "hello",
Hostname: "hello-penberg.turso.io",
},
{
Name: "other",
Hostname: "other-penberg.turso.io",
},
}

got := suggestDatabaseName("hello-penberg", databases)
if got != "hello" {
t.Fatalf("suggestDatabaseName() = %q, want %q", got, "hello")
}
}

func TestSuggestDatabaseNameIgnoresUnmatchedName(t *testing.T) {
databases := []turso.Database{
{
Name: "hello",
Hostname: "hello-penberg.turso.io",
},
}

got := suggestDatabaseName("missing", databases)
if got != "" {
t.Fatalf("suggestDatabaseName() = %q, want empty string", got)
}
}