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