chrome-cli 1.2.0

A CLI tool for browser automation via the Chrome DevTools Protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
mod capabilities;
mod cli;
mod console;
mod dialog;
mod dom;
mod emulate;
mod examples;
mod form;
mod interact;
mod js;
mod navigate;
mod network;
mod page;
mod perf;
mod snapshot;
mod tabs;

use std::time::Duration;

use clap::{CommandFactory, Parser, error::ErrorKind};
use serde::Serialize;

use chrome_cli::chrome::{
    self, Channel, LaunchConfig, discover_chrome, find_available_port, find_chrome_executable,
    launch_chrome, query_version,
};
use chrome_cli::config;
use chrome_cli::connection::{self, extract_port_from_ws_url};
use chrome_cli::error::{AppError, ExitCode};
use chrome_cli::session::{self, SessionData};

use cli::{
    ChromeChannel, Cli, Command, CompletionsArgs, ConfigCommand, ConnectArgs, GlobalOpts, ManArgs,
};

#[tokio::main]
async fn main() {
    let cli = match Cli::try_parse() {
        Ok(cli) => cli,
        Err(e) => {
            // --help and --version are informational, not errors — print as-is
            if matches!(e.kind(), ErrorKind::DisplayHelp | ErrorKind::DisplayVersion) {
                e.print().expect("failed to write to stdout");
                std::process::exit(0);
            }
            // All other clap errors → JSON on stderr with exit code 1
            let msg = e.kind().to_string();
            let full = e.to_string();
            let clean = full
                .lines()
                .filter(|line| {
                    let trimmed = line.trim();
                    !trimmed.is_empty()
                        && !trimmed.starts_with("For more information")
                        && !trimmed.starts_with("Usage:")
                })
                .map(|line| line.strip_prefix("error: ").unwrap_or(line).trim())
                .collect::<Vec<_>>()
                .join(", ");
            let clean = if clean.is_empty() { msg } else { clean };
            let app_err = AppError {
                message: clean,
                code: ExitCode::GeneralError,
                custom_json: None,
            };
            app_err.print_json_stderr();
            std::process::exit(app_err.code as i32);
        }
    };

    if let Err(e) = run(&cli).await {
        e.print_json_stderr();
        #[allow(clippy::cast_possible_truncation)]
        std::process::exit(e.code as i32);
    }
}

async fn run(cli: &Cli) -> Result<(), AppError> {
    // Load config file (if any) and apply defaults to global opts
    let (config_path, config_file) = config::load_config(cli.global.config.as_deref());
    let global = apply_config_defaults(&cli.global, &config_file);

    match &cli.command {
        Command::Config(args) => {
            let resolved = build_resolved_config(&global, &config_file, config_path);
            execute_config(&args.command, &resolved)
        }
        Command::Connect(args) => execute_connect(&global, args).await,
        Command::Tabs(args) => tabs::execute_tabs(&global, args).await,
        Command::Navigate(args) => navigate::execute_navigate(&global, args).await,
        Command::Page(args) => page::execute_page(&global, args).await,
        Command::Dom(args) => dom::execute_dom(&global, args).await,
        Command::Js(args) => js::execute_js(&global, args).await,
        Command::Console(args) => console::execute_console(&global, args).await,
        Command::Network(args) => network::execute_network(&global, args).await,
        Command::Interact(args) => interact::execute_interact(&global, args).await,
        Command::Form(args) => form::execute_form(&global, args).await,
        Command::Emulate(args) => emulate::execute_emulate(&global, args).await,
        Command::Perf(args) => perf::execute_perf(&global, args).await,
        Command::Dialog(args) => dialog::execute_dialog(&global, args).await,
        Command::Examples(args) => examples::execute_examples(&global, args),
        Command::Capabilities(args) => capabilities::execute_capabilities(&global, args),
        Command::Completions(args) => execute_completions(args),
        Command::Man(args) => execute_man(args),
    }
}

/// Build a fully resolved config from merged `GlobalOpts` and config file sections.
///
/// This is used by `config show` to display the final merged configuration from
/// all sources (CLI flags > env vars > config file > defaults).
fn build_resolved_config(
    global: &GlobalOpts,
    config_file: &config::ConfigFile,
    config_path: Option<std::path::PathBuf>,
) -> config::ResolvedConfig {
    config::ResolvedConfig {
        config_path,
        connection: config::ResolvedConnection {
            host: global.host.clone(),
            port: global.port_or_default(),
            timeout_ms: global.timeout.unwrap_or(30_000),
        },
        launch: config::ResolvedLaunch {
            executable: config_file.launch.executable.clone(),
            channel: config_file
                .launch
                .channel
                .clone()
                .unwrap_or_else(|| "stable".to_string()),
            headless: config_file.launch.headless.unwrap_or(false),
            extra_args: config_file.launch.extra_args.clone().unwrap_or_default(),
        },
        output: config::ResolvedOutput {
            format: config_file
                .output
                .format
                .clone()
                .unwrap_or_else(|| "json".to_string()),
        },
        tabs: config::ResolvedTabs {
            auto_activate: config_file.tabs.auto_activate.unwrap_or(true),
            filter_internal: config_file.tabs.filter_internal.unwrap_or(true),
        },
    }
}

