Refactor config printing

This commit is contained in:
Andrey Golovizin 2021-07-14 14:27:21 +02:00
parent 6a6581b64f
commit 012e40eff8

View file

@ -134,25 +134,29 @@ fn get_config(_opts: &Opts, config_opts: &ConfigOpts) -> Result<(), anyhow::Erro
])?
.into_json()?;
debug!("config = {:?}", &config);
write_config(&mut std::io::stdout().lock(), &config, &keys)
}
fn write_config(
output: &mut dyn Write,
config: &WireguardConfig,
keys: &WireguardKeyPair,
) -> Result<(), anyhow::Error> {
let mut endpoint_addrs = config.data.endpoint.to_socket_addrs()?;
let endpoint_addr = endpoint_addrs
.next()
.ok_or_else(|| anyhow::anyhow!("no endpoint address received"))?;
debug!("endpoint_addr = {:?}", &endpoint_addr);
println!(
r"[Interface]
PrivateKey = {private_key}
Address = {address}
writeln!(output, "[Interface]")?;
writeln!(output, "PrivateKey = {}", &keys.private_key)?;
writeln!(output, "Address = {}", &config.data.address)?;
writeln!(output, "[Peer]")?;
writeln!(output, "PublicKey = {}", &config.data.public_key)?;
writeln!(output, "Endpoint = {}", &endpoint_addr)?;
writeln!(output, "AllowedIPs = 0.0.0.0/0 #, ::/0")?;
[Peer]
PublicKey = {public_key}
Endpoint = {endpoint_addr}
AllowedIPs = 0.0.0.0/0 #, ::/0",
public_key = &config.data.public_key,
private_key = &keys.private_key,
address = &config.data.address,
endpoint_addr = &endpoint_addr,
);
Ok(())
}