|
1 | 1 | import type { SourceConfig } from "../types/config.js"; |
| 2 | +import { parseConnectionInfoFromDSN, getDefaultPortForType } from "./dsn-obfuscate.js"; |
2 | 3 |
|
3 | 4 | /** |
4 | 5 | * Information about a source and its tools for display |
@@ -32,24 +33,28 @@ const BOX = { |
32 | 33 | * Parse host and database from source config |
33 | 34 | */ |
34 | 35 | function parseHostAndDatabase(source: SourceConfig): { host: string; database: string } { |
35 | | - // If DSN is provided, parse it |
| 36 | + // If DSN is provided, use the proper DSN parser |
36 | 37 | if (source.dsn) { |
37 | | - try { |
38 | | - const url = new URL(source.dsn); |
39 | | - const host = url.port ? `${url.hostname}:${url.port}` : url.hostname; |
40 | | - const database = url.pathname.replace(/^\//, "") || ""; |
41 | | - return { host, database }; |
42 | | - } catch { |
43 | | - return { host: "unknown", database: "" }; |
| 38 | + const parsed = parseConnectionInfoFromDSN(source.dsn); |
| 39 | + if (parsed) { |
| 40 | + // For SQLite, there's no host - just show the database path |
| 41 | + if (parsed.type === "sqlite") { |
| 42 | + return { host: "", database: parsed.database || ":memory:" }; |
| 43 | + } |
| 44 | + // For other databases, construct host:port string |
| 45 | + const port = parsed.port || getDefaultPortForType(parsed.type!); |
| 46 | + const host = port ? `${parsed.host}:${port}` : parsed.host || ""; |
| 47 | + return { host, database: parsed.database || "" }; |
44 | 48 | } |
| 49 | + return { host: "unknown", database: "" }; |
45 | 50 | } |
46 | 51 |
|
47 | 52 | // Otherwise use individual connection params |
48 | 53 | const host = source.host |
49 | 54 | ? source.port |
50 | 55 | ? `${source.host}:${source.port}` |
51 | 56 | : source.host |
52 | | - : "localhost"; |
| 57 | + : ""; |
53 | 58 | const database = source.database || ""; |
54 | 59 |
|
55 | 60 | return { host, database }; |
@@ -88,7 +93,9 @@ export function generateStartupTable(sources: SourceDisplayInfo[]): string { |
88 | 93 | const hostDbWidth = Math.max( |
89 | 94 | 24, |
90 | 95 | ...sources.map((s) => { |
91 | | - const hostDb = s.database ? `${s.host}/${s.database}` : s.host; |
| 96 | + const hostDb = s.host |
| 97 | + ? s.database ? `${s.host}/${s.database}` : s.host |
| 98 | + : s.database || ""; |
92 | 99 | return hostDb.length; |
93 | 100 | }) |
94 | 101 | ); |
@@ -120,7 +127,9 @@ export function generateStartupTable(sources: SourceDisplayInfo[]): string { |
120 | 127 | // Source header row |
121 | 128 | const idType = fitString(`${source.id} (${source.type})`, idTypeWidth); |
122 | 129 | const hostDb = fitString( |
123 | | - source.database ? `${source.host}/${source.database}` : source.host, |
| 130 | + source.host |
| 131 | + ? source.database ? `${source.host}/${source.database}` : source.host |
| 132 | + : source.database || "", |
124 | 133 | hostDbWidth |
125 | 134 | ); |
126 | 135 |
|
|
0 commit comments