/// Apply config file defaults to global opts for fields that weren't set by CLI/env.
///
/// The precedence chain is: CLI flags > env vars > config file > built-in defaults.
/// Since clap already handles CLI flags and env vars (via `env = "..."` attributes),
/// we only fill in values from the config file for fields that are still at their defaults.
fn apply_config_defaults(cli_global: &GlobalOpts, config: &config::ConfigFile) -> GlobalOpts {
    // We can't easily detect "user provided --host" vs "default_value was used" for String
    // fields. For Option fields, None means unset; for host (which has default_value), we
    // check whether it matches the built-in default.
    let host_is_default =
        cli_global.host == "127.0.0.1" && std::env::var("CHROME_CLI_HOST").is_err();

    GlobalOpts {
        port: cli_global.port.or(config.connection.port),
        host: if host_is_default {
            config
                .connection
                .host
                .clone()
                .unwrap_or_else(|| cli_global.host.clone())
        } else {
            cli_global.host.clone()
        },
        ws_url: cli_global.ws_url.clone(),
        timeout: cli_global.timeout.or(config.connection.timeout_ms),
        tab: cli_global.tab.clone(),
        auto_dismiss_dialogs: cli_global.auto_dismiss_dialogs,
        config: cli_global.config.clone(),
        output: cli::OutputFormat {
            json: cli_global.output.json,
            pretty: cli_global.output.pretty,
            plain: cli_global.output.plain,
        },
    }
}

#[derive(Serialize)]
struct ConfigInitOutput {
    created: String,
}

#[derive(Serialize)]
struct ConfigPathOutput {
    config_path: Option<String>,
}

/// Execute config subcommands.
fn execute_config(cmd: &ConfigCommand, resolved: &config::ResolvedConfig) -> Result<(), AppError> {
    match cmd {
        ConfigCommand::Show => {
            print_json(resolved)?;
            Ok(())
        }
        ConfigCommand::Init(args) => {
            let path = config::init_config(args.path.as_deref())?;
            print_json(&ConfigInitOutput {
                created: path.display().to_string(),
            })?;
            Ok(())
        }
        ConfigCommand::Path => {
            print_json(&ConfigPathOutput {
                config_path: resolved
                    .config_path
                    .as_ref()
                    .map(|p| p.display().to_string()),
            })?;
            Ok(())
        }
    }
}

#[allow(clippy::unnecessary_wraps)]
fn execute_completions(args: &CompletionsArgs) -> Result<(), AppError> {
    let mut cmd = Cli::command();
    clap_complete::generate(args.shell, &mut cmd, "chrome-cli", &mut std::io::stdout());
    Ok(())
}

