()
| 768 | } |
| 769 | |
| 770 | pub async fn run() -> Result<()> { |
| 771 | let cli = match Cli::try_parse() { |
| 772 | Ok(c) => c, |
| 773 | Err(e) => { |
| 774 | if matches!(e.kind(), ErrorKind::DisplayHelp | ErrorKind::DisplayVersion) { |
| 775 | e.exit(); |
| 776 | } |
| 777 | |
| 778 | let err_str = e.render().to_string(); |
| 779 | let clean_err = err_str.replace("For more information, try '--help'.", ""); |
| 780 | eprintln!("{}", clean_err.trim_end()); |
| 781 | println!(); |
| 782 | |
| 783 | // Show subcommand-specific help when the error is about a subcommand's args |
| 784 | let mut cmd = Cli::command(); |
| 785 | let sub_name = std::env::args_os().skip(1).find_map(|arg| { |
| 786 | let s = arg.to_string_lossy(); |
| 787 | if !s.starts_with('-') && cmd.get_subcommands().any(|c| c.get_name() == s) { |
| 788 | Some(s.into_owned()) |
| 789 | } else { |
| 790 | None |
| 791 | } |
| 792 | }); |
| 793 | match sub_name { |
| 794 | Some(name) => { |
| 795 | if let Some(sub_cmd) = cmd.find_subcommand_mut(&name) { |
| 796 | let _ = sub_cmd.print_help(); |
| 797 | } else { |
| 798 | let _ = cmd.print_help(); |
| 799 | } |
| 800 | } |
| 801 | None => { |
| 802 | let _ = cmd.print_help(); |
| 803 | } |
| 804 | } |
| 805 | std::process::exit(1); |
| 806 | } |
| 807 | }; |
| 808 | |
| 809 | // Handle kill-daemon without connecting to Chrome |
| 810 | if matches!(cli.command, Commands::KillDaemon) { |
| 811 | let pid_path = protocol::pid_path(); |
| 812 | #[cfg(unix)] |
| 813 | let sock_path = protocol::socket_path(); |
| 814 | match std::fs::read_to_string(&pid_path) { |
| 815 | Ok(pid_str) => { |
| 816 | let pid: u32 = pid_str |
| 817 | .trim() |
| 818 | .parse() |
| 819 | .map_err(|_| anyhow::anyhow!("Invalid PID in {}", pid_path.display()))?; |
| 820 | #[cfg(unix)] |
| 821 | { |
| 822 | // Refuse PID 0: kill(0, ...) signals every process in the |
| 823 | // caller's process group — it would take down this CLI and |
| 824 | // its siblings. A corrupted/truncated PID file could read "0". |
| 825 | if pid == 0 { |
| 826 | anyhow::bail!("PID in {} is 0; refusing to signal", pid_path.display()); |
| 827 | } |
no test coverage detected