read environment variables well

This commit is contained in:
Motiejus Jakštys 2022-07-14 21:30:36 +03:00
parent cf6cf0c671
commit 7192d0d333

View File

@ -22,8 +22,11 @@ const c = @cImport({
@cInclude("nss.h");
});
// cannot use os.getenv due to ziglang/zig#4524
extern fn getenv([*:0]const u8) ?[*:0]const u8;
const ENV_DB = "TURBONSS_DB";
const ENV_LOGLEVEL = "TURBONSS_LOGLEVEL";
const ENV_VERBOSE = "TURBONSS_VERBOSE";
const ENV_OMIT_MEMBERS = "TURBONSS_OMIT_MEMBERS";
export var turbonss_db_path: [:0]const u8 = "/etc/turbonss/db.turbo";
@ -51,58 +54,48 @@ var global_init = once(init);
// assigns State from environment variables et al
fn init() void {
//const verbose = blk: {
// if (os.getenvZ(ENV_LOGLEVEL)) |env| {
// const got = mem.sliceTo(env, 0);
// if (mem.eql(u8, got, "0")) {
// break :blk false;
// } else if (mem.eql(u8, got, "1")) {
// break :blk true;
// } else {
// std.debug.print(
// "warning: unrecognized {s}={s}. Expected between 0 or 1\n",
// .{ ENV_LOGLEVEL, got },
// );
// }
// }
// break :blk false;
//};
const verbose = blk: {
if (getenv(ENV_VERBOSE)) |env| {
const got = mem.sliceTo(env, 0);
if (mem.eql(u8, got, "0")) {
break :blk false;
} else if (mem.eql(u8, got, "1")) {
break :blk true;
} else {
std.debug.print(
"warning: unrecognized {s}={s}. Expected between 0 or 1\n",
.{ ENV_VERBOSE, got },
);
}
}
break :blk false;
};
//const omit_members = blk: {
// if (os.getenvZ(ENV_OMIT_MEMBERS)) |env| {
// const got = mem.sliceTo(env, 0);
// if (mem.eql(u8, got, "1")) {
// break :blk true;
// } else if (mem.eql(u8, got, "0")) {
// break :blk false;
// } else if (mem.eql(u8, got, "auto")) {
// // not set, do autodiscover
// } else {
// std.debug.print(
// "warning: unrecognized {s}={s}. Expected 0, 1 or auto\n",
// .{ ENV_OMIT_MEMBERS, got },
// );
// }
// }
const omit_members = blk: {
if (getenv(ENV_OMIT_MEMBERS)) |env| {
const got = mem.sliceTo(env, 0);
if (mem.eql(u8, got, "1")) {
break :blk true;
} else if (mem.eql(u8, got, "0")) {
break :blk false;
} else if (mem.eql(u8, got, "auto")) {
// not set, do autodiscover
} else {
std.debug.print(
"warning: unrecognized {s}={s}. Expected 0, 1 or auto\n",
.{ ENV_OMIT_MEMBERS, got },
);
}
}
//if (os.argv.len == 0) break :blk false;
//break :blk mem.eql(u8, mem.sliceTo(os.argv[0], 0), "id");
//};
break :blk false; // https://github.com/ziglang/zig/issues/4524#issuecomment-1184748756
};
//const fname = os.getenvZ(ENV_DB) orelse turbonss_db_path;
//if (verbose)
// std.debug.print("opening '{s}'\n", .{fname});
const fname = turbonss_db_path;
const verbose = false;
const omit_members = false;
// TODO: neither getenvZ nor os.argv work as expected on a shared library.
//const omit_members = if (os.argv.len == 0)
// false
//else
// mem.eql(u8, mem.sliceTo(os.argv[0], 0), "id");
if (verbose)
std.debug.print("omitting members from getgr* calls: {any}\n", .{omit_members});
const fname = if (getenv(ENV_DB)) |env|
mem.sliceTo(env, 0)
else
turbonss_db_path;
const file = File.open(fname) catch |err| {
if (verbose)
@ -110,8 +103,10 @@ fn init() void {
return;
};
if (verbose)
std.debug.print("turbonss database opened\n", .{});
if (verbose) {
std.debug.print("turbonss database '{s}' opened\n", .{fname});
std.debug.print("omitting members from getgr* calls: {any}\n", .{omit_members});
}
global_state = State{
.file = file,