Skip to content

Commit bda821e

Browse files
committed
Adding some more examples and removing dead lines
1 parent c51da2a commit bda821e

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Add zdotenv to your zig project:
1111
zig fetch --save https://github.com/BitlyTwiser/zdotenv/archive/refs/tags/0.1.0.tar.gz
1212
```
1313

14+
Add to build file:
15+
```
16+
const zdotenv = b.dependency("zdotenv", .{});
17+
exe.root_module.addImport("zdotenv", ymlz.module("root"));
18+
```
19+
1420
Zdotenv has 2 pathways:
1521

1622
1. Absolute path to .env
@@ -28,6 +34,31 @@ const z = try Zdotenv.init(std.heap.page_allocator);
2834
try z.load();
2935
```
3036

37+
Example from Main:
38+
```
39+
const std = @import("std");
40+
const zdotenv = @import("lib.zig");
41+
const assert = std.debug.assert;
42+
43+
/// The binary main is used for testing the package to showcase the API
44+
pub fn main() !void {
45+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
46+
defer arena.deinit();
47+
const allocator = arena.allocator();
48+
49+
var dotenv = try zdotenv.Zdotenv.init(allocator);
50+
try dotenv.load();
51+
52+
const env_map = try std.process.getEnvMap(allocator);
53+
const pass = env_map.get("PASSWORD") orelse "foobar";
54+
55+
assert(std.mem.eql(u8, pass, "I AM ALIVE!!"));
56+
}
57+
58+
```
59+
60+
Importing this into your own library, you will use `@import("zdotenv")`. Otherwise, this would be the same :)
61+
3162
## C usage:
3263
Zig (at the time of this writing) does not have a solid way of directly adjusting the env variables. Doing things like:
3364
```

build.zig

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub fn build(b: *std.Build) void {
1212
.target = target,
1313
.optimize = optimize,
1414
});
15+
16+
// For setting env vars using C
1517
exe.linkLibC();
1618
b.installArtifact(exe);
1719

src/dotenv.zig

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ pub const Zdotenv = struct {
5050

5151
return;
5252
};
53-
// defer file.close();
5453

5554
// Set file
5655
self.file = file;
@@ -73,7 +72,6 @@ pub const Zdotenv = struct {
7372

7473
return;
7574
};
76-
// defer file.close();
7775

7876
//Set file
7977
self.file = file;
@@ -94,6 +92,7 @@ pub const Zdotenv = struct {
9492
// Dupe strings with terminating zero for C
9593
const key_z = try self.allocator.dupeZ(u8, entry.key_ptr.*);
9694
const value_z = try self.allocator.dupeZ(u8, entry.value_ptr.*);
95+
9796
if (setenv(key_z, value_z, 1) != 0) {
9897
std.debug.print("Failed to set env var\n", .{});
9998
return;

src/lib.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Lib.zig is the package interface where all modules are collected for export
22

3-
pub const parser = @import("parser.zig");
3+
const parser = @import("parser.zig");
44
pub const Parser = parser.Parser;
55

6-
pub const zdotenv = @import("dotenv.zig");
6+
const zdotenv = @import("dotenv.zig");
77
pub const Zdotenv = zdotenv.Zdotenv;

src/main.zig

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
const std = @import("std");
2+
const zdotenv = @import("lib.zig").Zdotenv;
3+
const assert = std.debug.assert;
24

35
/// The binary main is used for testing the package to showcase the API
4-
pub fn main() !void {}
6+
pub fn main() !void {
7+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
8+
defer arena.deinit();
9+
const allocator = arena.allocator();
10+
11+
var dotenv = try zdotenv.init(allocator);
12+
try dotenv.load();
13+
14+
const env_map = try std.process.getEnvMap(allocator);
15+
const pass = env_map.get("PASSWORD_ENV") orelse "foobar";
16+
17+
std.debug.print("{s}", .{pass});
18+
19+
assert(std.mem.eql(u8, pass, "I AM ALIVE!!"));
20+
}

0 commit comments

Comments
 (0)