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