summaryrefslogtreecommitdiff
path: root/DockerStep.zig
blob: 9673c099f4c321cf46d399705ff5b3a4fd84f611 (plain) (blame)
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
//! A zig builder step that runs "docker run ..."
//! This is primarily meant to do integration tests.
const DockerStep = @This();

const std = @import("std");
const Step = std.build.Step;
const RunStep = std.build.RunStep;

pub const Options = struct {
    /// Container name
    name: []const u8,

    /// The name of the image to run
    image: []const u8,

    /// The ports to expose
    ports: []const []const u8,
};

step: *Step,

pub fn create(b: *std.Build, opts: Options) *DockerStep {
    const self = b.allocator.create(DockerStep) catch @panic("OOM");

    const run_delete = remove(b, opts.name);

    const run_create = run: {
        const run = RunStep.create(b, b.fmt("docker run {s}", .{opts.name}));
        run.has_side_effects = true;
        run.addArgs(&.{
            "docker",
            "run",
            "-d",
            "--rm",
            "--name",
            opts.name,
        });
        for (opts.ports) |p| run.addArgs(&.{ "-p", p });
        run.addArg(opts.image);
        break :run run;
    };

    run_create.step.dependOn(run_delete.step);

    self.* = .{
        .step = &run_create.step,
    };
    return self;
}

pub fn remove(b: *std.Build, name: []const u8) *DockerStep {
    const self = b.allocator.create(DockerStep) catch @panic("OOM");

    const run_delete = run: {
        const run = RunStep.create(b, b.fmt("docker rm {s}", .{name}));
        run.has_side_effects = true;
        run.addArgs(&.{ "docker", "rm", name, "-f" });
        break :run run;
    };

    self.* = .{
        .step = &run_delete.step,
    };
    return self;
}