Skip to content

Commit c1038ca

Browse files
committed
feat: containers
1 parent aca43d6 commit c1038ca

File tree

8 files changed

+10417
-23
lines changed

8 files changed

+10417
-23
lines changed

deno.lock

Lines changed: 550 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api_specification.ts

Lines changed: 9792 additions & 0 deletions
Large diffs are not rendered by default.

src/containers/containers.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { DockerEngineClient } from '../docker_engine_client.ts';
2+
import type { ContainerInspectResponse } from '../api_specification.ts'
3+
4+
5+
export class Containers extends DockerEngineClient {
6+
7+
/**
8+
* Create an new Containers instance.
9+
*
10+
* @param path Socket path to use for the connection
11+
*/
12+
constructor(path? : string) {
13+
super(path)
14+
}
15+
16+
public list() : Promise<ContainerInspectResponse[]> {
17+
return this.send('/containers/json').then((response) => response.json())
18+
}
19+
}

src/containers/index.ts

Whitespace-only changes.

src/containers/list/container.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface Container {
2+
Id : string,
3+
Names : unknown[],
4+
Image : string,
5+
ImageID : string,
6+
}

src/docker.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Containers } from './containers/containers.ts';
2+
3+
export class Docker {
4+
private readonly path : string | undefined;
5+
6+
/**
7+
* Create an new Docker engine client instance.
8+
*
9+
* @param path Socket path to use for the connection
10+
*/
11+
constructor(path? : string) {
12+
this.path = path;
13+
}
14+
15+
public get containers() : Containers {
16+
return new Containers(this.path);
17+
}
18+
}

src/docker_engine_client.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Socket, type SocketRequestInit } from '@typescriptplayground/socket';
2+
3+
/**
4+
* This class represents a docker engine client.
5+
*/
6+
export class DockerEngineClient {
7+
private readonly socket : Socket;
8+
9+
/**
10+
* Create an new Docker engine client instance.
11+
*
12+
* @param path Socket path to use for the connection
13+
*/
14+
constructor(path : string = '/var/run/docker.sock') {
15+
this.socket = new Socket(path);
16+
}
17+
18+
/**
19+
* Send instruction to the docker engine.
20+
*
21+
* @param path Endpoint, ex. `/containers/json`
22+
*/
23+
protected send(
24+
path : string,
25+
init? : SocketRequestInit
26+
) : Promise<Response> {
27+
return this.socket.request(path, init);
28+
}
29+
}

src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { Socket } from '@typescriptplayground/socket';
1+
import { Docker } from './docker.ts';
22

3-
const socket = new Socket('/var/run/docker.sock');
3+
const docker = new Docker()
44

5-
socket.request("/containers/json")
6-
.then(res => res.json())
7-
.then(json => console.log(json));
5+
docker.containers.list().then(containers => {containers.map(container => console.log(container.Image))})

0 commit comments

Comments
 (0)