Skip to content

dayvster/zdotenv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zdotenv

License: MIT zig Docs

A simple Zig library for loading environment variables from .env files.

Installation

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,
});

Features

  • Loads key-value pairs from one or more .env files
  • Ignores comments and blank lines
  • Provides easy access to environment variables via a hash map
  • Memory-safe: all allocations are freed with deinit

Usage

Example

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

API

  • Dotenv.init(allocator) — create a new instance
  • Dotenv.load(paths) — load one or more .env files
  • Dotenv.get(key) — get the value for a key
  • Dotenv.deinit() — free all memory

Testing

Run all tests:

zig build test

License

LICENSE

About

A simple Zig library for loading environment variables from .env files.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages