╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
177 /// if any additional allocation needs to be done, the arena inside the cmd args will be used. 178 pub const Command = union(CommandKind) { 179 serve: srv.Options, 180 181 pub fn initMaybe(cmd_args: *CommandArgs) !?Command { 182 const command_kind = cmd_args.command_kind orelse return null; 183 switch (command_kind) { 184 .serve => { 185 if (cmd_args.positional_args.len != 0) return null; 186 187 var options: srv.Options = .{}; 188 189 if (cmd_args.get("--http-listen")) |val_maybe| { 190 options.http_listen = val_maybe orelse return error.HttpListenNeedsValue; 191 } 192 if (cmd_args.get("--ssh-listen")) |val_maybe| { 193 options.ssh_listen = val_maybe orelse return error.SshListenNeedsValue; 194 } 195 if (cmd_args.get("--wui-listen")) |val_maybe| { 196 options.wui_listen = val_maybe orelse return error.WuiListenNeedsValue; 197 } 198 if (cmd_args.get("--data-dir")) |val_maybe| { 199 options.data_dir = val_maybe orelse return error.DataDirNeedsValue; 200 } 201 202 return .{ .serve = options }; 203 }, 204 } 205 } 206 }; 207 208 /// parses the given args into a command if valid, and determines how it should be run 209 /// (via the TUI or CLI). 210 pub const CommandDispatch = union(enum) { 211 invalid: union(enum) { 212 command: []const u8, 213 argument: struct { 214 command: ?CommandKind, 215 value: []const u8, 216 }, 217 }, 218 help: ?CommandKind, 219 local, 220 cli: Command, 221 222 pub fn init(cmd_args: *CommandArgs) !CommandDispatch { 223 const dispatch = try initIgnoreUnused(cmd_args); 224 if (cmd_args.unused_args.count() > 0) { 225 return .{ 226 .invalid = .{ 227 .argument = .{ 228 .command = switch (dispatch) { 229 .invalid => return dispatch, // if there was already an error, return it instead 230 .help => |cmd_kind_maybe| cmd_kind_maybe, 231 .local => null, 232 .cli => |command| command, 233 }, 234 .value = cmd_args.unused_args.keys()[0], 235 }, 236 }, 237 }; 238 } 239 return dispatch; 240 } 241 242 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch { 243 const show_help = cmd_args.contains("--help"); 244 245 if (cmd_args.command_kind) |command_kind| { 246 if (show_help) { 247 return .{ .help = command_kind }; 248 } else if (try Command.initMaybe(cmd_args)) |cmd| { 249 return .{ .cli = cmd }; 250 } else { 251 return .{ .help = command_kind }; 252 } 253 } else if (cmd_args.command_name) |command_name| { 254 return .{ .invalid = .{ .command = command_name } }; 255 } else if (show_help) { 256 return .{ .help = null }; 257 } else { 258 return .local; 259 } 260 } 261 }; 262