╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
203 if (cmd_args.positional_args.len != 0) return null; 204 205 var options: srv.Options = .{}; 206 if (cmd_args.get("--web-listen")) |value_maybe| options.web_listen = value_maybe orelse return error.WebListenNeedsValue; 207 if (cmd_args.get("--ssh-listen")) |value_maybe| { 208 const value = value_maybe orelse return error.SshListenNeedsValue; 209 options.ssh_listen = if (std.mem.eql(u8, value, "none")) null else value; 210 } 211 // urls are joined with "/" + a path 212 if (cmd_args.get("--web-url")) |value_maybe| options.web_url = std.mem.trimEnd(u8, value_maybe orelse return error.WebUrlNeedsValue, "/"); 213 if (cmd_args.get("--ssh-url")) |value_maybe| options.ssh_url = std.mem.trimEnd(u8, value_maybe orelse return error.SshUrlNeedsValue, "/"); 214 if (cmd_args.get("--data-dir")) |value_maybe| options.data_dir = value_maybe orelse return error.DataDirNeedsValue; 215 return options; 216 } 217 218 /// parses the given args into a command if valid, and determines how it should be run 219 /// (via the TUI or CLI). 220 pub const CommandDispatch = union(enum) { 221 invalid: union(enum) { 222 command: []const u8, 223 argument: struct { 224 command: ?CommandKind, 225 value: []const u8, 226 }, 227 }, 228 help: ?CommandKind, 229 local, 230 cli: Command, 231 232 pub fn init(cmd_args: *CommandArgs) !CommandDispatch { 233 const dispatch = try initIgnoreUnused(cmd_args); 234 if (cmd_args.unused_args.count() > 0) { 235 return .{ 236 .invalid = .{ 237 .argument = .{ 238 .command = switch (dispatch) { 239 .invalid => return dispatch, // if there was already an error, return it instead 240 .help => |cmd_kind_maybe| cmd_kind_maybe, 241 .local => null, 242 .cli => |command| command, 243 }, 244 .value = cmd_args.unused_args.keys()[0], 245 }, 246 }, 247 }; 248 } 249 return dispatch; 250 } 251 252 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch { 253 const show_help = cmd_args.contains("--help"); 254 255 if (cmd_args.command_kind) |command_kind| { 256 if (show_help) { 257 return .{ .help = command_kind }; 258 } else if (try Command.initMaybe(cmd_args)) |cmd| { 259 return .{ .cli = cmd }; 260 } else { 261 return .{ .help = command_kind }; 262 } 263 } else if (cmd_args.command_name) |command_name| { 264 return .{ .invalid = .{ .command = command_name } }; 265 } else if (show_help) { 266 return .{ .help = null }; 267 } else { 268 return .local; 269 } 270 } 271 }; 272