From 053119083c2c93cb1fc4129bc647c03301f4010d Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Mon, 23 Jan 2023 11:28:32 +0100 Subject: [PATCH] zig env: remove the json output The current json output is not very convenient to use from the shell or a Zig program. An example using the shell: `zig env | jq -r .lib_dir`. Remove the json output, and instead use the POSIX shell syntax: name="value" Additionally, when args is not empty, assume each argument is the environment variable name and print the associated value. Unrecognized environment variables are ignored. The new output format has been copied from `go env`, with the difference that `go env` uses OS specific syntax for windows and plan9. Define the environment variables in a single place, in order to avoid possible bugs. --- src/print_env.zig | 52 ++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/print_env.zig b/src/print_env.zig index cf4720c5f2..fcfc3f3aeb 100644 --- a/src/print_env.zig +++ b/src/print_env.zig @@ -1,11 +1,16 @@ const std = @import("std"); +const mem = std.mem; const build_options = @import("build_options"); const introspect = @import("introspect.zig"); const Allocator = std.mem.Allocator; const fatal = @import("main.zig").fatal; +const Env = struct { + name: []const u8, + value: []const u8, +}; + pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writer) !void { - _ = args; const self_exe_path = try introspect.findZigExePath(gpa); defer gpa.free(self_exe_path); @@ -25,32 +30,33 @@ pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writ const triple = try info.target.zigTriple(gpa); defer gpa.free(triple); + const envars: []Env = &[_]Env{ + .{ .name = "zig_exe", .value = self_exe_path }, + .{ .name = "lib_dir", .value = zig_lib_directory.path.? }, + .{ .name = "std_dir", .value = zig_std_dir }, + .{ .name = "global_cache_dir", .value = global_cache_dir }, + .{ .name = "version", .value = build_options.version }, + .{ .name = "target", .value = triple }, + }; + var bw = std.io.bufferedWriter(stdout); const w = bw.writer(); - var jws = std.json.writeStream(w, .{ .whitespace = .indent_1 }); + if (args.len > 0) { + for (args) |name| { + for (envars) |env| { + if (mem.eql(u8, name, env.name)) { + try w.print("{s}\n", .{env.value}); + } + } + } + try bw.flush(); - try jws.beginObject(); + return; + } - try jws.objectField("zig_exe"); - try jws.write(self_exe_path); - - try jws.objectField("lib_dir"); - try jws.write(zig_lib_directory.path.?); - - try jws.objectField("std_dir"); - try jws.write(zig_std_dir); - - try jws.objectField("global_cache_dir"); - try jws.write(global_cache_dir); - - try jws.objectField("version"); - try jws.write(build_options.version); - - try jws.objectField("target"); - try jws.write(triple); - - try jws.endObject(); - try w.writeByte('\n'); + for (envars) |env| { + try w.print("{[name]s}=\"{[value]s}\"\n", env); + } try bw.flush(); }