aboutsummaryrefslogtreecommitdiff
path: root/src/env.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.zig')
-rw-r--r--src/env.zig30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/env.zig b/src/env.zig
index 7e8b355..bb1e8bc 100644
--- a/src/env.zig
+++ b/src/env.zig
@@ -11,7 +11,6 @@ const Self = @This();
allocator: std.mem.Allocator,
prefix: []const u8,
etc: []const u8, // ${PREFIX}/etc/
-args: *cli.Args,
config: conf.Config,
ports_dir: []const u8,
@@ -21,26 +20,30 @@ default_category: ?[]const u8,
default_repo_branch: []const u8,
editor_cmd: ?[]const u8,
-pub fn init(allocator: std.mem.Allocator, etc: []const u8, args: *cli.Args) Self {
+_proc_env: std.process.Environ,
+
+pub fn init(allocator: std.mem.Allocator, env: std.process.Environ, etc: []const u8) Self {
const prefix = if (options.path_prefix[options.path_prefix.len - 1] == '/')
options.path_prefix[0 .. options.path_prefix.len - 1]
else
options.path_prefix;
- const config = read_config(allocator, .join(.{ etc, "porteur/porteur.conf" }));
+ const path: fs.Path = .join(.{ etc, "porteur/porteur.conf" });
+ const config = read_config(allocator, &path);
var self: Self = .{
.allocator = allocator,
.prefix = prefix,
.etc = etc,
- .args = args,
.config = config,
- .ports_dir = std.posix.getenvZ("PORTSDIR") orelse "/usr/ports",
+ .ports_dir = env.getPosix("PORTSDIR") orelse "/usr/ports",
.distfiles_dir = prefix ++ "/porteur/distfiles/{{TREENAME}}",
.distfiles_history = 1,
.default_category = null,
.default_repo_branch = "main",
.editor_cmd = null,
+
+ ._proc_env = env,
};
var iter = config.iterate();
@@ -64,7 +67,7 @@ pub fn init(allocator: std.mem.Allocator, etc: []const u8, args: *cli.Args) Self
}
res = a[0];
}
- break :history res;
+ break :history @max(1, res);
};
} else if (std.mem.eql(u8, v.key, "category") and v.val.len > 0) {
self.default_category = v.val;
@@ -105,7 +108,7 @@ pub fn ports_dist_dir(self: Self, tree_name: []const u8, port_name: []const u8)
.{ .key = "{{TREENAME}}", .val = tree_name },
.{ .key = "{{PORTNAME}}", .val = port_name },
}) |v| {
- if (std.mem.indexOf(u8, src, v.key)) |idx| {
+ if (std.mem.find(u8, src, v.key)) |idx| {
@memcpy(dest.buf[dest.len .. dest.len + idx], src[0..idx]);
dest.len += idx;
@memcpy(dest.buf[dest.len .. dest.len + v.val.len], v.val);
@@ -139,6 +142,11 @@ pub fn work_path(self: Self, subpath: anytype) fs.Path {
return p;
}
+/// Return the value of process' environment variable with the given name.
+pub fn get(self: Self, name: []const u8) ?[:0]const u8 {
+ return self._proc_env.getPosix(name);
+}
+
pub fn alloc(self: Self, size: usize) []u8 {
return self.allocator.alloc(u8, size) catch cli.fatal("out of memory", .{});
}
@@ -147,6 +155,10 @@ pub fn dealloc(self: Self, bytes: []const u8) void {
self.allocator.free(bytes);
}
+pub fn dupe(self: Self, bytes: []const u8) []u8 {
+ return self.allocator.dupe(u8, bytes) catch cli.fatal("out of memory", .{});
+}
+
pub fn err(self: Self, comptime fmt: []const u8, args: anytype) void {
_ = self;
cli.stderr.write_line("Error: " ++ fmt, args);
@@ -165,8 +177,8 @@ pub fn info(self: Self, comptime fmt: []const u8, args: anytype) void {
cli.stdout.flush();
}
-fn read_config(allocator: std.mem.Allocator, conf_file: fs.Path) conf.Config {
- if (conf.Config.from_file(allocator, conf_file.name())) |res| {
+fn read_config(allocator: std.mem.Allocator, conf_file: *const fs.Path) conf.Config {
+ if (conf.Config.from_file(allocator, conf_file)) |res| {
return switch (res) {
.conf => |c| c,
.err => |e| empty: {