-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1046 from mittwald/feat/database-list-command
feat: add "database list" command for convenience
- Loading branch information
Showing
3 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { assertStatus } from "@mittwald/api-client-commons"; | ||
import { MittwaldAPIV2Client } from "@mittwald/api-client"; | ||
import { ListColumns } from "../../rendering/formatter/Table.js"; | ||
import { ListBaseCommand } from "../../lib/basecommands/ListBaseCommand.js"; | ||
import { projectFlags } from "../../lib/resources/project/flags.js"; | ||
|
||
type ResponseItem = { | ||
id: string; | ||
kind: "mysql" | "redis"; | ||
name: string; | ||
version: string; | ||
description: string; | ||
hostname: string; | ||
isReady: boolean; | ||
createdAt: string; | ||
}; | ||
type Response = Awaited< | ||
ReturnType<MittwaldAPIV2Client["database"]["listMysqlDatabases"]> | ||
>; | ||
|
||
export class List extends ListBaseCommand<typeof List, ResponseItem, Response> { | ||
static description = "List all kinds of databases belonging to a project."; | ||
|
||
static args = {}; | ||
static flags = { | ||
...ListBaseCommand.baseFlags, | ||
...projectFlags, | ||
}; | ||
|
||
public async getData(): Promise<ResponseItem[]> { | ||
const projectId = await this.withProjectId(List); | ||
const databases: ResponseItem[] = []; | ||
|
||
const mysqlResponse = await this.apiClient.database.listMysqlDatabases({ | ||
projectId, | ||
}); | ||
assertStatus(mysqlResponse, 200); | ||
|
||
const redisResponse = await this.apiClient.database.listRedisDatabases({ | ||
projectId, | ||
}); | ||
assertStatus(redisResponse, 200); | ||
|
||
databases.push( | ||
...mysqlResponse.data.map((d) => ({ ...d, kind: "mysql" as const })), | ||
...redisResponse.data.map((d) => ({ | ||
...d, | ||
kind: "redis" as const, | ||
isReady: true, | ||
})), | ||
); | ||
|
||
return databases; | ||
} | ||
|
||
protected getColumns(ignoredData: ResponseItem[]): ListColumns<ResponseItem> { | ||
const { id, name, createdAt } = super.getColumns(ignoredData, { | ||
shortIdKey: "name", | ||
}); | ||
return { | ||
id, | ||
name, | ||
version: { | ||
header: "Version", | ||
get(row) { | ||
if (row.kind === "mysql") { | ||
return `MySQL ${row.version}`; | ||
} else if (row.kind === "redis") { | ||
return `Redis ${row.version}`; | ||
} else { | ||
return "Unknown"; | ||
} | ||
}, | ||
}, | ||
description: { | ||
header: "Description", | ||
}, | ||
hostname: { | ||
header: "Hostname", | ||
}, | ||
status: { | ||
header: "Status", | ||
get: (row) => { | ||
if (!row.isReady) { | ||
return "pending"; | ||
} | ||
return "ready"; | ||
}, | ||
}, | ||
createdAt, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters