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