-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlist.ts
More file actions
88 lines (79 loc) · 2.54 KB
/
list.ts
File metadata and controls
88 lines (79 loc) · 2.54 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Command } from '@cliffy/command';
import { handleCommandError } from '@/lib/errors.ts';
import { createAuthenticatedMuxClient } from '@/lib/mux.ts';
interface ListOptions {
orderDirection?: string;
limit?: number;
page?: number;
json?: boolean;
compact?: boolean;
timeframe?: string[];
}
export const listCommand = new Command()
.description('List annotations from Mux Data')
.option(
'--order-direction <orderDirection:string>',
'Order direction (asc or desc)',
{
value: (value: string): string => {
if (value !== 'asc' && value !== 'desc') {
throw new Error(
`Invalid order direction: ${value}. Must be "asc" or "desc".`,
);
}
return value;
},
},
)
.option('--limit <limit:number>', 'Number of results to return', {
default: 25,
})
.option('--page <page:number>', 'Page number for pagination', { default: 1 })
.option(
'--timeframe <timeframe:string>',
'Timeframe as Unix timestamps or duration (e.g., "24:hours"). Can be specified multiple times.',
{ collect: true },
)
.option('--json', 'Output JSON instead of pretty format')
.option('--compact', 'Output one line per annotation (grep-friendly)')
.action(async (options: ListOptions) => {
try {
const mux = await createAuthenticatedMuxClient();
const params: Record<string, unknown> = {
limit: options.limit,
page: options.page,
};
if (options.orderDirection) {
params.order_direction = options.orderDirection;
}
if (options.timeframe && options.timeframe.length > 0) {
params.timeframe = options.timeframe;
}
const response = await mux.data.annotations.list(params as never);
if (options.json) {
console.log(JSON.stringify(response, null, 2));
return;
}
const data = response.data ?? [];
if (data.length === 0) {
console.log('No annotations found.');
return;
}
if (options.compact) {
for (const annotation of data) {
console.log(
`${annotation.id}\t${annotation.date ?? '-'}\t${annotation.note ?? '-'}`,
);
}
} else {
for (const annotation of data) {
console.log(`Annotation ID: ${annotation.id}`);
console.log(` Date: ${annotation.date ?? '-'}`);
console.log(` Note: ${annotation.note ?? '-'}`);
console.log('');
}
}
} catch (error) {
await handleCommandError(error, 'annotations', 'list', options);
}
});