╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
139 const extra_args = args_slice[1..]; 140 141 const command_kind: ?CommandKind = inline for (0..@typeInfo(CommandKind).@"enum".fields.len) |i| { 142 if (std.mem.eql(u8, command_name, commandHelp(@enumFromInt(i)).name)) { 143 break @enumFromInt(i); 144 } 145 } else null; 146 147 return .{ 148 .allocator = allocator, 149 .arena = arena, 150 .command_kind = command_kind, 151 .command_name = command_name, 152 .positional_args = extra_args, 153 .map_args = map_args, 154 .unused_args = unused_args, 155 }; 156 } 157 } 158 159 pub fn deinit(self: *CommandArgs) void { 160 self.arena.deinit(); 161 self.allocator.destroy(self.arena); 162 } 163 164 pub fn contains(self: *CommandArgs, arg: []const u8) bool { 165 _ = self.unused_args.orderedRemove(arg); 166 return self.map_args.contains(arg); 167 } 168 169 pub fn get(self: *CommandArgs, comptime arg: []const u8) ??[]const u8 { 170 comptime std.debug.assert(value_flags.has(arg)); // can only call `get` with flags included in `value_flags` 171 _ = self.unused_args.orderedRemove(arg); 172 return self.map_args.get(arg); 173 } 174 }; 175 176 /// parses the args into a format that can be directly used by a repo. 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 cli: Command, 220 221 pub fn init(cmd_args: *CommandArgs) !CommandDispatch { 222 const dispatch = try initIgnoreUnused(cmd_args); 223 if (cmd_args.unused_args.count() > 0) { 224 return .{ 225 .invalid = .{ 226 .argument = .{ 227 .command = switch (dispatch) { 228 .invalid => return dispatch, // if there was already an error, return it instead 229 .help => |cmd_kind_maybe| cmd_kind_maybe, 230 .cli => |command| command, 231 }, 232 .value = cmd_args.unused_args.keys()[0], 233 }, 234 }, 235 }; 236 } 237 return dispatch; 238 } 239 240 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch { 241 const show_help = cmd_args.contains("--help"); 242 243 if (cmd_args.command_kind) |command_kind| { 244 if (show_help) { 245 return .{ .help = command_kind }; 246 } else if (try Command.initMaybe(cmd_args)) |cmd| { 247 return .{ .cli = cmd }; 248 } else { 249 return .{ .help = command_kind }; 250 } 251 } else if (cmd_args.command_name) |command_name| { 252 return .{ .invalid = .{ .command = command_name } }; 253 } else { 254 return .{ .help = null }; 255 } 256 } 257 }; 258