Skip to content

Commit c2a5ea3

Browse files
committed
improve network attachment output
1 parent 9a53fed commit c2a5ea3

File tree

1 file changed

+50
-25
lines changed
  • internal-packages/testcontainers/src

1 file changed

+50
-25
lines changed

internal-packages/testcontainers/src/index.ts

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ function stringToLines(str: string): string[] {
9494
return str.split("\n").filter(Boolean);
9595
}
9696

97+
function lineToWords(line: string): string[] {
98+
return line.trim().split(/\s+/);
99+
}
100+
97101
async function getDockerNetworks(): Promise<string[]> {
98102
try {
99103
const result = await x("docker", ["network", "ls" /* , "--no-trunc" */]);
@@ -114,53 +118,68 @@ async function getDockerContainers(): Promise<string[]> {
114118
}
115119
}
116120

117-
type DockerNetworkAttachment = {
118-
networkId: string;
119-
networkName: string;
120-
containers: string[];
121+
type DockerResource = { id: string; name: string };
122+
123+
type DockerNetworkAttachment = DockerResource & {
124+
containers: DockerResource[];
121125
};
122126

123127
export async function getDockerNetworkAttachments(): Promise<DockerNetworkAttachment[]> {
124128
let attachments: DockerNetworkAttachment[] = [];
125-
let networkIds: string[] = [];
129+
let networks: DockerResource[] = [];
126130

127131
try {
128-
const result = await x("docker", ["network", "ls", "-q"]);
129-
networkIds = stringToLines(result.stdout);
132+
const result = await x("docker", [
133+
"network",
134+
"ls",
135+
"--format",
136+
'{{.ID | printf "%.12s"}} {{.Name}}',
137+
]);
138+
139+
const lines = stringToLines(result.stdout);
140+
141+
networks = lines.map((line) => {
142+
const [id, name] = lineToWords(line);
143+
return { id, name };
144+
});
130145
} catch (err) {
131146
console.error("Failed to list docker networks:", err);
132147
}
133148

134-
for (const networkId of networkIds) {
149+
for (const { id, name } of networks) {
135150
try {
136-
const inspectResult = await x("docker", [
151+
// Get containers, one per line: id name\n
152+
const containersResult = await x("docker", [
137153
"network",
138154
"inspect",
139155
"--format",
140-
'{{ .Name }}{{ range $k, $v := .Containers }} {{ printf "%.12s %s" $k .Name }}{{ end }}',
141-
networkId,
156+
"{{range $k, $v := .Containers}}{{$k}} {{$v.Name}}\n{{end}}",
157+
id,
142158
]);
159+
const lines = stringToLines(containersResult.stdout);
143160

144-
const [networkName, ...containers] = inspectResult.stdout.trim().split(/\s+/);
145-
attachments.push({ networkId, networkName, containers });
161+
const containers: DockerResource[] = lines.map((line) => {
162+
const [id, name] = lineToWords(line);
163+
return { id, name };
164+
});
165+
166+
attachments.push({ id, name, containers });
146167
} catch (err) {
147-
console.error(`Failed to inspect network ${networkId}:`, err);
148-
attachments.push({ networkId, networkName: String(err), containers: [] });
168+
console.error(`Failed to inspect network ${id}:`, err);
169+
attachments.push({ id, name, containers: [] });
149170
}
150171
}
151172

152173
return attachments;
153174
}
154175

155-
type DockerContainerNetwork = {
156-
containerId: string;
157-
containerName: string;
176+
type DockerContainerNetwork = DockerResource & {
158177
networks: string[];
159178
};
160179

161180
export async function getDockerContainerNetworks(): Promise<DockerContainerNetwork[]> {
162181
let results: DockerContainerNetwork[] = [];
163-
let containers: string[] = [];
182+
let containers: DockerResource[] = [];
164183

165184
try {
166185
const result = await x("docker", [
@@ -169,26 +188,32 @@ export async function getDockerContainerNetworks(): Promise<DockerContainerNetwo
169188
"--format",
170189
'{{.ID | printf "%.12s"}} {{.Names}}',
171190
]);
172-
containers = stringToLines(result.stdout);
191+
192+
const lines = stringToLines(result.stdout);
193+
194+
containers = lines.map((line) => {
195+
const [id, name] = lineToWords(line);
196+
return { id, name };
197+
});
173198
} catch (err) {
174199
console.error("Failed to list docker containers:", err);
175200
}
176201

177-
for (const [containerId, containerName] of containers.map((c) => c.trim().split(/\s+/))) {
202+
for (const { id, name } of containers) {
178203
try {
179204
const inspectResult = await x("docker", [
180205
"inspect",
181206
"--format",
182207
"{{ range $k, $v := .NetworkSettings.Networks }}{{ $k }}{{ end }}",
183-
containerId,
208+
id,
184209
]);
185210

186211
const networks = inspectResult.stdout.trim().split(/\s+/);
187212

188-
results.push({ containerId, containerName, networks });
213+
results.push({ id, name, networks });
189214
} catch (err) {
190-
console.error(`Failed to inspect container ${containerId}:`, err);
191-
results.push({ containerId, containerName: String(err), networks: [] });
215+
console.error(`Failed to inspect container ${id}:`, err);
216+
results.push({ id, name: String(err), networks: [] });
192217
}
193218
}
194219

0 commit comments

Comments
 (0)