diff options
| author | Robert Günzler <r@gnzler.io> | 2026-06-24 11:57:02 +0900 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2026-09-22 18:01:30 +0200 |
| commit | c677e8873e0eae790a1b8b07844a10efa9f63a1e (patch) | |
| tree | 95bc9709c374d27962a73712d24807a98cc69bb4 /src/os.zig | |
| parent | fcdf352af595759aa34e6ed037ee10810867a616 (diff) | |
| download | site-c677e8873e0eae790a1b8b07844a10efa9f63a1e.tar.gz | |
wip design for v2
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
| -rw-r--r-- | src/os.zig | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/os.zig b/src/os.zig new file mode 100644 index 0000000..ad9fe23 --- /dev/null +++ b/src/os.zig @@ -0,0 +1,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); +} |