╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
160 .allocator = allocator, 161 .arena = arena, 162 .command_kind = command_kind, 163 .command_name = command_name, 164 .positional_args = extra_args, 165 .map_args = map_args, 166 .unused_args = unused_args, 167 }; 168 } 169 } 170 171 pub fn deinit(self: *CommandArgs) void { 172 self.arena.deinit(); 173 self.allocator.destroy(self.arena); 174 } 175 176 pub fn contains(self: *CommandArgs, arg: []const u8) bool { 177 _ = self.unused_args.orderedRemove(arg); 178 return self.map_args.contains(arg); 179 } 180 181 pub fn get(self: *CommandArgs, comptime arg: []const u8) ??[]const u8 { 182 comptime std.debug.assert(value_flags.has(arg)); // can only call `get` with flags included in `value_flags` 183 _ = self.unused_args.orderedRemove(arg); 184 return self.map_args.get(arg); 185 } 186 }; 187 188 /// parses the args into a format that can be directly used by a repo. 189 /// if any additional allocation needs to be done, the arena inside the cmd args will be used. 190 pub const Command = union(CommandKind) { 191 serve: srv.Options, 192 ssh_helper: struct { 193 ssh_connect: []const u8, 194 service: ?[]const u8, 195 dir: ?[]const u8, 196 }, 197 198 pub fn initMaybe(cmd_args: *CommandArgs) !?Command { 199 const command_kind = cmd_args.command_kind orelse return null; 200 switch (command_kind) { 201 .serve => { 202 if (cmd_args.positional_args.len != 0) return null; 203 204 var options: srv.Options = .{}; 205 if (cmd_args.get("--http-listen")) |val_maybe| { 206 options.http_listen = val_maybe orelse return error.HttpListenNeedsValue; 207 } 208 if (cmd_args.get("--ssh-listen")) |val_maybe| { 209 options.ssh_listen = val_maybe orelse return error.SshListenNeedsValue; 210 } 211 if (cmd_args.get("--wui-listen")) |val_maybe| { 212 options.wui_listen = val_maybe orelse return error.WuiListenNeedsValue; 213 } 214 if (cmd_args.get("--data-dir")) |val_maybe| { 215 options.data_dir = val_maybe orelse return error.DataDirNeedsValue; 216 } 217 if (cmd_args.contains("--tui")) options.tui = true; 218 219 return .{ .serve = options }; 220 }, 221 .ssh_helper => { 222 if (cmd_args.positional_args.len > 1) return null; 223 224 const options: srv.Options = .{}; 225 226 return .{ .ssh_helper = .{ 227 .ssh_connect = if (cmd_args.get("--ssh-connect")) |val_maybe| 228 val_maybe orelse options.ssh_listen 229 else 230 return error.SshListenNeedsValue, 231 .service = if (cmd_args.get("--service")) |val_maybe| 232 val_maybe orelse return error.ServiceNeedsValue 233 else 234 null, 235 .dir = if (cmd_args.positional_args.len == 1) cmd_args.positional_args[0] else null, 236 } }; 237 }, 238 } 239 } 240 }; 241 242 /// parses the given args into a command if valid, and determines how it should be run 243 /// (via the TUI or CLI). 244 pub const CommandDispatch = union(enum) { 245 invalid: union(enum) { 246 command: []const u8, 247 argument: struct { 248 command: ?CommandKind, 249 value: []const u8, 250 }, 251 }, 252 help: ?CommandKind, 253 cli: Command, 254 255 pub fn init(cmd_args: *CommandArgs) !CommandDispatch { 256 const dispatch = try initIgnoreUnused(cmd_args); 257 if (cmd_args.unused_args.count() > 0) { 258 return .{ 259 .invalid = .{ 260 .argument = .{ 261 .command = switch (dispatch) { 262 .invalid => return dispatch, // if there was already an error, return it instead 263 .help => |cmd_kind_maybe| cmd_kind_maybe, 264 .cli => |command| command, 265 }, 266 .value = cmd_args.unused_args.keys()[0], 267 }, 268 }, 269 }; 270 } 271 return dispatch; 272 } 273 274 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch { 275 const show_help = cmd_args.contains("--help"); 276 277 if (cmd_args.command_kind) |command_kind| { 278 if (show_help) { 279 return .{ .help = command_kind }; 280 } else if (try Command.initMaybe(cmd_args)) |cmd| { 281 return .{ .cli = cmd }; 282 } else { 283 return .{ .help = command_kind }; 284 } 285 } else if (cmd_args.command_name) |command_name| { 286 return .{ .invalid = .{ .command = command_name } }; 287 } else if (show_help) { 288 return .{ .help = null }; 289 } else { 290 return .{ .help = null }; 291 } 292 } 293 }; 294