setdns 0.1.1

Temporarily apply and restore system DNS configuration, including global and split DNS.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::process::Command;

pub(crate) fn flush_dns_cache() {
    run_command("dscacheutil", &["-flushcache"], "flush macOS DNS cache");
    run_command(
        "killall",
        &["-HUP", "mDNSResponder"],
        "reload mDNSResponder",
    );
}

fn run_command(command: &str, args: &[&str], action: &'static str) {
    match Command::new(command).args(args).status() {
        Ok(status) if status.success() => log::debug!("{action} succeeded"),
        Ok(status) => log::warn!("failed to {action}: {command} exited with {status}"),
        Err(err) => log::warn!("failed to launch {command} to {action}: {err}"),
    }
}