mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-10 08:05:14 +02:00
feat(cli): formalize dev-friendly result output
This commit is contained in:
parent
42365481ac
commit
b95cb9b334
2 changed files with 22 additions and 5 deletions
|
|
@ -28,6 +28,16 @@ export interface PrintListArgs<Row> {
|
|||
io: KtxCliIo;
|
||||
}
|
||||
|
||||
export interface KtxJsonResultEnvelope<T> {
|
||||
kind: string;
|
||||
data: T;
|
||||
meta?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function writeJsonResult<T>(io: KtxCliIo, envelope: KtxJsonResultEnvelope<T>): void {
|
||||
io.stdout.write(`${JSON.stringify(envelope, null, 2)}\n`);
|
||||
}
|
||||
|
||||
export function printList<Row extends object>(args: PrintListArgs<Row>): void {
|
||||
switch (args.mode) {
|
||||
case 'json':
|
||||
|
|
@ -61,12 +71,11 @@ function printListPlain<Row extends object>(args: PrintListArgs<Row>): void {
|
|||
}
|
||||
|
||||
function printListJson<Row extends object>(args: PrintListArgs<Row>): void {
|
||||
const envelope = {
|
||||
writeJsonResult(args.io, {
|
||||
kind: 'list',
|
||||
data: { items: args.rows },
|
||||
meta: { command: args.command },
|
||||
};
|
||||
args.io.stdout.write(`${JSON.stringify(envelope, null, 2)}\n`);
|
||||
});
|
||||
}
|
||||
|
||||
function pluralize(count: number, singular: string): string {
|
||||
|
|
|
|||
|
|
@ -398,10 +398,18 @@ joins: []
|
|||
listIo.io,
|
||||
);
|
||||
expect(code).toBe(0);
|
||||
expect(listIo.stderr()).toBe('');
|
||||
|
||||
const parsed = JSON.parse(listIo.stdout());
|
||||
expect(parsed.kind).toBe('list');
|
||||
expect(parsed.meta).toEqual({ command: 'sl list' });
|
||||
expect(parsed).toMatchObject({
|
||||
kind: 'list',
|
||||
data: {
|
||||
items: expect.any(Array),
|
||||
},
|
||||
meta: {
|
||||
command: 'sl list',
|
||||
},
|
||||
});
|
||||
expect(parsed.data.items).toHaveLength(1);
|
||||
expect(parsed.data.items[0]).toMatchObject({
|
||||
connectionId: 'warehouse',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue