~gpanders/wk

wk/src/util.zig -rw-r--r-- 986 bytes
4f358b1aGregory Anders Import std.io in main.zig a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const std = @import("std");
const fs = std.fs;
const mem = std.mem;
const os = std.os;
const warn = std.debug.warn;
const ChildProcess = std.ChildProcess;

pub fn execAndCheck(allocator: *mem.Allocator, args: []const []const u8) !ChildProcess.ExecResult {
    const result = try std.ChildProcess.exec(.{
        .allocator = allocator,
        .argv = args,
    });

    switch (result.term) {
        .Exited => |code| if (code != 0) {
            warn("The following command failed with exit code {}:\n", .{code});
            for (args) |arg| warn("{} ", .{arg});
            warn("\n", .{});
            warn("{}\n", .{result.stderr});
            return error.CommandFailed;
        },
        else => {
            warn("The following command terminated unexpectedly:\n", .{});
            for (args) |arg| warn("{} ", .{arg});
            warn("\n", .{});
            warn("{}\n", .{result.stderr});
            return error.CommandFailed;
        },
    }

    return result;
}