╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
128 if (args_slice.len == 0) { 129 return .{ 130 .allocator = allocator, 131 .arena = arena, 132 .command_kind = null, 133 .command_name = null, 134 .positional_args = args_slice, 135 .map_args = map_args, 136 .unused_args = unused_args, 137 }; 138 } else { 139 const command_name = args_slice[0]; 140 const extra_args = args_slice[1..]; 141 142 const command_kind: ?CommandKind = inline for (0..@typeInfo(CommandKind).@"enum".field_names.len) |i| { 143 if (std.mem.eql(u8, command_name, commandHelp(@fromBackingInt(@intCast(i))).name)) { 144 break @fromBackingInt(@intCast(i)); 145 } 146 } else null; 147 148 return .{ 149 .allocator = allocator, 150 .arena = arena, 151 .command_kind = command_kind, 152 .command_name = command_name, 153 .positional_args = extra_args, 154 .map_args = map_args, 155 .unused_args = unused_args, 156 }; 157 } 158 } 159 160 pub fn deinit(self: *CommandArgs) void { 161 self.arena.deinit(); 162 self.allocator.destroy(self.arena); 163 } 164 165 pub fn contains(self: *CommandArgs, arg: []const u8) bool { 166 _ = self.unused_args.orderedRemove(arg); 167 return self.map_args.contains(arg); 168 } 169 170 pub fn get(self: *CommandArgs, comptime arg: []const u8) ??[]const u8 { 171 comptime std.debug.assert(value_flags.has(arg)); // can only call `get` with flags included in `value_flags` 172 _ = self.unused_args.orderedRemove(arg); 173 return self.map_args.get(arg); 174 } 175 }; 176 177 /// parses the args into a format that can be directly used by a repo. 178 /// if any additional allocation needs to be done, the arena inside the cmd args will be used. 179 pub const Command = union(CommandKind) { 180 serve: srv.Options, 181 182 pub fn initMaybe(cmd_args: *CommandArgs) !?Command { 183 const command_kind = cmd_args.command_kind orelse return null; 184 switch (command_kind) { 185 .serve => { 186 return .{ .serve = try serveOptions(cmd_args) orelse return null }; 187 }, 188 } 189 } 190 }; 191 192 pub fn parseServeOptions(allocator: std.mem.Allocator, args: []const []const u8) !srv.Options { 193 var cmd_args = try CommandArgs.init(allocator, args); 194 defer cmd_args.deinit(); 195 const options = try serveOptions(&cmd_args) orelse return error.InvalidServeArgument; 196 if (cmd_args.unused_args.count() != 0) return error.InvalidServeArgument; 197 return options; 198 } 199 200 fn serveOptions(cmd_args: *CommandArgs) !?srv.Options { 201 if (cmd_args.positional_args.len != 0) return null; 202 203 var options: srv.Options = .{}; 204 if (cmd_args.get("--http-listen")) |value_maybe| { 205 const value = value_maybe orelse return error.HttpListenNeedsValue; 206 options.http_listen = if (std.mem.eql(u8, value, "none")) null else value; 207 } 208 if (cmd_args.get("--ssh-listen")) |value_maybe| { 209 const value = value_maybe orelse return error.SshListenNeedsValue; 210 options.ssh_listen = if (std.mem.eql(u8, value, "none")) null else value; 211 } 212 if (cmd_args.get("--wui-listen")) |value_maybe| options.wui_listen = value_maybe orelse return error.WuiListenNeedsValue; 213 if (cmd_args.get("--data-dir")) |value_maybe| options.data_dir = value_maybe orelse return error.DataDirNeedsValue; 214 return options; 215 } 216 217 /// parses the given args into a command if valid, and determines how it should be run 218 /// (via the TUI or CLI). 219 pub const CommandDispatch = union(enum) { 220 invalid: union(enum) { 221 command: []const u8, 222 argument: struct { 223 command: ?CommandKind, 224 value: []const u8, 225 }, 226 }, 227 help: ?CommandKind, 228 local, 229 cli: Command, 230 231 pub fn init(cmd_args: *CommandArgs) !CommandDispatch { 232 const dispatch = try initIgnoreUnused(cmd_args); 233 if (cmd_args.unused_args.count() > 0) { 234 return .{ 235 .invalid = .{ 236 .argument = .{ 237 .command = switch (dispatch) { 238 .invalid => return dispatch, // if there was already an error, return it instead 239 .help => |cmd_kind_maybe| cmd_kind_maybe, 240 .local => null, 241 .cli => |command| command, 242 }, 243 .value = cmd_args.unused_args.keys()[0], 244 }, 245 }, 246 }; 247 } 248 return dispatch; 249 } 250 251 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch { 252 const show_help = cmd_args.contains("--help"); 253 254 if (cmd_args.command_kind) |command_kind| { 255 if (show_help) { 256 return .{ .help = command_kind }; 257 } else if (try Command.initMaybe(cmd_args)) |cmd| { 258 return .{ .cli = cmd }; 259 } else { 260 return .{ .help = command_kind }; 261 } 262 } else if (cmd_args.command_name) |command_name| { 263 return .{ .invalid = .{ .command = command_name } }; 264 } else if (show_help) { 265 return .{ .help = null }; 266 } else { 267 return .local; 268 } 269 } 270 }; 271