//! csv2journal command-line entry point (M1-CLI-006). const std = @import("std"); const csv2journal = @import("csv2journal"); /// Debug calls are compiled in whatever the build mode; `log.configure` /// chooses at startup which of them are written. pub const std_options: std.Options = .{ .log_level = .debug, .logFn = csv2journal.log.logFn, }; /// Everything `main` can propagate: argument and arena allocation, writing the /// usage text, and flushing stdout. const Error = error{ OutOfMemory, Unexpected, WriteFailed } || std.Io.File.Writer.Error; pub fn main(init: std.process.Init) Error!u8 { // The process arena lives exactly as long as the run, which is also how // long every snapshot and derived value needs to live (M1-PROJ-015). const arena = init.arena.allocator(); const argv = try init.minimal.args.toSlice(arena); const args = try arena.alloc([]const u8, argv.len); for (argv, 0..) |argument, index| args[index] = argument; const invocation = csv2journal.cli.Invocation.parse(args) catch { try emit(init.io, std.Io.File.stderr(), csv2journal.cli.usage); return 2; }; csv2journal.log.configure(init.environ_map.get("CSV2JOURNAL_LOG")) catch { try emit(init.io, std.Io.File.stderr(), "CSV2JOURNAL_LOG must be info or debug\n"); return 2; }; var buffer: [4096]u8 = undefined; var stdout_writer = std.Io.File.stdout().writer(init.io, &buffer); const failure = try csv2journal.cli.execute(arena, init.io, &stdout_writer.interface, std.Io.Dir.cwd(), invocation); try stdout_writer.flush(); return if (failure) 1 else 0; } fn emit(io: std.Io, file: std.Io.File, text: []const u8) !void { var buffer: [4096]u8 = undefined; var file_writer = file.writer(io, &buffer); try file_writer.interface.writeAll(text); try file_writer.interface.flush(); }