Skip to content

Commit

Permalink
Add --version to dockerc and --dockerc-version to runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsIrl committed Aug 16, 2024
1 parent dd7277e commit 3926efe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
8 changes: 8 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const skip_crun_build = b.option(bool, "skip_crun_build", "Skip crun build") orelse false;
const dockerc_version = b.option([]const u8, "dockerc_version", "Set dockerc version") orelse "HEAD";

const build_info = b.addOptions();
build_info.addOption([]const u8, "dockerc_version", dockerc_version);

const zstd = b.createModule(.{});
zstd.addAssemblyFile(b.path("zstd/lib/decompress/huf_decompress_amd64.S"));
Expand Down Expand Up @@ -184,6 +188,8 @@ pub fn build(b: *std.Build) void {
.link_libc = true,
});

runtime.addOptions("build_info", build_info);

runtime.addImport("zstd", zstd);
runtime.addImport("fuse-overlayfs", fuse_fss);

Expand Down Expand Up @@ -404,6 +410,8 @@ pub fn build(b: *std.Build) void {
.link_libc = true,
});

dockerc.root_module.addOptions("build_info", build_info);

dockerc.addIncludePath(b.path("zstd/lib"));
dockerc.root_module.addImport("zstd", zstd);
dockerc.addCSourceFiles(.{
Expand Down
7 changes: 7 additions & 0 deletions src/dockerc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const builtin = @import("builtin");
const std = @import("std");
const clap = @import("clap");
const common = @import("common.zig");
const build_info = @import("build_info");

const mkdtemp = common.mkdtemp;
const extract_file = common.extract_file;
Expand Down Expand Up @@ -48,6 +49,7 @@ pub fn main() !void {

const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\--version Display dockerc version.
\\-i, --image <str> Image to pull.
\\-o, --output <str> Output file.
\\--arch <str> Architecture (amd64, arm64).
Expand All @@ -66,6 +68,11 @@ pub fn main() !void {
};
defer res.deinit();

if (res.args.version != 0) {
std.debug.print("dockerc version: {s}\n", .{build_info.dockerc_version});
return;
}

if (res.args.help != 0) {
try clap.help(io.getStdErr().writer(), clap.Help, &params, .{});
return;
Expand Down
14 changes: 11 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const assert = std.debug.assert;
const common = @import("common.zig");
const build_info = @import("build_info");

const mkdtemp = common.mkdtemp;
const extract_file = common.extract_file;
Expand Down Expand Up @@ -356,6 +357,16 @@ fn umount(path: [*:0]const u8) void {
}

pub fn main() !u8 {
var args = std.process.args();
const executable_path = args.next() orelse @panic("unreachable: there must be a executable name");

while (args.next()) |arg| {
if (eql(u8, arg, "--dockerc-version")) {
std.debug.print("dockerc version: {s}\n", .{build_info.dockerc_version});
return 0;
}
}

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
Expand Down Expand Up @@ -485,9 +496,6 @@ pub fn main() !u8 {
std.posix.close(read_fd);
}

var args = std.process.args();
const executable_path = args.next() orelse unreachable;

var temp_dir_path = "/tmp/dockerc-XXXXXX".*;
try mkdtemp(&temp_dir_path);

Expand Down

0 comments on commit 3926efe

Please sign in to comment.