dayvster/zdotenv
A simple Zig library for loading environment variables from .env files.
A simple Zig library for loading environment variables from .env
files.
Fetch the package with Zig:
zig fetch --save git+https://github.com/dayvster/zdotenv
Then add it as a module in your build.zig
:
const dotenv_mod = b.addModule("dotenv", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
});
.env
filesdeinit
See examples/basic.zig
:
const std = @import("std");
const Dotenv = @import("dotenv").Dotenv;
pub fn main() !void {
const allocator = std.heap.page_allocator;
var dotenv = Dotenv.init(allocator);
defer dotenv.deinit();
try dotenv.load(&[_][]const u8{"./examples/example.env"});
if (dotenv.get("FOO")) |val| {
std.debug.print("FOO={s}\n", .{val});
} else {
std.debug.print("FOO not found\n", .{});
}
}
With an example.env
file:
FOO=bar
BAZ=qux
Dotenv.init(allocator)
— create a new instanceDotenv.load(paths)
— load one or more .env
filesDotenv.get(key)
— get the value for a keyDotenv.deinit()
— free all memoryRun all tests:
zig build test