aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorManlio Perillo <manlio.perillo@gmail.com>2023-01-23 11:28:32 +0100
committerAndrew Kelley <andrew@ziglang.org>2023-10-17 20:10:23 -0700
commit053119083c2c93cb1fc4129bc647c03301f4010d (patch)
tree242484e01291155621e8655635fa9e4276eb03b1 /src
parent7a9500fd80990a3dc47821e627162bf769eecbba (diff)
downloadzig-053119083c2c93cb1fc4129bc647c03301f4010d.tar.gz
zig-053119083c2c93cb1fc4129bc647c03301f4010d.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/print_env.zig56
1 files changed, 31 insertions, 25 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 });
-
- try jws.beginObject();
-
- 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');
+ 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();
+
+ return;
+ }
+
+ for (envars) |env| {
+ try w.print("{[name]s}=\"{[value]s}\"\n", env);
+ }
try bw.flush();
}