╭ main.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const cmd = @import("./command.zig"); 6 const srv = @import("./serve.zig"); 7 8 // cook the terminal before a panic/segfault trace is printed, so the trace 9 // isn't mangled by raw mode and the alternate buffer 10 pub const std_options_debug_io = xit.xitui.terminal.crash_debug_io; 11 12 pub const RunOpts = struct { 13 out: *std.Io.Writer, 14 err: *std.Io.Writer, 15 }; 16 17 pub fn main(init: std.process.Init) !u8 { 18 var debug_allocator: std.heap.DebugAllocator(.{}) = .init; 19 const allocator = if (builtin.mode == .Debug) debug_allocator.allocator() else std.heap.smp_allocator; 20 defer if (builtin.mode == .Debug) { 21 _ = debug_allocator.deinit(); 22 }; 23 24 var threaded = std.Io.Threaded.init(allocator, .{ 25 .environ = init.minimal.environ, 26 }); 27 defer threaded.deinit(); 28 const io = threaded.io(); 29 30 var args: std.ArrayList([]const u8) = .empty; 31 defer args.deinit(allocator); 32 33 var arg_it = try init.minimal.args.iterateAllocator(allocator); 34 defer arg_it.deinit(); 35 _ = arg_it.skip(); 36 while (arg_it.next()) |arg| { 37 try args.append(allocator, arg); 38 } 39 40 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 41 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 42 const run_opts = RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 43 44 const cwd_path = try std.process.currentPathAlloc(io, allocator); 45 defer allocator.free(cwd_path); 46 47 run(.xit, .{}, io, allocator, args.items, cwd_path, run_opts) catch |err| switch (err) { 48 error.HandledError => return 1, 49 else => |e| return e, 50 }; 51 52 return 0; 53 } 54 55 pub fn run( 56 comptime repo_kind: rp.RepoKind, 57 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 58 io: std.Io, 59 allocator: std.mem.Allocator, 60 args: []const []const u8, 61 cwd_path: []const u8, 62 run_opts: RunOpts, 63 ) !void { 64 var cmd_args = try cmd.CommandArgs.init(allocator, args); 65 defer cmd_args.deinit(); 66 67 switch (try cmd.CommandDispatch.init(&cmd_args)) { 68 .invalid => |invalid| switch (invalid) { 69 .command => |command| { 70 try run_opts.err.print("\"{s}\" is not a valid command\n\n", .{command}); 71 try cmd.printHelp(null, run_opts.err); 72 return error.HandledError; 73 }, 74 .argument => |argument| { 75 try run_opts.err.print("\"{s}\" is not a valid argument\n\n", .{argument.value}); 76 try cmd.printHelp(argument.command, run_opts.err); 77 return error.HandledError; 78 }, 79 }, 80 .help => |cmd_kind_maybe| try cmd.printHelp(cmd_kind_maybe, run_opts.out), 81 .cli => |cli_cmd| switch (cli_cmd) { 82 .serve => |options| try srv.run(repo_kind, any_repo_opts, io, allocator, cwd_path, options, run_opts.err, {}), 83 }, 84 } 85 } 86