╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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