repl: handle when original file doesn't exist
This commit is contained in:
parent
c05be26dbd
commit
a18809eb5b
1 changed files with 9 additions and 4 deletions
13
src/main.zig
13
src/main.zig
|
@ -27,8 +27,11 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
|
||||||
const stdout = &stdout_file.outStream().stream;
|
const stdout = &stdout_file.outStream().stream;
|
||||||
const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path"));
|
const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path"));
|
||||||
|
|
||||||
var file_read = try std.fs.File.openRead(scri_path);
|
var file_read_opt: ?std.fs.File = std.fs.File.openRead(scri_path) catch |err| blk: {
|
||||||
const total_bytes = try file_read.getEndPos();
|
if (err == error.FileNotFound) break :blk null;
|
||||||
|
return err;
|
||||||
|
};
|
||||||
|
const total_bytes = if (file_read_opt) |file_read| try file_read.getEndPos() else 0;
|
||||||
|
|
||||||
var cmds = langs.CommandList.init(allocator);
|
var cmds = langs.CommandList.init(allocator);
|
||||||
defer cmds.deinit();
|
defer cmds.deinit();
|
||||||
|
@ -41,7 +44,7 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
|
||||||
// existing_cmds, and then passed along to cmds),
|
// existing_cmds, and then passed along to cmds),
|
||||||
// we can't defer them here
|
// we can't defer them here
|
||||||
var scri_existing = try allocator.alloc(u8, total_bytes);
|
var scri_existing = try allocator.alloc(u8, total_bytes);
|
||||||
_ = try file_read.read(scri_existing);
|
_ = try file_read_opt.?.read(scri_existing);
|
||||||
|
|
||||||
// we can defer this because we copy the Command structs back to cmds
|
// we can defer this because we copy the Command structs back to cmds
|
||||||
var existing_cmds = try lang.parse(scri_existing);
|
var existing_cmds = try lang.parse(scri_existing);
|
||||||
|
@ -64,7 +67,9 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
file_read.close();
|
if (file_read_opt) |file_read| {
|
||||||
|
file_read.close();
|
||||||
|
}
|
||||||
|
|
||||||
var file = try std.fs.File.openWrite(scri_path);
|
var file = try std.fs.File.openWrite(scri_path);
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
Loading…
Reference in a new issue