summaryrefslogtreecommitdiff
path: root/DockerStep.zig
diff options
context:
space:
mode:
authorChristian Segundo2023-07-24 10:28:58 +0200
committerChristian Segundo2023-07-24 10:28:58 +0200
commitf743e05e207046073999ee7234a157359d6a4f57 (patch)
tree0ceb01ffdcdc3a094e488c28e80c80d89f8309c5 /DockerStep.zig
parentb02b7f71978a172848322f0671d580e425634916 (diff)
downloadzmission-master.tar.gz
Diffstat (limited to 'DockerStep.zig')
-rw-r--r--DockerStep.zig65
1 files changed, 65 insertions, 0 deletions
diff --git a/DockerStep.zig b/DockerStep.zig
new file mode 100644
index 0000000..9673c09
--- /dev/null
+++ b/DockerStep.zig
@@ -0,0 +1,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;
+}