Skip to content

Commit 13f51da

Browse files
committed
Add support for -e/--env
1 parent 8724698 commit 13f51da

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ Skopeo is used for loading images, for other locations refer to [its documentati
2222
- [X] x86_64 support
2323
- [ ] arm64 support
2424
- [X] Supports arguments
25-
- [ ] Support `-p`
25+
- [X] [Supports environment variables specifying environment variables][2]
2626
- [ ] Support `-v`
2727
- [ ] Support other [arguments][0]...
2828

2929
[0]: https://docs.docker.com/engine/reference/commandline/container_run/
3030
[1]: https://github.com/containers/skopeo/blob/main/docs/skopeo.1.md#image-names
31+
[2]: https://docs.docker.com/reference/cli/docker/container/run/#env

src/main.zig

+58-23
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const mkdtemp = common.mkdtemp;
66
const extract_file = common.extract_file;
77

88
const squashfuse_content = @embedFile("tools/squashfuse");
9-
const crun_content = @embedFile("tools/crun");
109
const overlayfs_content = @embedFile("tools/fuse-overlayfs");
1110

11+
const crun_content = @embedFile("tools/crun");
12+
1213
fn getOffset(path: []const u8) !u64 {
1314
var file = try std.fs.cwd().openFile(path, .{});
1415
try file.seekFromEnd(-8);
@@ -21,51 +22,85 @@ fn getOffset(path: []const u8) !u64 {
2122

2223
const eql = std.mem.eql;
2324

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];
2632

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 {
2741
var jsonReader = std.json.reader(allocator, file.reader());
2842

2943
// TODO: having to specify max_value_len seems like a bug
3044
var root_value = try std.json.Value.jsonParse(allocator, &jsonReader, .{ .max_value_len = 99999999 });
3145

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;
4266
},
4367
else => return error.InvalidJSON,
4468
}
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+
}
5280

5381
var args = std.process.args();
5482
_ = args.next() orelse @panic("there should be an executable");
5583

5684
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")) {
5893
_ = args.next();
5994
@panic("not implemented");
6095
} else if (eql(u8, arg, "-v")) {
6196
_ = args.next();
6297
@panic("not implemented");
6398
} else if (eql(u8, arg, "--")) {
6499
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 });
66101
}
67102
} else {
68-
try argVal.append(std.json.Value{ .string = arg });
103+
try args_json.append(std.json.Value{ .string = arg });
69104
}
70105
}
71106

0 commit comments

Comments
 (0)