about summary refs log tree commit diff
path: root/src/os.zig
blob: ad9fe237a99b6a7fac33e7b34f35ee8ac4f03975 (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const std = @import("std");
const c = @cImport({
    @cInclude("stdlib.h");
    @cInclude("time.h");
});

const log = std.log.scoped(.os);

pub fn captureStdout(
    io: std.Io,
    command: []const u8,
    opts: struct {
        cwd: std.process.Child.Cwd = .inherit,
        environ_map: ?*const std.process.Environ.Map = null,
        progress_node: std.Progress.Node = std.Progress.Node.none,
    },
    w: *std.Io.Writer,
) !void {
    log.debug("Running <<< {s}", .{command});

    var p = try std.process.spawn(io, .{
        .argv = &.{ "sh", "-c", command },
        .cwd = opts.cwd,
        .stdin = .ignore,
        .stderr = .ignore,
        .stdout = .pipe,
        .environ_map = opts.environ_map,
        .progress_node = opts.progress_node,
    });

    var buf: [1024]u8 = undefined;
    var r = p.stdout.?.readerStreaming(io, &buf);

    _ = try w.sendFileAll(&r, .unlimited);
    try w.flush();

    const result = try p.wait(io);
    if (result.exited != 0) return error.NonZeroExitCode;
}

pub fn shell(
    io: std.Io,
    command: []const u8,
    opts: struct {
        cwd: std.process.Child.Cwd = .inherit,
        environ_map: ?*const std.process.Environ.Map = null,
        progress_node: std.Progress.Node = std.Progress.Node.none,
    },
) !void {
    log.debug("Running Shell <<< {s}", .{command});

    var p = try std.process.spawn(io, .{
        .argv = &.{ "sh", "-c", command },
        .cwd = opts.cwd,
        .stdin = .ignore,
        .stderr = .inherit,
        .stdout = .inherit,
        .environ_map = opts.environ_map,
        .progress_node = opts.progress_node,
    });

    const result = try p.wait(io);
    if (result.exited != 0) return error.NonZeroExitCode;
}

// use libc strftime to format time string
pub fn formatTimestamp(alloc: std.mem.Allocator, ts: std.Io.Timestamp, fmt: [*c]const u8) ![]const u8 {
    const mtime_str = try alloc.alloc(u8, std.os.linux.PATH_MAX);
    var tm: c.struct_tm = undefined;

    const sec = ts.toSeconds();
    _ = c.localtime_r(&sec, &tm);
    const rc = c.strftime(
        mtime_str.ptr,
        mtime_str.len,
        fmt,
        &tm,
    );
    switch (std.posix.errno(rc)) {
        .SUCCESS => {},
        else => |err| return std.posix.unexpectedErrno(err),
    }
    return try alloc.realloc(mtime_str, rc);
}