feat: add base mod

This commit is contained in:
2024-07-19 18:49:37 +02:00
parent f1764732c3
commit 4ffcb4a2fa
8 changed files with 68 additions and 13 deletions

View File

@@ -3,6 +3,20 @@ import { Container } from "./container.ts";
import { Image } from "./image.ts";
import { modem } from "./modem.ts";
/**
* @module
*
* A simple wrapper for pulling a docker image, and creating docker containers.
*
* @example
* ```ts
* import { docker } from "@valkyr/testcontainers";
*
* await docker.pullImage("docker:image");
*
* const container = await docker.createContainer({ Image: "docker:image" });
* ```
*/
export class Docker {
/**
* Create a new docker container.
@@ -12,7 +26,10 @@ export class Docker {
* @params config - The configuration for the container.
* @params query - Query parameters.
*/
async createContainer(config: Partial<ContainerConfig>, query: Partial<{ name: string; platform: string }> = {}) {
async createContainer(
config: Partial<ContainerConfig>,
query: Partial<{ name: string; platform: string }> = {},
): Promise<Container> {
const { Id, Warnings } = await modem.post<{ Id: string; Warnings: string[] }>({
path: "/containers/create",
query,
@@ -26,7 +43,7 @@ export class Docker {
*
* @param image - The image to pull.
*/
async pullImage(image: string) {
async pullImage(image: string): Promise<void> {
await new Image().create({ fromImage: image });
}
}

View File

@@ -12,7 +12,7 @@ export class Exec {
*
* @param body - Request body schema.
*/
async start(body: Partial<StartSchema> = {}) {
async start(body: Partial<StartSchema> = {}): Promise<void> {
await modem.post({ path: `/exec/${this.id}/start`, body });
await this.#endSignal();
}
@@ -20,7 +20,7 @@ export class Exec {
/**
* Return low-level information about the exec instance.
*/
async inspect() {
async inspect(): Promise<InspectResponse> {
return modem.get<InspectResponse>({ path: `/exec/${this.id}/json` });
}
@@ -37,7 +37,7 @@ export class Exec {
* [TODO] Introduce a timeout signal in case we want to add a treshold to the
* running time.
*/
async #endSignal() {
async #endSignal(): Promise<void> {
while (true) {
const info = await this.inspect();
if (info.Running === false) {

View File

@@ -1,7 +1,10 @@
import { Client, type Response } from "../../http/mod.ts";
class Modem {
constructor(readonly options: Deno.ConnectOptions | Deno.UnixConnectOptions, readonly client = new Client(options)) {}
constructor(
readonly options: Deno.ConnectOptions | Deno.UnixConnectOptions,
readonly client: Client = new Client(options),
) {}
/**
* Send a `POST` request to the Docker API.
@@ -59,7 +62,7 @@ class Modem {
}
}
export const modem = new Modem({
export const modem: Modem = new Modem({
path: "/var/run/docker.sock",
transport: "unix",
});