Skip to content

Commit 85df280

Browse files
committed
When explicit mode (serve/fetch/help) isn't given, infer it from the options
1 parent 734cf24 commit 85df280

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ COPY --from=0 /browser/zig-out/bin/lightpanda /bin/lightpanda
7676

7777
EXPOSE 9222/tcp
7878

79-
CMD ["/bin/lightpanda", "--host", "0.0.0.0", "--port", "9222"]
79+
CMD ["/bin/lightpanda", "serve", "--host", "0.0.0.0", "--port", "9222"]

src/main.zig

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ pub fn main() !void {
4747
var fba = std.heap.FixedBufferAllocator.init(&mem);
4848
const args = try parseArgs(fba.allocator());
4949

50-
if (args.mode == .usage) {
50+
if (args.mode == .help) {
5151
// don't need to create a gpa in this case
52-
return args.printUsageAndExit(args.mode.usage);
52+
return args.printUsageAndExit(args.mode.help);
5353
}
5454

5555
// allocator
@@ -63,7 +63,7 @@ pub fn main() !void {
6363
};
6464

6565
switch (args.mode) {
66-
.usage => unreachable, // handled above
66+
.help => unreachable, // handled above
6767
.serve => |opts| {
6868
const address = std.net.Address.parseIp4(opts.host, opts.port) catch |err| {
6969
log.err("address (host:port) {any}\n", .{err});
@@ -126,8 +126,14 @@ const Command = struct {
126126
mode: Mode,
127127
exec_name: []const u8,
128128

129-
const Mode = union(enum) {
130-
usage: bool, // false when being printed because of an error
129+
const ModeType = enum {
130+
help,
131+
fetch,
132+
serve,
133+
};
134+
135+
const Mode = union(ModeType) {
136+
help: bool, // false when being printed because of an error
131137
fetch: Fetch,
132138
serve: Serve,
133139
};
@@ -159,15 +165,15 @@ const Command = struct {
159165
\\
160166
\\serve command
161167
\\Starts a websocket CDP server
162-
\\Example: {s} server --host 127.0.0.1 --port 9222
168+
\\Example: {s} serve --host 127.0.0.1 --port 9222
163169
\\
164170
\\Options:
165171
\\--host Host of the CDP server
166-
\\
167172
\\ Defaults to "127.0.0.1"
168-
\\--port Port of the CDP server
169173
\\
174+
\\--port Port of the CDP server
170175
\\ Defaults to 9222
176+
\\
171177
\\--timeout Inactivity timeout in seconds before disconnecting clients
172178
\\ Defaults to 3 (seconds)
173179
\\
@@ -184,31 +190,62 @@ const Command = struct {
184190

185191
fn parseArgs(allocator: Allocator) !Command {
186192
var args = try std.process.argsWithAllocator(allocator);
193+
defer args.deinit();
187194

188195
const exec_name = std.fs.path.basename(args.next().?);
196+
189197
var cmd = Command{
190-
.mode = .{ .usage = false },
198+
.mode = .{ .help = false },
191199
.exec_name = try allocator.dupe(u8, exec_name),
192200
};
193201

194-
const command = args.next() orelse "";
195-
if (std.mem.eql(u8, command, "serve")) {
196-
cmd.mode = .{ .serve = parseServeArgs(allocator, &args) catch return cmd };
197-
return cmd;
202+
const mode_string = args.next() orelse "";
203+
const mode = std.meta.stringToEnum(Command.ModeType, mode_string) orelse blk: {
204+
const inferred_mode = inferMode(mode_string) orelse return cmd;
205+
// "command" wasn't a command but an option. We can't reset args, but
206+
// we can create a new one. Not great, but this fallback is temporary
207+
// as we transition to this command mode approach.
208+
args.deinit();
209+
210+
args = try std.process.argsWithAllocator(allocator);
211+
// skip the exec_name
212+
_ = args.skip();
213+
214+
break :blk inferred_mode;
215+
};
216+
217+
cmd.mode = switch (mode) {
218+
.help => Command.Mode{ .help = true },
219+
.serve => Command.Mode{ .serve = parseServeArgs(allocator, &args) catch return cmd },
220+
.fetch => Command.Mode{ .fetch = parseFetchArgs(allocator, &args) catch return cmd },
221+
};
222+
return cmd;
223+
}
224+
225+
fn inferMode(opt: []const u8) ?Command.ModeType {
226+
if (opt.len == 0) {
227+
return .serve;
198228
}
199229

200-
if (std.mem.eql(u8, command, "fetch")) {
201-
cmd.mode = .{ .fetch = parseFetchArgs(allocator, &args) catch return cmd };
202-
return cmd;
230+
if (std.mem.eql(u8, opt, "--dump")) {
231+
return .fetch;
232+
}
233+
if (std.mem.startsWith(u8, opt, "--") == false) {
234+
return .fetch;
203235
}
204236

205-
if (std.mem.eql(u8, command, "help")) {
206-
cmd.mode = .{ .usage = true };
207-
return cmd;
237+
if (std.mem.eql(u8, opt, "--host")) {
238+
return .serve;
208239
}
209240

210-
log.err("first argument should be one of: fetch, serve or help", .{});
211-
return cmd;
241+
if (std.mem.eql(u8, opt, "--port")) {
242+
return .serve;
243+
}
244+
245+
if (std.mem.eql(u8, opt, "--timeout")) {
246+
return .serve;
247+
}
248+
return null;
212249
}
213250

214251
fn parseServeArgs(

0 commit comments

Comments
 (0)