wip handling errors in top level

This commit is contained in:
Motiejus Jakštys 2022-06-22 16:23:02 +03:00
parent 62b643dbc2
commit 6aa4b01692
2 changed files with 27 additions and 15 deletions

View File

@ -22,8 +22,8 @@ pub fn write(self: *ErrCtx, bytes: []const u8) error{}!usize {
self.overflow = bytes.len > can_add;
self.buf.appendSliceAssumeCapacity(bytes[0..math.min(bytes.len, can_add)]);
// not adding the final zero is ok, because it needs
// to be ignored in the iterator anyway.
// not adding the final zero is ok, because it will
// be ignored in the iterator anyway.
_ = self.buf.append(0) catch null;
return bytes.len;

View File

@ -40,25 +40,25 @@ fn execute(
allocator: Allocator,
stderr: anytype,
argv: []const [*:0]const u8,
) !u8 {
) u8 {
const result = flags.parse(argv, &[_]flags.Flag{
.{ .name = "-h", .kind = .boolean },
.{ .name = "--passwd", .kind = .arg },
.{ .name = "--group", .kind = .arg },
.{ .name = "--output", .kind = .arg },
}) catch {
try stderr.writeAll(usage);
stderr.writeAll(usage) catch {};
return 1;
};
if (result.boolFlag("-h")) {
try io.getStdOut().writeAll(usage);
io.getStdOut().writeAll(usage) catch return 1;
return 0;
}
if (result.args.len != 0) {
try stderr.print("ERROR: unknown option '{s}'\n", .{result.args[0]});
try stderr.writeAll(usage);
stderr.print("ERROR: unknown option '{s}'\n", .{result.args[0]}) catch {};
stderr.writeAll(usage) catch {};
return 1;
}
@ -66,10 +66,20 @@ fn execute(
const groupFname = result.argFlag("--group") orelse "./group";
const outFile = result.argFlag("--output") orelse "./db.turbo";
//std.debug.print("passwd file name: {s}\n", .{passwdFname});
var passwdFile = try fs.cwd().openFile(passwdFname, .{ .mode = .read_only });
// to catch a specific file.OpenError, wait for
// https://github.com/ziglang/zig/issues/2473
var passwdFile = fs.cwd().openFile(
passwdFname,
.{ .mode = .read_only },
) catch |err| {
stderr.print("Error opening {s}: {s}\n", .{ passwdFname, @errorName(err) }) catch {};
return 1;
};
defer passwdFile.close();
var groupFile = try fs.cwd().openFile(groupFname, .{ .mode = .read_only });
var groupFile = fs.cwd().openFile(groupFname, .{ .mode = .read_only }) catch |err| {
stderr.print("Error opening {s}: {s}\n", .{ groupFname, @errorName(err) }) catch {};
return 1;
};
defer groupFile.close();
var err_ctx = ErrCtx{};
@ -79,9 +89,11 @@ fn execute(
passwdFile.reader(),
);
errdefer {
var it = err_ctx.iterator();
stderr.print("ERROR", .{}) catch {};
var it = err_ctx.rev();
while (it.next()) |err|
std.debug.print("ERROR: {s}\n", .{err});
stderr.print(": {s}", .{err}) catch {};
stderr.print("\n", .{}) catch {};
}
defer for (users) |*user| user.deinit(allocator);
defer allocator.free(users);
@ -118,7 +130,7 @@ test "invalid argument" {
var stderr = ArrayList(u8).init(allocator);
defer stderr.deinit();
const exit_code = try execute(allocator, stderr.writer(), args[0..]);
const exit_code = execute(allocator, stderr.writer(), args[0..]);
try testing.expectEqual(@as(u8, 1), exit_code);
try testing.expect(mem.startsWith(
u8,
@ -128,8 +140,6 @@ test "invalid argument" {
}
test "smoke test" {
if (true) return error.SkipZigTest;
const allocator = testing.allocator;
var stderr = ArrayList(u8).init(allocator);
defer stderr.deinit();
@ -181,6 +191,8 @@ test "smoke test" {
"--group", groupPath,
"--output", outPath,
};
if (true) return error.SkipZigTest;
const exit_code = try execute(allocator, stderr.writer(), args);
try testing.expectEqual(@as(u8, 0), exit_code);