╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 const Command = union(CommandKind) { 192 serve: struct { 193 http_listen: []const u8, 194 ssh_listen: ?[]const u8, 195 data_dir: []const u8, 196 }, 197 ssh_helper: struct { 198 ssh_connect: []const u8, 199 service: ?[]const u8, 200 dir: ?[]const u8, 201 }, 202 203 pub fn initMaybe(cmd_args: *CommandArgs) !?Command { 204 const command_kind = cmd_args.command_kind orelse return null; 205 switch (command_kind) { 206 .serve => { 207 if (cmd_args.positional_args.len != 0) return null; 208 209 return .{ .serve = .{ 210 .http_listen = (cmd_args.get("--http-listen") orelse null) orelse "127.0.0.1:8080", 211 .ssh_listen = (cmd_args.get("--ssh-listen") orelse null), 212 .data_dir = (cmd_args.get("--data-dir") orelse null) orelse ".", 213 } }; 214 }, 215 .ssh_helper => { 216 if (cmd_args.positional_args.len > 1) return null; 217 218 return .{ .ssh_helper = .{ 219 .ssh_connect = (cmd_args.get("--ssh-connect") orelse null) orelse "127.0.0.1:8081", 220 .service = (cmd_args.get("--service") orelse null), 221 .dir = if (cmd_args.positional_args.len == 1) cmd_args.positional_args[0] else null, 222 } }; 223 }, 224 } 225 } 226 }; 227 228 /// parses the given args into a command if valid, and determines how it should be run 229 /// (via the TUI or CLI). 230 pub const CommandDispatch = union(enum) { 231 invalid: union(enum) { 232 command: []const u8, 233 argument: struct { 234 command: ?CommandKind, 235 value: []const u8, 236 }, 237 }, 238 help: ?CommandKind, 239 cli: Command, 240 241 pub fn init(cmd_args: *CommandArgs) !CommandDispatch { 242 const dispatch = try initIgnoreUnused(cmd_args); 243 if (cmd_args.unused_args.count() > 0) { 244 return .{ 245 .invalid = .{ 246 .argument = .{ 247 .command = switch (dispatch) { 248 .invalid => return dispatch, // if there was already an error, return it instead 249 .help => |cmd_kind_maybe| cmd_kind_maybe, 250 .cli => |command| command, 251 }, 252 .value = cmd_args.unused_args.keys()[0], 253 }, 254 }, 255 }; 256 } 257 return dispatch; 258 } 259 260 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch { 261 const show_help = cmd_args.contains("--help"); 262 263 if (cmd_args.command_kind) |command_kind| { 264 if (show_help) { 265 return .{ .help = command_kind }; 266 } else if (try Command.initMaybe(cmd_args)) |cmd| { 267 return .{ .cli = cmd }; 268 } else { 269 return .{ .help = command_kind }; 270 } 271 } else if (cmd_args.command_name) |command_name| { 272 return .{ .invalid = .{ .command = command_name } }; 273 } else if (show_help) { 274 return .{ .help = null }; 275 } else { 276 return .{ .help = null }; 277 } 278 } 279 }; 280