1
Fork 0

read environment variables well

This commit is contained in:
Motiejus Jakštys 2022-07-14 21:30:36 +03:00
parent cf6cf0c671
commit 7192d0d333
1 changed files with 48 additions and 53 deletions

View File

@ -22,8 +22,11 @@ const c = @cImport({
@cInclude("nss.h"); @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_DB = "TURBONSS_DB";
const ENV_LOGLEVEL = "TURBONSS_LOGLEVEL"; const ENV_VERBOSE = "TURBONSS_VERBOSE";
const ENV_OMIT_MEMBERS = "TURBONSS_OMIT_MEMBERS"; const ENV_OMIT_MEMBERS = "TURBONSS_OMIT_MEMBERS";
export var turbonss_db_path: [:0]const u8 = "/etc/turbonss/db.turbo"; 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 // assigns State from environment variables et al
fn init() void { fn init() void {
//const verbose = blk: { const verbose = blk: {
// if (os.getenvZ(ENV_LOGLEVEL)) |env| { if (getenv(ENV_VERBOSE)) |env| {
// const got = mem.sliceTo(env, 0); const got = mem.sliceTo(env, 0);
// if (mem.eql(u8, got, "0")) { if (mem.eql(u8, got, "0")) {
// break :blk false; break :blk false;
// } else if (mem.eql(u8, got, "1")) { } else if (mem.eql(u8, got, "1")) {
// break :blk true; break :blk true;
// } else { } else {
// std.debug.print( std.debug.print(
// "warning: unrecognized {s}={s}. Expected between 0 or 1\n", "warning: unrecognized {s}={s}. Expected between 0 or 1\n",
// .{ ENV_LOGLEVEL, got }, .{ ENV_VERBOSE, got },
// ); );
// } }
// } }
// break :blk false; break :blk false;
//}; };
//const omit_members = blk: { const omit_members = blk: {
// if (os.getenvZ(ENV_OMIT_MEMBERS)) |env| { if (getenv(ENV_OMIT_MEMBERS)) |env| {
// const got = mem.sliceTo(env, 0); const got = mem.sliceTo(env, 0);
// if (mem.eql(u8, got, "1")) { if (mem.eql(u8, got, "1")) {
// break :blk true; break :blk true;
// } else if (mem.eql(u8, got, "0")) { } else if (mem.eql(u8, got, "0")) {
// break :blk false; break :blk false;
// } else if (mem.eql(u8, got, "auto")) { } else if (mem.eql(u8, got, "auto")) {
// // not set, do autodiscover // not set, do autodiscover
// } else { } else {
// std.debug.print( std.debug.print(
// "warning: unrecognized {s}={s}. Expected 0, 1 or auto\n", "warning: unrecognized {s}={s}. Expected 0, 1 or auto\n",
// .{ ENV_OMIT_MEMBERS, got }, .{ ENV_OMIT_MEMBERS, got },
// ); );
// } }
// } }
// if (os.argv.len == 0) break :blk false; //if (os.argv.len == 0) break :blk false;
// break :blk mem.eql(u8, mem.sliceTo(os.argv[0], 0), "id"); //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; const fname = if (getenv(ENV_DB)) |env|
//if (verbose) mem.sliceTo(env, 0)
// std.debug.print("opening '{s}'\n", .{fname}); else
const fname = turbonss_db_path; 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 file = File.open(fname) catch |err| { const file = File.open(fname) catch |err| {
if (verbose) if (verbose)
@ -110,8 +103,10 @@ fn init() void {
return; return;
}; };
if (verbose) if (verbose) {
std.debug.print("turbonss database opened\n", .{}); std.debug.print("turbonss database '{s}' opened\n", .{fname});
std.debug.print("omitting members from getgr* calls: {any}\n", .{omit_members});
}
global_state = State{ global_state = State{
.file = file, .file = file,