-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsql-instances.ts
More file actions
38 lines (32 loc) · 1.04 KB
/
sql-instances.ts
File metadata and controls
38 lines (32 loc) · 1.04 KB
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
import memoize from 'memoizee'
import { execCommandMultiline } from '../util/exec.js'
export type GoogleCloudSqlInstance = {
name: string
region: string
connectionName: string
port: number
}
const versionNameToPort = (version: string): number => {
const lowercased = version.toLowerCase()
if (lowercased.includes('mysql')) return 3306
if (lowercased.includes('postgres')) return 5432
return 1433
}
const parseInstance = (instance: string): GoogleCloudSqlInstance => {
const [connectionName, version] = instance.split(',')
const [, region, name] = connectionName.split(':')
const port = versionNameToPort(version)
return { name, region, connectionName, port }
}
export const fetchGoogleCloudSqlInstances = memoize(
(project: string): GoogleCloudSqlInstance[] => {
const instances = execCommandMultiline(`
gcloud sql instances list \
--project=${project} \
--format='csv(connectionName,databaseVersion)' \
--quiet
`)
// skip header line
return instances.slice(1).map(parseInstance)
}
)