-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathformatters.ts
More file actions
311 lines (272 loc) · 9.69 KB
/
formatters.ts
File metadata and controls
311 lines (272 loc) · 9.69 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import type {
DockerPs,
DockerBuild,
DockerLogs,
DockerImages,
DockerRun,
DockerExec,
DockerComposeUp,
DockerComposeDown,
DockerPull,
} from "../schemas/index.js";
/** Formats structured Docker container data into a human-readable listing with state and ports. */
export function formatPs(data: DockerPs): string {
const lines = [`${data.total} containers (${data.running} running, ${data.stopped} stopped)`];
for (const c of data.containers) {
const ports = c.ports.length
? ` [${c.ports.map((p) => (p.host ? `${p.host}->${p.container}/${p.protocol}` : `${p.container}/${p.protocol}`)).join(", ")}]`
: "";
lines.push(` ${c.state.padEnd(10)} ${c.name} (${c.image})${ports}`);
}
return lines.join("\n");
}
/** Formats structured Docker build results into a human-readable success/failure summary. */
export function formatBuild(data: DockerBuild): string {
if (data.success) {
const parts = [`Build succeeded in ${data.duration}s`];
if (data.imageId) parts[0] += ` → ${data.imageId}`;
if (data.steps) parts.push(`${data.steps} steps`);
return parts.join(", ");
}
const lines = [`Build failed (${data.duration}s)`];
for (const err of data.errors) {
lines.push(` ${err}`);
}
return lines.join("\n");
}
/** Formats structured Docker logs data into a human-readable output with container name and line count. */
export function formatLogs(data: DockerLogs): string {
return `${data.container} (${data.total} lines)\n${data.lines.join("\n")}`;
}
/** Formats structured Docker image data into a human-readable listing with repository, tag, and size. */
export function formatImages(data: DockerImages): string {
if (data.total === 0) return "No images found.";
const lines = [`${data.total} images:`];
for (const img of data.images) {
const tag = img.tag && img.tag !== "<none>" ? `:${img.tag}` : "";
lines.push(` ${img.repository}${tag} (${img.size}, ${img.created})`);
}
return lines.join("\n");
}
/** Formats structured Docker run output into a human-readable summary. */
export function formatRun(data: DockerRun): string {
const name = data.name ? ` (${data.name})` : "";
const mode = data.detached ? "detached" : "attached";
return `Container ${data.containerId}${name} started from ${data.image} [${mode}]`;
}
/** Formats structured Docker exec output into a human-readable summary. */
export function formatExec(data: DockerExec): string {
const status = data.success ? "succeeded" : `failed (exit code ${data.exitCode})`;
const lines = [`Exec ${status}`];
if (data.stdout) lines.push(data.stdout);
if (data.stderr) lines.push(`stderr: ${data.stderr}`);
return lines.join("\n");
}
/** Formats structured Docker Compose up output into a human-readable summary. */
export function formatComposeUp(data: DockerComposeUp): string {
if (!data.success) return "Compose up failed";
if (data.started === 0) return "Compose up succeeded (no new services started)";
return `Compose up: ${data.started} services started (${data.services.join(", ")})`;
}
/** Formats structured Docker Compose down output into a human-readable summary. */
export function formatComposeDown(data: DockerComposeDown): string {
if (!data.success) return "Compose down failed";
return `Compose down: ${data.stopped} stopped, ${data.removed} removed`;
}
/** Formats structured Docker pull output into a human-readable summary. */
export function formatPull(data: DockerPull): string {
if (!data.success) return `Pull failed for ${data.image}:${data.tag}`;
const digest = data.digest ? ` (${data.digest.slice(0, 19)}...)` : "";
return `Pulled ${data.image}:${data.tag}${digest}`;
}
// ── Compact types, mappers, and formatters ───────────────────────────
/** Compact ps: short containerId, name, image, status only. Drop ports, createdAt, state details. */
export interface DockerPsCompact {
[key: string]: unknown;
containers: Array<{ id: string; name: string; image: string; status: string }>;
total: number;
running: number;
stopped: number;
}
export function compactPsMap(data: DockerPs): DockerPsCompact {
return {
containers: data.containers.map((c) => ({
id: c.id.slice(0, 12),
name: c.name,
image: c.image,
status: c.status,
})),
total: data.total,
running: data.running,
stopped: data.stopped,
};
}
export function formatPsCompact(data: DockerPsCompact): string {
const lines = [`${data.total} containers (${data.running} running)`];
for (const c of data.containers) {
lines.push(` ${c.id.slice(0, 12)} ${c.name} (${c.image}) ${c.status}`);
}
return lines.join("\n");
}
/** Compact images: repository, tag, short id, size. Drop createdAt. */
export interface DockerImagesCompact {
[key: string]: unknown;
images: Array<{ id: string; repository: string; tag: string; size: string }>;
total: number;
}
export function compactImagesMap(data: DockerImages): DockerImagesCompact {
return {
images: data.images.map((img) => ({
id: img.id.slice(0, 12),
repository: img.repository,
tag: img.tag,
size: img.size,
})),
total: data.total,
};
}
export function formatImagesCompact(data: DockerImagesCompact): string {
if (data.total === 0) return "No images found.";
const lines = [`${data.total} images:`];
for (const img of data.images) {
const tag = img.tag && img.tag !== "<none>" ? `:${img.tag}` : "";
lines.push(` ${img.repository}${tag} (${img.size})`);
}
return lines.join("\n");
}
/** Compact build: success, imageId, duration. Drop warnings array details, keep error count. */
export interface DockerBuildCompact {
[key: string]: unknown;
success: boolean;
imageId?: string;
duration: number;
errorCount: number;
}
export function compactBuildMap(data: DockerBuild): DockerBuildCompact {
return {
success: data.success,
...(data.imageId ? { imageId: data.imageId } : {}),
duration: data.duration,
errorCount: data.errors.length,
};
}
export function formatBuildCompact(data: DockerBuildCompact): string {
if (data.success) {
const id = data.imageId ? ` → ${data.imageId}` : "";
return `Build succeeded in ${data.duration}s${id}`;
}
return `Build failed (${data.duration}s, ${data.errorCount} errors)`;
}
/** Compact logs: container, count, first/last few lines. Drop full lines array if large. */
export interface DockerLogsCompact {
[key: string]: unknown;
container: string;
total: number;
head: string[];
tail: string[];
}
export function compactLogsMap(data: DockerLogs): DockerLogsCompact {
const HEAD_SIZE = 5;
const TAIL_SIZE = 5;
return {
container: data.container,
total: data.total,
head: data.lines.slice(0, HEAD_SIZE),
tail: data.total > HEAD_SIZE + TAIL_SIZE ? data.lines.slice(-TAIL_SIZE) : [],
};
}
export function formatLogsCompact(data: DockerLogsCompact): string {
const parts = [`${data.container} (${data.total} lines)`];
if (data.head.length) parts.push(data.head.join("\n"));
if (data.tail.length)
parts.push(
` ... ${data.total - data.head.length - data.tail.length} lines omitted ...`,
data.tail.join("\n"),
);
return parts.join("\n");
}
/** Compact pull: passthrough (already small). */
export interface DockerPullCompact {
[key: string]: unknown;
image: string;
tag: string;
success: boolean;
}
export function compactPullMap(data: DockerPull): DockerPullCompact {
return {
image: data.image,
tag: data.tag,
success: data.success,
};
}
export function formatPullCompact(data: DockerPullCompact): string {
if (!data.success) return `Pull failed for ${data.image}:${data.tag}`;
return `Pulled ${data.image}:${data.tag}`;
}
/** Compact run: passthrough (already small). */
export interface DockerRunCompact {
[key: string]: unknown;
containerId: string;
image: string;
detached: boolean;
}
export function compactRunMap(data: DockerRun): DockerRunCompact {
return {
containerId: data.containerId,
image: data.image,
detached: data.detached,
};
}
export function formatRunCompact(data: DockerRunCompact): string {
const mode = data.detached ? "detached" : "attached";
return `Container ${data.containerId} from ${data.image} [${mode}]`;
}
/** Compact exec: passthrough (already small). */
export interface DockerExecCompact {
[key: string]: unknown;
exitCode: number;
success: boolean;
}
export function compactExecMap(data: DockerExec): DockerExecCompact {
return {
exitCode: data.exitCode,
success: data.success,
};
}
export function formatExecCompact(data: DockerExecCompact): string {
return data.success ? "Exec succeeded" : `Exec failed (exit code ${data.exitCode})`;
}
/** Compact compose up: passthrough (already small). */
export interface DockerComposeUpCompact {
[key: string]: unknown;
success: boolean;
started: number;
}
export function compactComposeUpMap(data: DockerComposeUp): DockerComposeUpCompact {
return {
success: data.success,
started: data.started,
};
}
export function formatComposeUpCompact(data: DockerComposeUpCompact): string {
if (!data.success) return "Compose up failed";
return `Compose up: ${data.started} services started`;
}
/** Compact compose down: passthrough (already small). */
export interface DockerComposeDownCompact {
[key: string]: unknown;
success: boolean;
stopped: number;
removed: number;
}
export function compactComposeDownMap(data: DockerComposeDown): DockerComposeDownCompact {
return {
success: data.success,
stopped: data.stopped,
removed: data.removed,
};
}
export function formatComposeDownCompact(data: DockerComposeDownCompact): string {
if (!data.success) return "Compose down failed";
return `Compose down: ${data.stopped} stopped, ${data.removed} removed`;
}