╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
130 } 131 } 132 } 133 134 // in any other case, just consider it a positional arg 135 try positional_args.append(arena.allocator(), arg); 136 } 137 } 138 139 const args_slice = try positional_args.toOwnedSlice(arena.allocator()); 140 if (args_slice.len == 0) { 141 return .{ 142 .allocator = allocator, 143 .arena = arena, 144 .command_kind = null, 145 .command_name = null, 146 .positional_args = args_slice, 147 .map_args = map_args, 148 .unused_args = unused_args, 149 }; 150 } else { 151 const command_name = args_slice[0]; 152 const extra_args = args_slice[1..]; 153 154 const command_kind: ?CommandKind = inline for (0..@typeInfo(CommandKind).@"enum".fields.len) |i| { 155 if (std.mem.eql(u8, command_name, commandHelp(@enumFromInt(i)).name)) { 156 break @enumFromInt(i); 157 } 158 } else null; 159 160 return .{ 161 .allocator = allocator, 162 .arena = arena, 163 .command_kind = command_kind, 164 .command_name = command_name, 165 .positional_args = extra_args, 166 .map_args = map_args, 167 .unused_args = unused_args, 168 }; 169 } 170 } 171 172 pub fn deinit(self: *CommandArgs) void { 173 self.arena.deinit(); 174 self.allocator.destroy(self.arena); 175 } 176 177 pub fn contains(self: *CommandArgs, arg: []const u8) bool { 178 _ = self.unused_args.orderedRemove(arg); 179 return self.map_args.contains(arg); 180 } 181 182 pub fn get(self: *CommandArgs, comptime arg: []const u8) ??[]const u8 { 183 comptime std.debug.assert(value_flags.has(arg)); // can only call `get` with flags included in `value_flags` 184 _ = self.unused_args.orderedRemove(arg); 185 return self.map_args.get(arg); 186 } 187 }; 188 189 /// parses the args into a format that can be directly used by a repo. 190 /// if any additional allocation needs to be done, the arena inside the cmd args will be used. 191 pub const Command = union(CommandKind) { 192 serve: struct { 193 http_listen: []const u8, 194 ssh_listen: ?[]const u8, 195 data_dir: []const u8, 196 }, 197 ssh_helper: struct { 198 ssh_connect: []const u8, 199 service: ?[]const u8, 200 dir: ?[]const u8, 201 }, 202 203 pub fn initMaybe(cmd_args: *CommandArgs) !?Command { 204 const command_kind = cmd_args.command_kind orelse return null; 205 switch (command_kind) { 206 .serve => { 207 if (cmd_args.positional_args.len != 0) return null; 208 209 return .{ .serve = .{ 210 .http_listen = (cmd_args.get("--http-listen") orelse null) orelse "127.0.0.1:8080", 211 .ssh_listen = (cmd_args.get("--ssh-listen") orelse null), 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