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 @@...@@ -1,11 +1,16 @@
1const std = @import("std");1const std = @import("std");
2const mem = std.mem;
2const build_options = @import("build_options");3const build_options = @import("build_options");
3const introspect = @import("introspect.zig");4const introspect = @import("introspect.zig");
4const Allocator = std.mem.Allocator;5const Allocator = std.mem.Allocator;
5const fatal = @import("main.zig").fatal;6const fatal = @import("main.zig").fatal;
67
8const Env = struct {
9 name: []const u8,
10 value: []const u8,
11};
12
7pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writer) !void {13pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writer) !void {
8 _ = args;
9 const self_exe_path = try introspect.findZigExePath(gpa);14 const self_exe_path = try introspect.findZigExePath(gpa);
10 defer gpa.free(self_exe_path);15 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...@@ -25,32 +30,33 @@ pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writ
25 const triple = try info.target.zigTriple(gpa);30 const triple = try info.target.zigTriple(gpa);
26 defer gpa.free(triple);31 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
28 var bw = std.io.bufferedWriter(stdout);42 var bw = std.io.bufferedWriter(stdout);
29 const w = bw.writer();43 const w = bw.writer();
3044
31 var jws = std.json.writeStream(w, .{ .whitespace = .indent_1 });45 if (args.len > 0) {
3246 for (args) |name| {
33 try jws.beginObject();47 for (envars) |env| {
3448 if (mem.eql(u8, name, env.name)) {
35 try jws.objectField("zig_exe");49 try w.print("{s}\n", .{env.value});
36 try jws.write(self_exe_path);50 }
3751 }
38 try jws.objectField("lib_dir");52 }
39 try jws.write(zig_lib_directory.path.?);53 try bw.flush();
4054
41 try jws.objectField("std_dir");55 return;
42 try jws.write(zig_std_dir);56 }
4357
44 try jws.objectField("global_cache_dir");58 for (envars) |env| {
45 try jws.write(global_cache_dir);59 try w.print("{[name]s}=\"{[value]s}\"\n", env);
4660 }
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');
55 try bw.flush();61 try bw.flush();
56}62}