╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
211 if (cmd_args.positional_args.len != 1) return null; 212 213 return .{ .upload_pack = .{ 214 .dir = cmd_args.positional_args[0], 215 .options = .{ 216 .advertise_refs = cmd_args.contains("--http-backend-info-refs"), 217 .is_stateless = cmd_args.contains("--stateless-rpc"), 218 }, 219 } }; 220 }, 221 .receive_pack => { 222 if (cmd_args.positional_args.len != 1) return null; 223 224 return .{ .receive_pack = .{ 225 .dir = cmd_args.positional_args[0], 226 .options = .{ 227 .skip_connectivity_check = cmd_args.contains("--skip-connectivity-check"), 228 .advertise_refs = cmd_args.contains("--http-backend-info-refs"), 229 .is_stateless = cmd_args.contains("--stateless-rpc"), 230 }, 231 } }; 232 }, 233 .http_backend => { 234 if (cmd_args.positional_args.len != 0) return null; 235 236 return .http_backend; 237 }, 238 } 239 } 240 }; 241 } 242 243 /// parses the given args into a command if valid, and determines how it should be run 244 /// (via the TUI or CLI). 245 pub fn CommandDispatch(comptime repo_kind: rp.RepoKind, comptime hash_kind: hash.HashKind) type { 246 return union(enum) { 247 invalid: union(enum) { 248 command: []const u8, 249 argument: struct { 250 command: ?CommandKind, 251 value: []const u8, 252 }, 253 }, 254 help: ?CommandKind, 255 cli: Command(repo_kind, hash_kind), 256 257 pub fn init(cmd_args: *CommandArgs) !CommandDispatch(repo_kind, hash_kind) { 258 const dispatch = try initIgnoreUnused(cmd_args); 259 if (cmd_args.unused_args.count() > 0) { 260 return .{ 261 .invalid = .{ 262 .argument = .{ 263 .command = switch (dispatch) { 264 .invalid => return dispatch, // if there was already an error, return it instead 265 .help => |cmd_kind_maybe| cmd_kind_maybe, 266 .cli => |command| command, 267 }, 268 .value = cmd_args.unused_args.keys()[0], 269 }, 270 }, 271 }; 272 } 273 return dispatch; 274 } 275 276 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch(repo_kind, hash_kind) { 277 const show_help = cmd_args.contains("--help"); 278 279 if (cmd_args.command_kind) |command_kind| { 280 if (show_help) { 281 return .{ .help = command_kind }; 282 } else if (try Command(repo_kind, hash_kind).initMaybe(cmd_args)) |cmd| { 283 return .{ .cli = cmd }; 284 } else { 285 return .{ .help = command_kind }; 286 } 287 } else if (cmd_args.command_name) |command_name| { 288 return .{ .invalid = .{ .command = command_name } }; 289 } else if (show_help) { 290 return .{ .help = null }; 291 } else { 292 return .{ .help = null }; 293 } 294 } 295 }; 296 } 297