@@ -6,9 +6,10 @@ const mkdtemp = common.mkdtemp;
6
6
const extract_file = common .extract_file ;
7
7
8
8
const squashfuse_content = @embedFile ("tools/squashfuse" );
9
- const crun_content = @embedFile ("tools/crun" );
10
9
const overlayfs_content = @embedFile ("tools/fuse-overlayfs" );
11
10
11
+ const crun_content = @embedFile ("tools/crun" );
12
+
12
13
fn getOffset (path : []const u8 ) ! u64 {
13
14
var file = try std .fs .cwd ().openFile (path , .{});
14
15
try file .seekFromEnd (-8 );
@@ -21,51 +22,85 @@ fn getOffset(path: []const u8) !u64 {
21
22
22
23
const eql = std .mem .eql ;
23
24
24
- fn processArgs (file : std.fs.File , allocator : std.mem.Allocator ) ! void {
25
- // const file = try std.fs.openFileAbsolute(path, .{ .mode = .read_write });
25
+ // inspired from std.posix.getenv
26
+ fn getEnvFull (key : []const u8 ) ? [:0 ]const u8 {
27
+ var ptr = std .c .environ ;
28
+ while (ptr [0 ]) | line | : (ptr += 1 ) {
29
+ var line_i : usize = 0 ;
30
+ while (line [line_i ] != 0 and line [line_i ] != '=' ) : (line_i += 1 ) {}
31
+ const this_key = line [0.. line_i ];
26
32
33
+ if (! std .mem .eql (u8 , this_key , key )) continue ;
34
+
35
+ return std .mem .sliceTo (line , 0 );
36
+ }
37
+ return null ;
38
+ }
39
+
40
+ fn processArgs (file : std.fs.File , allocator : std.mem.Allocator ) ! void {
27
41
var jsonReader = std .json .reader (allocator , file .reader ());
28
42
29
43
// TODO: having to specify max_value_len seems like a bug
30
44
var root_value = try std .json .Value .jsonParse (allocator , & jsonReader , .{ .max_value_len = 99999999 });
31
45
32
- const argVal = args : {
33
- switch (root_value ) {
34
- .object = > | * object | {
35
- const processVal = object .getPtr ("process" ) orelse @panic ("no process key" );
36
- switch (processVal .* ) {
37
- .object = > | * process | {
38
- const argsVal = process .getPtr ("args" ) orelse @panic ("no args key" );
39
- switch (argsVal .* ) {
40
- .array = > | * argsArr | {
41
- break :args argsArr ;
46
+ var args_json : * std .ArrayList (std .json .Value ) = undefined ;
47
+ var env_json : * std .ArrayList (std .json .Value ) = undefined ;
48
+
49
+ switch (root_value ) {
50
+ .object = > | * object | {
51
+ const processVal = object .getPtr ("process" ) orelse @panic ("no process key" );
52
+ switch (processVal .* ) {
53
+ .object = > | * process | {
54
+ const argsVal = process .getPtr ("args" ) orelse @panic ("no args key" );
55
+ switch (argsVal .* ) {
56
+ .array = > | * argsArr | {
57
+ args_json = argsArr ;
58
+ },
59
+ else = > return error .InvalidJSON ,
60
+ }
61
+
62
+ if (process .getPtr ("env" )) | envVal | {
63
+ switch (envVal .* ) {
64
+ .array = > | * envArr | {
65
+ env_json = envArr ;
42
66
},
43
67
else = > return error .InvalidJSON ,
44
68
}
45
- },
46
- else = > return error .InvalidJSON ,
47
- }
48
- },
49
- else = > return error .InvalidJSON ,
50
- }
51
- };
69
+ } else {
70
+ var array = std .json .Array .init (allocator );
71
+ env_json = & array ;
72
+ try process .put ("env" , std.json.Value { .array = array });
73
+ }
74
+ },
75
+ else = > return error .InvalidJSON ,
76
+ }
77
+ },
78
+ else = > return error .InvalidJSON ,
79
+ }
52
80
53
81
var args = std .process .args ();
54
82
_ = args .next () orelse @panic ("there should be an executable" );
55
83
56
84
while (args .next ()) | arg | {
57
- if (eql (u8 , arg , "-p" )) {
85
+ if (eql (u8 , arg , "-e" ) or eql (u8 , arg , "--env" )) {
86
+ const environment_variable = args .next () orelse @panic ("expected environment variable" );
87
+ if (std .mem .indexOfScalar (u8 , environment_variable , '=' )) | _ | {
88
+ try env_json .append (std.json.Value { .string = environment_variable });
89
+ } else {
90
+ try env_json .append (std.json.Value { .string = getEnvFull (environment_variable ) orelse @panic ("environment variable does not exist" ) });
91
+ }
92
+ } else if (eql (u8 , arg , "-p" )) {
58
93
_ = args .next ();
59
94
@panic ("not implemented" );
60
95
} else if (eql (u8 , arg , "-v" )) {
61
96
_ = args .next ();
62
97
@panic ("not implemented" );
63
98
} else if (eql (u8 , arg , "--" )) {
64
99
while (args .next ()) | arg_inner | {
65
- try argVal .append (std.json.Value { .string = arg_inner });
100
+ try args_json .append (std.json.Value { .string = arg_inner });
66
101
}
67
102
} else {
68
- try argVal .append (std.json.Value { .string = arg });
103
+ try args_json .append (std.json.Value { .string = arg });
69
104
}
70
105
}
71
106
0 commit comments