This commit is contained in:
Lucas F. 2025-12-25 15:00:11 -03:00
commit a78e6b3cc9
7 changed files with 394 additions and 0 deletions

86
README.md Normal file
View file

@ -0,0 +1,86 @@
# zig-dotenv
A simple and lightweight library for loading `.env` files in Zig projects.
- **Zero dependencies** — only the Zig standard library
- **Safe** — proper error handling and automatic memory cleanup
- **Convenient**`get`, `getOr`, and `require` methods for easy access
- **Well-tested** — full unit test coverage
## Installation
Add the package to your project with `zig fetch`:
```bash
zig fetch --save https://git.lucasf.xyz/public/zig-dotenv/archive/0.1.0.tar.gz
```
Then, in your `build.zig`:
```zig
const std = @import("std");
pub fn build(b: *std.Build) void {
...
const exe = b.addExecutable(.{ ... });
...
const dotenv_dep = b.dependency("zigdotenv", .{});
exe.root_module.addImport("zigdotenv", dotenv_dep.module("zigdotenv"));
}
```
## Basic Usage
```zig
const std = @import("std");
const dotenv = @import("zigdotenv");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Load the .env file (fails on unexpected errors)
var env = try dotenv.DotEnv.load(allocator, ".env");
defer env.deinit();
// Simple access
if (env.get("API_KEY")) |key| {
std.debug.print("API_KEY = {s}\n", .{key});
}
// With fallback default
const debug = env.getOr("DEBUG", "false");
std.debug.print("DEBUG = {s}\n", .{debug});
// Required variable (fails if missing)
const database_url = try env.require("DATABASE_URL");
std.debug.print("DB URL = {s}\n", .{database_url});
}
```
## Supported format
The parser supports the standard `.env` format:
```env
# Comments are ignored
API_KEY=supersecret123
DATABASE_URL=postgres://localhost:5432/myapp
# Spaces are trimmed
DEBUG = true
# Single or double quotes are stripped
QUOTED="value with spaces"
SINGLE='another value'
# Empty value
EMPTY=
```
## License
MIT © Lucas F.
___
Feito com ❤️ em Zig. Enjoy! 🚀