From 012e40eff833cc57f7efeda12f83a2ccdfd29ade Mon Sep 17 00:00:00 2001 From: Andrey Golovizin Date: Wed, 14 Jul 2021 14:27:21 +0200 Subject: [PATCH] Refactor config printing --- src/main.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 96425ff..73e468a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) }