╭ changed ──────╮ │src/command.zig│ ╰───────────────╯ ╭───────────╮ │← all files│ ╰───────────╯ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │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 - }, │ │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 │ │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; │ │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 │ │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; │ │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 │ │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 - } │ │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 } │ │227 - }; │ │228 -} │ │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). │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │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 - }, │ │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 }, │ │241 - help: ?CommandKind, │ │242 - cli: Command(repo_kind, hash_kind), │ │237 + }, │ │238 + help: ?CommandKind, │ │239 + cli: Command, │ │240 │ │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], │ │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 }, │ │258 - }; │ │259 - } │ │260 - return dispatch; │ │254 + }, │ │255 + }; │ │256 } │ │257 + return dispatch; │ │258 + } │ │259 │ │263 - pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch(repo_kind, hash_kind) { │ │264 - const show_help = cmd_args.contains("--help"); │ │260 + pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch { │ │261 + const show_help = cmd_args.contains("--help"); │ │262 │ │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 }; │ │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 { │ │279 - return .{ .help = null }; │ │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 } │ │282 - }; │ │283 -} │ │278 + } │ │279 +}; │ │280 │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