srctree

Gregory Mullen parent 1271da08 e4d4a8da
add overwrite index option

inlinesplit
src/main.zig added: 34, removed: 4, total 30
@@ -1,8 +1,27 @@
pub fn main() !void {
const a = std.heap.page_allocator;
if (std.fs.cwd().openFile("index.html", .{})) |_| {
std.debug.print("Delete index.html then rerun imgzen\n", .{});
std.posix.exit(1);
 
var overwrite_index: bool = false;
var args = std.process.args();
_ = args.next(); // arg0
while (args.next()) |arg| {
if (std.mem.eql(u8, arg, "--overwrite-index")) {
overwrite_index = true;
} else {
std.debug.print("Unexpected argument {s}\n", .{arg});
std.posix.exit(2);
}
}
 
if (std.fs.cwd().openFile("index.html", .{})) |f| {
defer f.close();
if (!overwrite_index or !try imgzenIndex(f)) {
if (overwrite_index) {
std.debug.print("index.html file header doesn't match expected file\n", .{});
}
std.debug.print("Delete index.html then rerun imgzen\n", .{});
std.posix.exit(1);
}
} else |_| {}
 
var file = try std.fs.cwd().createFile("index.html", .{});
@@ -36,6 +55,17 @@ pub fn main() !void {
try bout.writeAll(index_tail);
}
 
fn usage() !void {
// TODO write usage
}
 
fn imgzenIndex(f: std.fs.File) !bool {
var buffer: [index.len]u8 = undefined;
const count = try f.read(&buffer);
if (count != buffer.len) return false;
return std.mem.eql(u8, buffer[0..], index[0..]);
}
 
fn isVideo(str: []const u8) bool {
return std.mem.endsWith(u8, str, ".mp4");
}