fn execute_man(args: &ManArgs) -> Result<(), AppError> {
    let cmd = Cli::command();

    let target = match &args.command {
        None => cmd,
        Some(name) => find_subcommand(&cmd, name).ok_or_else(|| AppError {
            message: format!("unknown command: {name}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        })?,
    };

    let man = clap_mangen::Man::new(target);
    man.render(&mut std::io::stdout()).map_err(|e| AppError {
        message: format!("failed to render man page: {e}"),
        code: ExitCode::GeneralError,
        custom_json: None,
    })?;
    Ok(())
}

fn find_subcommand(cmd: &clap::Command, name: &str) -> Option<clap::Command> {
    let parent_name = cmd.get_name().to_string();
    for sub in cmd.get_subcommands() {
        if sub.get_name() == name {
            let full_name = format!("{parent_name}-{name}");
            let leaked: &'static str = Box::leak(full_name.into_boxed_str());
            return Some(sub.clone().name(leaked));
        }
    }
    None
}

#[derive(Serialize)]
struct ConnectionInfo {
    ws_url: String,
    port: u16,
    #[serde(skip_serializing_if = "Option::is_none")]
    pid: Option<u32>,
}

#[derive(Serialize)]
struct StatusInfo {
    ws_url: String,
    port: u16,
    #[serde(skip_serializing_if = "Option::is_none")]
    pid: Option<u32>,
    timestamp: String,
    reachable: bool,
}

#[derive(Serialize)]
struct DisconnectInfo {
    disconnected: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    killed_pid: Option<u32>,
}

fn print_json(value: &impl Serialize) -> Result<(), AppError> {
    let json = serde_json::to_string(value).map_err(|e| AppError {
        message: format!("serialization error: {e}"),
        code: ExitCode::GeneralError,
        custom_json: None,
    })?;
    println!("{json}");
    Ok(())
}

fn convert_channel(ch: ChromeChannel) -> Channel {
    match ch {
        ChromeChannel::Stable => Channel::Stable,
        ChromeChannel::Canary => Channel::Canary,
        ChromeChannel::Beta => Channel::Beta,
        ChromeChannel::Dev => Channel::Dev,
    }
}

fn warn_if_remote_host(host: &str) {
    if host != "127.0.0.1" && host != "localhost" && host != "::1" {
        eprintln!(
            "warning: connecting to non-localhost host {host}. \
             CDP connections to remote hosts may expose sensitive data."
        );
    }
}

/// Write session data after a successful connect. Non-fatal on failure.
///
/// When `info.pid` is `None` (e.g. auto-discover or direct WS URL), this checks
/// the existing session file and preserves its PID if the port matches. This
/// prevents losing the PID stored by a prior `--launch` when reconnecting to the
/// same Chrome instance.
fn save_session(info: &ConnectionInfo) {
    // Preserve PID and active_tab_id from existing session when reconnecting to the same port.
    let existing = session::read_session()
        .ok()
        .flatten()
        .filter(|existing| existing.port == info.port);
    let pid = info.pid.or_else(|| existing.as_ref().and_then(|e| e.pid));
    let active_tab_id = existing.and_then(|e| e.active_tab_id);

    let data = SessionData {
        ws_url: info.ws_url.clone(),
        port: info.port,
        pid,
        active_tab_id,
        timestamp: session::now_iso8601(),
    };
    if let Err(e) = session::write_session(&data) {
        eprintln!("warning: could not save session file: {e}");
    }
}

async fn execute_connect(global: &GlobalOpts, args: &ConnectArgs) -> Result<(), AppError> {
    // Handle --status
    if args.status {
        return execute_status(global).await;
    }

    // Handle --disconnect
    if args.disconnect {
        return execute_disconnect();
    }

    let timeout = Duration::from_millis(global.timeout.unwrap_or(30_000));

    warn_if_remote_host(&global.host);

    // Strategy 1: Direct WebSocket URL
    if let Some(ws_url) = &global.ws_url {
        let port = extract_port_from_ws_url(ws_url).unwrap_or(global.port_or_default());
        let info = ConnectionInfo {
            ws_url: ws_url.clone(),
            port,
            pid: None,
        };
        save_session(&info);
        print_json(&info)?;
        return Ok(());
    }

    // Strategy 2: Explicit --launch
    if args.launch {
        return execute_launch(args, timeout).await;
    }

    // Strategy 3: Check existing session first, then auto-discover, then auto-launch.
    // When --port is explicit, skip the session file and only try that port directly.
    if global.port.is_none() {
        if let Some(session_data) = session::read_session()? {
            if connection::health_check(&global.host, session_data.port)
                .await
                .is_ok()
            {
                let info = ConnectionInfo {
                    ws_url: session_data.ws_url,
                    port: session_data.port,
                    pid: session_data.pid,
                };
                save_session(&info);
                print_json(&info)?;
                return Ok(());
            }
        }
    }

    let discover_result = if global.port.is_some() {
        // Explicit --port: try only that port, no DevToolsActivePort fallback
        query_version(&global.host, global.port_or_default())
            .await
            .map(|v| (v.ws_debugger_url, global.port_or_default()))
    } else {
        discover_chrome(&global.host, global.port_or_default()).await
    };

    match discover_result {
        Ok((ws_url, port)) => {
            let info = ConnectionInfo {
                ws_url,
                port,
                pid: None,
            };
            save_session(&info);
            print_json(&info)?;
            Ok(())
        }
        Err(discover_err) => {
            if global.port.is_some() {
                // Explicit --port: don't auto-launch on a different port
                Err(discover_err.into())
            } else {
                // Try auto-launch if Chrome is available
                match execute_launch(args, timeout).await {
                    Ok(()) => Ok(()),
                    Err(_) => Err(discover_err.into()),
                }
            }
        }
    }
}

/// Maximum number of port-retry attempts when Chrome fails to bind.
const MAX_PORT_RETRIES: u8 = 3;

async fn execute_launch(args: &ConnectArgs, timeout: Duration) -> Result<(), AppError> {
    let executable = match &args.chrome_path {
        Some(path) => path.clone(),
        None => find_chrome_executable(convert_channel(args.channel))?,
    };

    let mut last_err = None;
    for _ in 0..MAX_PORT_RETRIES {
        let port = find_available_port()?;

        let config = LaunchConfig {
            executable: executable.clone(),
            port,
            headless: args.headless,
            extra_args: args.chrome_arg.clone(),
            user_data_dir: None,
        };

        match launch_chrome(config, timeout).await {
            Ok(process) => {
                // Query the version to get the WebSocket URL
                let version = query_version("127.0.0.1", port).await?;

                // Detach so Chrome keeps running after we exit
                let (pid, port) = process.detach();

                let info = ConnectionInfo {
                    ws_url: version.ws_debugger_url,
                    port,
                    pid: Some(pid),
                };
                save_session(&info);
                print_json(&info)?;
                return Ok(());
            }
            Err(e @ chrome::ChromeError::LaunchFailed(_)) => {
                last_err = Some(e);
                continue;
            }
            Err(e) => return Err(e.into()),
        }
    }

    Err(last_err
        .unwrap_or_else(|| chrome::ChromeError::LaunchFailed("all port retries exhausted".into()))
        .into())
}

async fn execute_status(global: &GlobalOpts) -> Result<(), AppError> {
    let session_data = session::read_session()?.ok_or_else(AppError::no_session)?;

    let reachable = connection::health_check(&global.host, session_data.port)
        .await
        .is_ok();

    let status = StatusInfo {
        ws_url: session_data.ws_url,
        port: session_data.port,
        pid: session_data.pid,
        timestamp: session_data.timestamp,
        reachable,
    };

    if global.output.plain {
        print!("{}", format_plain_status(&status));
        return Ok(());
    }

    let json = if global.output.pretty {
        serde_json::to_string_pretty(&status)
    } else {
        serde_json::to_string(&status)
    };
    let json = json.map_err(|e| AppError {
        message: format!("serialization error: {e}"),
        code: ExitCode::GeneralError,
        custom_json: None,
    })?;
    println!("{json}");
    Ok(())
}

fn format_plain_status(status: &StatusInfo) -> String {
    use std::fmt::Write;
    let mut out = String::new();
    let _ = writeln!(out, "ws_url:    {}", status.ws_url);
    let _ = writeln!(out, "port:      {}", status.port);
    match status.pid {
        Some(pid) => {
            let _ = writeln!(out, "pid:       {pid}");
        }
        None => {
            let _ = writeln!(out, "pid:       -");
        }
    }
    let _ = writeln!(out, "timestamp: {}", status.timestamp);
    let _ = writeln!(out, "reachable: {}", status.reachable);
    out
}

fn execute_disconnect() -> Result<(), AppError> {
    let session_data = session::read_session()?;
    let mut killed_pid = None;

    if let Some(data) = &session_data {
        if let Some(pid) = data.pid {
            kill_process(pid);
            killed_pid = Some(pid);
        }
    }

    session::delete_session()?;

    let output = DisconnectInfo {
        disconnected: true,
        killed_pid,
    };
    print_json(&output)?;
    Ok(())
}

fn kill_process(pid: u32) {
    #[cfg(unix)]
    {
        use std::thread;

        // PID values are always within i32 range on all supported platforms.
        #[allow(clippy::cast_possible_wrap)]
        let pid_i32 = pid as i32;

        // Send SIGTERM to the process group (negative PID) to kill Chrome and
        // all its child processes (renderer, GPU, utility, etc.).
        // SAFETY: libc::kill with a negative pid targets the process group.
        let term_result = unsafe { libc::kill(-pid_i32, libc::SIGTERM) };
        if term_result != 0 {
            // Process group kill failed — try killing just the main process.
            // This can happen if Chrome didn't become a process group leader.
            unsafe { libc::kill(pid_i32, libc::SIGTERM) };
        }

        // Poll for up to ~2 seconds to see if the process has exited.
        let poll_interval = Duration::from_millis(100);
        let max_wait = Duration::from_secs(2);
        let start = std::time::Instant::now();

        while start.elapsed() < max_wait {
            // kill(pid, 0) checks if the process exists without sending a signal.
            // SAFETY: signal 0 is a null signal used only for existence checks.
            let exists = unsafe { libc::kill(pid_i32, 0) };
            if exists != 0 {
                // Process no longer exists — SIGTERM worked.
                return;
            }
            thread::sleep(poll_interval);
        }

        // SIGTERM didn't terminate the process within the timeout. Escalate to
        // SIGKILL on the process group, then fall back to the main PID.
        let kill_result = unsafe { libc::kill(-pid_i32, libc::SIGKILL) };
        if kill_result != 0 {
            unsafe { libc::kill(pid_i32, libc::SIGKILL) };
        }
    }
    #[cfg(windows)]
    {
        // /T kills the process tree, /F forces termination.
        let _ = std::process::Command::new("taskkill")
            .args(["/T", "/F", "/PID", &pid.to_string()])
            .output();
    }
}