aboutsummaryrefslogtreecommitdiff
path: root/src/wgcmd.rs
blob: 32a23a6540b07b590511e1c9e19ac077f4e07005 (plain)
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
use crate::wg::config::WireguardConfig;
use std::process::Command;
use anyhow::{anyhow, Result, Context};

pub fn run(wg_conf: &WireguardConfig) -> Result<()> {
    let wg = Command::new("wg")
        .env("WG_COLOR_MODE", "always")
        .output()
        .context("could not run 'wg', is it installed?")?;

    if !wg.status.success() {
        let err = String::from_utf8(wg.stderr)?;
        return Err(anyhow!("error running 'wg': {}", err));
    }   

    let mut out = String::from_utf8(wg.stdout).context("error parsing stdout")?;

    for peer in wg_conf.peers.iter() {
        out = out.replace(peer.public_key.as_str(), peer.name.as_str());
    }

    println!("{}", out);
    
    Ok(())
}