authorgravatar for manlio.perillo@gmail.comManlio Perillo <manlio.perillo@gmail.com> 2023-01-23 11:28:32+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-17 20:10:23-07:00
log053119083c2c93cb1fc4129bc647c03301f4010d
tree242484e01291155621e8655635fa9e4276eb03b1
parent7a9500fd80990a3dc47821e627162bf769eecbba

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.

1 files changed, 31 insertions(+), 25 deletions(-)

src/print_env.zig+31-25
......@@ -1,11 +1,16 @@
11const std = @import("std");
2const mem = std.mem;
23const build_options = @import("build_options");
34const introspect = @import("introspect.zig");
45const Allocator = std.mem.Allocator;
56const fatal = @import("main.zig").fatal;
67
8const Env = struct {
9 name: []const u8,
10 value: []const u8,
11};
12
713pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writer) !void {
8 _ = args;
914 const self_exe_path = try introspect.findZigExePath(gpa);
1015 defer gpa.free(self_exe_path);
1116
......@@ -25,32 +30,33 @@ pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writ
2530 const triple = try info.target.zigTriple(gpa);
2631 defer gpa.free(triple);
2732
33 const envars: []Env = &[_]Env{
34 .{ .name = "zig_exe", .value = self_exe_path },
35 .{ .name = "lib_dir", .value = zig_lib_directory.path.? },
36 .{ .name = "std_dir", .value = zig_std_dir },
37 .{ .name = "global_cache_dir", .value = global_cache_dir },
38 .{ .name = "version", .value = build_options.version },
39 .{ .name = "target", .value = triple },
40 };
41
2842 var bw = std.io.bufferedWriter(stdout);
2943 const w = bw.writer();
3044
31 var jws = std.json.writeStream(w, .{ .whitespace = .indent_1 });
32
33 try jws.beginObject();
34
35 try jws.objectField("zig_exe");
36 try jws.write(self_exe_path);
37
38 try jws.objectField("lib_dir");
39 try jws.write(zig_lib_directory.path.?);
40
41 try jws.objectField("std_dir");
42 try jws.write(zig_std_dir);
43
44 try jws.objectField("global_cache_dir");
45 try jws.write(global_cache_dir);
46
47 try jws.objectField("version");
48 try jws.write(build_options.version);
49
50 try jws.objectField("target");
51 try jws.write(triple);
52
53 try jws.endObject();
54 try w.writeByte('\n');
45 if (args.len > 0) {
46 for (args) |name| {
47 for (envars) |env| {
48 if (mem.eql(u8, name, env.name)) {
49 try w.print("{s}\n", .{env.value});
50 }
51 }
52 }
53 try bw.flush();
54
55 return;
56 }
57
58 for (envars) |env| {
59 try w.print("{[name]s}=\"{[value]s}\"\n", env);
60 }
5561 try bw.flush();
5662}