Update to Zig 0.12
continuous-integration/drone/push Build is passing Details

master
Clément FRÉVILLE 12 months ago
parent 209cec7c9d
commit 1c14166088

@ -1,6 +1,6 @@
FROM alpine:3.19 AS builder FROM alpine:3.19 AS builder
RUN apk add --no-cache zig --repository=https://dl-cdn.alpinelinux.org/alpine/edge/testing RUN apk add --no-cache zig --repository=https://dl-cdn.alpinelinux.org/alpine/edge/community
WORKDIR /work WORKDIR /work
COPY src src COPY src src

@ -19,11 +19,11 @@ pub fn build(b: *std.Build) void {
.name = "codefirst-dockerproxy-clientdrone", .name = "codefirst-dockerproxy-clientdrone",
// In this case the main source file is merely a path, however, in more // In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file. // complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = b.path("src/main.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.strip = true,
}); });
exe.strip = true;
// This declares intent for the executable to be installed into the // This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default // standard location when the user invokes the "install" step (the default

@ -24,32 +24,30 @@ const CodefirstContainer = struct {
const jsonOptions = std.json.StringifyOptions{ .emit_null_optional_fields = false }; const jsonOptions = std.json.StringifyOptions{ .emit_null_optional_fields = false };
/// Sends a request to the CodeFirst proxy and print the response body. /// Sends a request to the CodeFirst proxy and print the response body.
fn sendRequest(method: std.http.Method, auth: CodefirstAuth, path: []const u8, body: []const u8) !std.http.Status { fn sendRequest(method: std.http.Method, auth: CodefirstAuth, path: []const u8, body: ?[]const u8) !std.http.Status {
const uri = std.Uri.parse(path) catch unreachable; var response_body = std.ArrayList(u8).init(allocator);
var headers = std.http.Headers{ .allocator = allocator }; const headers = [_]std.http.Header{.{ .name = ForwardedUser, .value = auth.user }};
defer headers.deinit(); const res = try client.fetch(.{
try headers.append(ForwardedUser, auth.user); .method = method,
if (body.len > 0) { .location = .{ .url = path },
try headers.append("Content-Type", "application/json"); .headers = .{ .content_type = if (body != null) .{ .override = "application/json" } else .default },
} .extra_headers = &headers,
var req = try client.request(method, uri, headers, .{}); .payload = body,
defer req.deinit(); .response_storage = .{ .dynamic = &response_body },
req.transfer_encoding = std.http.Client.RequestTransfer{ .content_length = body.len }; });
try req.start(); if (res.status.class() == .success) {
try req.writeAll(body); std.log.info("{s}", .{response_body.items});
try req.finish(); } else {
try req.wait(); std.log.err("[HTTP {d}] {s}", .{ res.status, response_body.items });
const responseBody = req.reader().readAllAlloc(allocator, 8192) catch unreachable; }
defer allocator.free(responseBody); return res.status;
std.log.info("{s}", .{responseBody});
return req.response.status;
} }
/// Tests if a container with the given name exists. /// Tests if a container with the given name exists.
fn exists(auth: CodefirstAuth, containerName: []const u8) !bool { fn exists(auth: CodefirstAuth, containerName: []const u8) !bool {
const path = try std.fmt.allocPrint(allocator, "{s}://{s}/containers/{s}/json", .{ auth.proxyScheme, auth.proxyHost, containerName }); const path = try std.fmt.allocPrint(allocator, "{s}://{s}/containers/{s}/json", .{ auth.proxyScheme, auth.proxyHost, containerName });
defer allocator.free(path); defer allocator.free(path);
var status = try sendRequest(.GET, auth, path, ""); const status = try sendRequest(.GET, auth, path, null);
return status == .ok; return status == .ok;
} }
@ -57,7 +55,7 @@ fn exists(auth: CodefirstAuth, containerName: []const u8) !bool {
fn createImage(auth: CodefirstAuth, imageName: []const u8) !void { fn createImage(auth: CodefirstAuth, imageName: []const u8) !void {
const path = try std.fmt.allocPrint(allocator, "{s}://{s}/images/create?fromImage={s}", .{ auth.proxyScheme, auth.proxyHost, imageName }); const path = try std.fmt.allocPrint(allocator, "{s}://{s}/images/create?fromImage={s}", .{ auth.proxyScheme, auth.proxyHost, imageName });
defer allocator.free(path); defer allocator.free(path);
_ = try sendRequest(.POST, auth, path, ""); _ = try sendRequest(.POST, auth, path, null);
} }
/// Creates a container with the given name. /// Creates a container with the given name.
@ -99,21 +97,22 @@ fn start(auth: CodefirstAuth, imageName: []const u8, containerName: []const u8,
fn delete(auth: CodefirstAuth, containerName: []const u8) !void { fn delete(auth: CodefirstAuth, containerName: []const u8) !void {
const path = try std.fmt.allocPrint(allocator, "{s}://{s}/containers/{s}", .{ auth.proxyScheme, auth.proxyHost, containerName }); const path = try std.fmt.allocPrint(allocator, "{s}://{s}/containers/{s}", .{ auth.proxyScheme, auth.proxyHost, containerName });
defer allocator.free(path); defer allocator.free(path);
_ = try sendRequest(.DELETE, auth, path, ""); _ = try sendRequest(.DELETE, auth, path, null);
} }
pub fn run() !void { pub fn run() !void {
const command_name = std.os.getenv("PLUGIN_COMMAND") orelse return PluginError.InvalidCommand; const env_map = std.process.getEnvMap(allocator) catch unreachable;
const command_name = env_map.get("PLUGIN_COMMAND") orelse return PluginError.InvalidCommand;
const command = std.meta.stringToEnum(Command, command_name) orelse return PluginError.InvalidCommand; const command = std.meta.stringToEnum(Command, command_name) orelse return PluginError.InvalidCommand;
const auth = CodefirstAuth{ const auth = CodefirstAuth{
.proxyScheme = std.os.getenv("PROXYSCHEME") orelse "http", .proxyScheme = env_map.get("PROXYSCHEME") orelse "http",
.proxyHost = std.os.getenv("PROXYHOST") orelse "dockerproxy:8080", .proxyHost = env_map.get("PROXYHOST") orelse "dockerproxy:8080",
.user = std.os.getenv("DRONE_REPO_OWNER") orelse return PluginError.InvalidUser, .user = env_map.get("DRONE_REPO_OWNER") orelse return PluginError.InvalidUser,
}; };
var containerNameEnv = std.os.getenv("PLUGIN_CONTAINER") orelse return PluginError.NoContainerName; const containerNameEnv = env_map.get("PLUGIN_CONTAINER") orelse return PluginError.NoContainerName;
var shortUser = try std.mem.replaceOwned(u8, allocator, auth.user, ".", ""); const shortUser = try std.mem.replaceOwned(u8, allocator, auth.user, ".", "");
defer allocator.free(shortUser); defer allocator.free(shortUser);
var containerName = try std.fmt.allocPrint(allocator, "{s}-{s}", .{ shortUser, containerNameEnv }); const containerName = try std.fmt.allocPrint(allocator, "{s}-{s}", .{ shortUser, containerNameEnv });
defer allocator.free(containerName); defer allocator.free(containerName);
var idx: usize = 0; var idx: usize = 0;
@ -140,10 +139,10 @@ pub fn run() !void {
switch (command) { switch (command) {
.create => { .create => {
const imageName = std.os.getenv("PLUGIN_IMAGE") orelse return PluginError.InvalidImage; const imageName = env_map.get("PLUGIN_IMAGE") orelse return PluginError.InvalidImage;
const private = std.mem.eql(u8, std.os.getenv("PLUGIN_PRIVATE") orelse "false", "true"); const private = std.mem.eql(u8, env_map.get("PLUGIN_PRIVATE") orelse "false", "true");
const overwrite = std.mem.eql(u8, std.os.getenv("PLUGIN_OVERWRITE") orelse "false", "true"); const overwrite = std.mem.eql(u8, env_map.get("PLUGIN_OVERWRITE") orelse "false", "true");
const admins = std.os.getenv("PLUGIN_ADMINS") orelse auth.user; const admins = env_map.get("PLUGIN_ADMINS") orelse auth.user;
const container = CodefirstContainer{ const container = CodefirstContainer{
.Id = "", .Id = "",
@ -167,8 +166,8 @@ pub fn run() !void {
} }
}, },
.start => { .start => {
const imageName = std.os.getenv("PLUGIN_IMAGE") orelse return PluginError.InvalidImage; const imageName = env_map.get("PLUGIN_IMAGE") orelse return PluginError.InvalidImage;
const private = std.mem.eql(u8, std.os.getenv("PLUGIN_PRIVATE") orelse "false", "true"); const private = std.mem.eql(u8, env_map.get("PLUGIN_PRIVATE") orelse "false", "true");
try start(auth, imageName, containerName, envs.items, private); try start(auth, imageName, containerName, envs.items, private);
}, },
.delete => { .delete => {
@ -187,6 +186,6 @@ pub fn main() !void {
PluginError.InvalidImage => std.log.err("Invalid image", .{}), PluginError.InvalidImage => std.log.err("Invalid image", .{}),
else => std.log.err("{}", .{err}), else => std.log.err("{}", .{err}),
} }
std.os.exit(1); std.process.exit(1);
}; };
} }

Loading…
Cancel
Save