From 898a9191454eed5b7804229e8b98451757e5d9f6 Mon Sep 17 00:00:00 2001 From: spencer Date: Fri, 17 Apr 2026 16:42:15 -0400 Subject: [PATCH] wip: feat: Migrated version command to zig. --- build.zig | 7 +++++++ build.zig.zon | 2 +- cmd/version.go | 1 + src/main.zig | 28 ++++++++++++++-------------- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/build.zig b/build.zig index 4431ea9..11330bf 100644 --- a/build.zig +++ b/build.zig @@ -83,6 +83,13 @@ pub fn build(b: *std.Build) void { }), }); + const version = b.option([]const u8, "version", "application version string") orelse "dev"; + + const options = b.addOptions(); + options.addOption([]const u8, "version", version); + + exe.root_module.addOptions("config", options); + // This declares intent for the executable to be installed into the // install prefix when running `zig build` (i.e. when executing the default // step). By default the install prefix is `zig-out/` but can be overridden diff --git a/build.zig.zon b/build.zig.zon index bebba28..c87c837 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -9,7 +9,7 @@ .name = .envr, // This is a [Semantic Version](https://semver.org/). // In a future version of Zig it will be used for package deduplication. - .version = "0.0.0", + .version = "0.3.0", // Together with name, this represents a globally unique package // identifier. This field is generated by the Zig toolchain when the // package is first created, and then *never changes*. This allows diff --git a/cmd/version.go b/cmd/version.go index 6bc5f5c..615d1d4 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -15,6 +15,7 @@ var ( var long bool // versionCmd represents the version command +// Deprecated: Remove when Zig has the chance to emit help var versionCmd = &cobra.Command{ Use: "version", Short: "Show envr's version", diff --git a/src/main.zig b/src/main.zig index 62078a0..e9f0fa8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,8 @@ const std = @import("std"); const Io = std.Io; +const config = @import("config"); + const envr = @import("envr"); const goBinary = "envr-go"; @@ -11,27 +13,25 @@ pub fn main(init: std.process.Init) !void { const args = try init.minimal.args.toSlice(arena); - // if (std.mem.eql(u8, args[1], "version")) { - // version(args[1..]); - // } else { - return fallbackToGo(init.io, args, arena); - // } + if (args.len > 1 and std.mem.eql(u8, args[1], "version")) { + return version(init.io); + } else { + return fallback_to_go(init.io, args, arena); + } } -fn version(args: []const [:0]const u8) void { +fn version(io: Io) !void { // std.debug.print("hello from Zig!\n", .{}); - // for (args[1..]) |arg| { - // std.debug.print("arg: {s}\n", .{arg}); - // } - // + var stdout_buffer: [1024]u8 = undefined; + var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer); + const stdout_writer = &stdout_file_writer.interface; - _ = args; - - std.debug.print("TODO: Implement\n", .{}); + try stdout_writer.print("{s}\n", .{config.version}); + try stdout_writer.flush(); } -fn fallbackToGo( +fn fallback_to_go( io: Io, args: []const [:0]const u8, arena: std.mem.Allocator,