╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const hash = xit.hash; 5 6 pub const CommandKind = enum { 7 serve, 8 ssh_helper, 9 }; 10 11 const Help = struct { 12 name: []const u8, 13 descrip: []const u8, 14 example: []const u8, 15 }; 16 17 fn commandHelp(command_kind: CommandKind) Help { 18 return switch (command_kind) { 19 .serve => .{ 20 .name = "serve", 21 .descrip = 22 \\a long-running server forwarding receive-pack and upload-pack. 23 , 24 .example = 25 \\haxy serve --http-listen 127.0.0.1:8080 --ssh-listen 127.0.0.1:8081 --data-dir /srv/git 26 , 27 }, 28 .ssh_helper => .{ 29 .name = "ssh-helper", 30 .descrip = 31 \\a helper run by sshd that forwards Git SSH service requests to serve. 32 , 33 .example = 34 \\haxy ssh-helper --ssh-connect 127.0.0.1:8081 --service upload-pack <directory> 35 , 36 }, 37 }; 38 } 39 40 pub fn printHelp(cmd_kind_maybe: ?CommandKind, writer: *std.Io.Writer) !void { 41 const print_indent = comptime blk: { 42 var indent = 0; 43 for (0..@typeInfo(CommandKind).@"enum".fields.len) |i| { 44 indent = @max(commandHelp(@enumFromInt(i)).name.len, indent); 45 } 46 indent += 2; 47 break :blk indent; 48 }; 49 50 if (cmd_kind_maybe) |cmd_kind| { 51 const help = commandHelp(cmd_kind); 52 // name and description 53 try writer.print("{s}", .{help.name}); 54 for (0..print_indent - help.name.len) |_| try writer.print(" ", .{}); 55 var split_iter = std.mem.splitScalar(u8, help.descrip, '\n'); 56 try writer.print("{s}\n", .{split_iter.first()}); 57 while (split_iter.next()) |line| { 58 for (0..print_indent) |_| try writer.print(" ", .{}); 59 try writer.print("{s}\n", .{line}); 60 } 61 try writer.print("\n", .{}); 62 // example 63 split_iter = std.mem.splitScalar(u8, help.example, '\n'); 64 while (split_iter.next()) |line| { 65 for (0..print_indent) |_| try writer.print(" ", .{}); 66 try writer.print("{s}\n", .{line}); 67 } 68 } else { 69 try writer.print("help: xit <command> [<args>]\n\n", .{}); 70 inline for (@typeInfo(CommandKind).@"enum".fields) |field| { 71 const help = commandHelp(@enumFromInt(field.value)); 72 // name and description 73 try writer.print("{s}", .{help.name}); 74 for (0..print_indent - help.name.len) |_| try writer.print(" ", .{}); 75 var split_iter = std.mem.splitScalar(u8, help.descrip, '\n'); 76 try writer.print("{s}\n", .{split_iter.first()}); 77 while (split_iter.next()) |line| { 78 for (0..print_indent) |_| try writer.print(" ", .{}); 79 try writer.print("{s}\n", .{line}); 80 } 81 } 82 } 83 } 84 85 pub const CommandArgs = struct { 86 allocator: std.mem.Allocator, 87 arena: *std.heap.ArenaAllocator, 88 command_kind: ?CommandKind, 89 command_name: ?[]const u8, 90 positional_args: []const []const u8, 91 map_args: std.StringArrayHashMapUnmanaged(?[]const u8), 92 unused_args: std.StringArrayHashMapUnmanaged(void), 93 94 // flags that can have a value associated with them 95 // must be included here 96 const value_flags = std.StaticStringMap(void).initComptime(.{ 97 .{"--http-listen"}, 98 .{"--ssh-listen"}, 99 .{"--ssh-connect"}, 100 .{"--data-dir"}, 101 .{"--service"}, 102 }); 103 104 pub fn init(allocator: std.mem.Allocator, args: []const []const u8) !CommandArgs { 105 const arena = try allocator.create(std.heap.ArenaAllocator); 106 arena.* = std.heap.ArenaAllocator.init(allocator); 107 errdefer { 108 arena.deinit(); 109 allocator.destroy(arena); 110 } 111 112 var positional_args: std.ArrayList([]const u8) = .empty; 113 var map_args: std.StringArrayHashMapUnmanaged(?[]const u8) = .empty; 114 var unused_args: std.StringArrayHashMapUnmanaged(void) = .empty; 115 116 for (args) |arg| { 117 if (arg.len > 1 and arg[0] == '-') { 118 try map_args.put(arena.allocator(), arg, null); 119 try unused_args.put(arena.allocator(), arg, {}); 120 } else { 121 // if the last key is a value flag and doesn't have a value yet, 122 // set this arg as its value 123 const keys = map_args.keys(); 124 if (keys.len > 0) { 125 const last_key = keys[keys.len - 1]; 126 if (map_args.get(last_key)) |last_val| { 127 if (value_flags.has(last_key) and last_val == null) { 128 try map_args.put(arena.allocator(), last_key, arg); 129 continue; 130 } 131 } 132 } 133 134 // in any other case, just consider it a positional arg 135 try positional_args.append(arena.allocator(), arg); 136 } 137 } 138 139 const args_slice = try positional_args.toOwnedSlice(arena.allocator()); 140 if (args_slice.len == 0) { 141 return .{ 142 .allocator = allocator, 143 .arena = arena, 144 .command_kind = null, 145 .command_name = null, 146 .positional_args = args_slice, 147 .map_args = map_args, 148 .unused_args = unused_args, 149 }; 150 } else { 151 const command_name = args_slice[0]; 152 const extra_args = args_slice[1..]; 153 154 const command_kind: ?CommandKind = inline for (0..@typeInfo(CommandKind).@"enum".fields.len) |i| { 155 if (std.mem.eql(u8, command_name, commandHelp(@enumFromInt(i)).name)) { 156 break @enumFromInt(i); 157 } 158 } else null; 159 160 return .{ 161 .allocator = allocator, 162 .arena = arena, 163 .command_kind = command_kind, 164 .command_name = command_name, 165 .positional_args = extra_args, 166 .map_args = map_args, 167 .unused_args = unused_args, 168 }; 169 } 170 } 171 172 pub fn deinit(self: *CommandArgs) void { 173 self.arena.deinit(); 174 self.allocator.destroy(self.arena); 175 } 176 177 pub fn contains(self: *CommandArgs, arg: []const u8) bool { 178 _ = self.unused_args.orderedRemove(arg); 179 return self.map_args.contains(arg); 180 } 181 182 pub fn get(self: *CommandArgs, comptime arg: []const u8) ??[]const u8 { 183 comptime std.debug.assert(value_flags.has(arg)); // can only call `get` with flags included in `value_flags` 184 _ = self.unused_args.orderedRemove(arg); 185 return self.map_args.get(arg); 186 } 187 }; 188 189 /// parses the args into a format that can be directly used by a repo. 190 /// if any additional allocation needs to be done, the arena inside the cmd args will be used. 191 pub fn Command(comptime repo_kind: rp.RepoKind, comptime hash_kind: hash.HashKind) type { 192 return union(CommandKind) { 193 serve: struct { 194 http_listen: []const u8, 195 ssh_listen: ?[]const u8, 196 data_dir: []const u8, 197 }, 198 ssh_helper: struct { 199 ssh_connect: []const u8, 200 service: ?[]const u8, 201 dir: ?[]const u8, 202 }, 203 204 pub fn initMaybe(cmd_args: *CommandArgs) !?Command(repo_kind, hash_kind) { 205 const command_kind = cmd_args.command_kind orelse return null; 206 switch (command_kind) { 207 .serve => { 208 if (cmd_args.positional_args.len != 0) return null; 209 210 return .{ .serve = .{ 211 .http_listen = (cmd_args.get("--http-listen") orelse null) orelse "127.0.0.1:8080", 212 .ssh_listen = (cmd_args.get("--ssh-listen") orelse null), 213 .data_dir = (cmd_args.get("--data-dir") orelse null) orelse ".", 214 } }; 215 }, 216 .ssh_helper => { 217 if (cmd_args.positional_args.len > 1) return null; 218 219 return .{ .ssh_helper = .{ 220 .ssh_connect = (cmd_args.get("--ssh-connect") orelse null) orelse "127.0.0.1:8081", 221 .service = (cmd_args.get("--service") orelse null), 222 .dir = if (cmd_args.positional_args.len == 1) cmd_args.positional_args[0] else null, 223 } }; 224 }, 225 } 226 } 227 }; 228 } 229 230 /// parses the given args into a command if valid, and determines how it should be run 231 /// (via the TUI or CLI). 232 pub fn CommandDispatch(comptime repo_kind: rp.RepoKind, comptime hash_kind: hash.HashKind) type { 233 return union(enum) { 234 invalid: union(enum) { 235 command: []const u8, 236 argument: struct { 237 command: ?CommandKind, 238 value: []const u8, 239 }, 240 }, 241 help: ?CommandKind, 242 cli: Command(repo_kind, hash_kind), 243 244 pub fn init(cmd_args: *CommandArgs) !CommandDispatch(repo_kind, hash_kind) { 245 const dispatch = try initIgnoreUnused(cmd_args); 246 if (cmd_args.unused_args.count() > 0) { 247 return .{ 248 .invalid = .{ 249 .argument = .{ 250 .command = switch (dispatch) { 251 .invalid => return dispatch, // if there was already an error, return it instead 252 .help => |cmd_kind_maybe| cmd_kind_maybe, 253 .cli => |command| command, 254 }, 255 .value = cmd_args.unused_args.keys()[0], 256 }, 257 }, 258 }; 259 } 260 return dispatch; 261 } 262 263 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch(repo_kind, hash_kind) { 264 const show_help = cmd_args.contains("--help"); 265 266 if (cmd_args.command_kind) |command_kind| { 267 if (show_help) { 268 return .{ .help = command_kind }; 269 } else if (try Command(repo_kind, hash_kind).initMaybe(cmd_args)) |cmd| { 270 return .{ .cli = cmd }; 271 } else { 272 return .{ .help = command_kind }; 273 } 274 } else if (cmd_args.command_name) |command_name| { 275 return .{ .invalid = .{ .command = command_name } }; 276 } else if (show_help) { 277 return .{ .help = null }; 278 } else { 279 return .{ .help = null }; 280 } 281 } 282 }; 283 } 284