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