File tree 5 files changed +53
-5
lines changed
5 files changed +53
-5
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,12 @@ Add zdotenv to your zig project:
11
11
zig fetch --save https://github.com/BitlyTwiser/zdotenv/archive/refs/tags/0.1.0.tar.gz
12
12
```
13
13
14
+ Add to build file:
15
+ ```
16
+ const zdotenv = b.dependency("zdotenv", .{});
17
+ exe.root_module.addImport("zdotenv", ymlz.module("root"));
18
+ ```
19
+
14
20
Zdotenv has 2 pathways:
15
21
16
22
1 . Absolute path to .env
@@ -28,6 +34,31 @@ const z = try Zdotenv.init(std.heap.page_allocator);
28
34
try z.load();
29
35
```
30
36
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
+
31
62
## C usage:
32
63
Zig (at the time of this writing) does not have a solid way of directly adjusting the env variables. Doing things like:
33
64
```
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ pub fn build(b: *std.Build) void {
12
12
.target = target ,
13
13
.optimize = optimize ,
14
14
});
15
+
16
+ // For setting env vars using C
15
17
exe .linkLibC ();
16
18
b .installArtifact (exe );
17
19
Original file line number Diff line number Diff line change @@ -50,7 +50,6 @@ pub const Zdotenv = struct {
50
50
51
51
return ;
52
52
};
53
- // defer file.close();
54
53
55
54
// Set file
56
55
self .file = file ;
@@ -73,7 +72,6 @@ pub const Zdotenv = struct {
73
72
74
73
return ;
75
74
};
76
- // defer file.close();
77
75
78
76
//Set file
79
77
self .file = file ;
@@ -94,6 +92,7 @@ pub const Zdotenv = struct {
94
92
// Dupe strings with terminating zero for C
95
93
const key_z = try self .allocator .dupeZ (u8 , entry .key_ptr .* );
96
94
const value_z = try self .allocator .dupeZ (u8 , entry .value_ptr .* );
95
+
97
96
if (setenv (key_z , value_z , 1 ) != 0 ) {
98
97
std .debug .print ("Failed to set env var\n " , .{});
99
98
return ;
Original file line number Diff line number Diff line change 1
1
// Lib.zig is the package interface where all modules are collected for export
2
2
3
- pub const parser = @import ("parser.zig" );
3
+ const parser = @import ("parser.zig" );
4
4
pub const Parser = parser .Parser ;
5
5
6
- pub const zdotenv = @import ("dotenv.zig" );
6
+ const zdotenv = @import ("dotenv.zig" );
7
7
pub const Zdotenv = zdotenv .Zdotenv ;
Original file line number Diff line number Diff line change 1
1
const std = @import ("std" );
2
+ const zdotenv = @import ("lib.zig" ).Zdotenv ;
3
+ const assert = std .debug .assert ;
2
4
3
5
/// 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
+ }
You can’t perform that action at this time.
0 commit comments