meta: cargo fmt
parent
d431953353
commit
4a458a413c
|
@ -1,8 +1,16 @@
|
||||||
use crate::{CLIENT_CONFIG_DIR, Endpoint, Error, IoErrorContext, WrappedIoError, ensure_dirs_exist};
|
use crate::{
|
||||||
|
ensure_dirs_exist, Endpoint, Error, IoErrorContext, WrappedIoError, CLIENT_CONFIG_DIR,
|
||||||
|
};
|
||||||
use indoc::writedoc;
|
use indoc::writedoc;
|
||||||
use ipnetwork::IpNetwork;
|
use ipnetwork::IpNetwork;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{fs::{File, OpenOptions}, io::{self, Write}, net::SocketAddr, os::unix::fs::PermissionsExt, path::{Path, PathBuf}};
|
use std::{
|
||||||
|
fs::{File, OpenOptions},
|
||||||
|
io::{self, Write},
|
||||||
|
net::SocketAddr,
|
||||||
|
os::unix::fs::PermissionsExt,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
use wgctrl::InterfaceName;
|
use wgctrl::InterfaceName;
|
||||||
|
|
||||||
#[derive(Clone, Deserialize, Serialize, Debug)]
|
#[derive(Clone, Deserialize, Serialize, Debug)]
|
||||||
|
@ -75,8 +83,7 @@ impl InterfaceConfig {
|
||||||
"
|
"
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
target_file
|
target_file.write_all(toml::to_string(self).unwrap().as_bytes())?;
|
||||||
.write_all(toml::to_string(self).unwrap().as_bytes())?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +99,8 @@ impl InterfaceConfig {
|
||||||
.write(true)
|
.write(true)
|
||||||
.open(path)
|
.open(path)
|
||||||
.with_path(path)?;
|
.with_path(path)?;
|
||||||
self.write_to(&mut target_file, comments, mode).with_path(path)
|
self.write_to(&mut target_file, comments, mode)
|
||||||
|
.with_path(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Overwrites the config file if it already exists.
|
/// Overwrites the config file if it already exists.
|
||||||
|
|
|
@ -9,7 +9,14 @@ use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select};
|
||||||
use ipnetwork::IpNetwork;
|
use ipnetwork::IpNetwork;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use publicip::Preference;
|
use publicip::Preference;
|
||||||
use std::{fmt::{Debug, Display}, fs::{File, OpenOptions}, io, net::SocketAddr, str::FromStr, time::SystemTime};
|
use std::{
|
||||||
|
fmt::{Debug, Display},
|
||||||
|
fs::{File, OpenOptions},
|
||||||
|
io,
|
||||||
|
net::SocketAddr,
|
||||||
|
str::FromStr,
|
||||||
|
time::SystemTime,
|
||||||
|
};
|
||||||
use wgctrl::{InterfaceName, KeyPair};
|
use wgctrl::{InterfaceName, KeyPair};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
@ -20,7 +27,10 @@ pub fn ensure_interactive(prompt: &str) -> Result<(), io::Error> {
|
||||||
if atty::is(atty::Stream::Stdin) {
|
if atty::is(atty::Stream::Stdin) {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(io::Error::new(io::ErrorKind::BrokenPipe, format!("Prompt \"{}\" failed because TTY isn't connected.", prompt)))
|
Err(io::Error::new(
|
||||||
|
io::ErrorKind::BrokenPipe,
|
||||||
|
format!("Prompt \"{}\" failed because TTY isn't connected.", prompt),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +55,7 @@ pub fn select<'a, T: ToString>(prompt: &str, items: &'a [T]) -> Result<(usize, &
|
||||||
pub enum Prefill<T> {
|
pub enum Prefill<T> {
|
||||||
Default(T),
|
Default(T),
|
||||||
Editable(String),
|
Editable(String),
|
||||||
None
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn input<T>(prompt: &str, prefill: Prefill<T>) -> Result<T, io::Error>
|
pub fn input<T>(prompt: &str, prefill: Prefill<T>) -> Result<T, io::Error>
|
||||||
|
@ -58,8 +68,10 @@ where
|
||||||
match prefill {
|
match prefill {
|
||||||
Prefill::Default(value) => input.default(value),
|
Prefill::Default(value) => input.default(value),
|
||||||
Prefill::Editable(value) => input.with_initial_text(value),
|
Prefill::Editable(value) => input.with_initial_text(value),
|
||||||
_ => &mut input
|
_ => &mut input,
|
||||||
}.with_prompt(prompt).interact()
|
}
|
||||||
|
.with_prompt(prompt)
|
||||||
|
.interact()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bring up a prompt to create a new CIDR. Returns the peer request.
|
/// Bring up a prompt to create a new CIDR. Returns the peer request.
|
||||||
|
@ -246,13 +258,19 @@ pub fn add_peer(
|
||||||
let invite_expires = if let Some(ref invite_expires) = args.invite_expires {
|
let invite_expires = if let Some(ref invite_expires) = args.invite_expires {
|
||||||
invite_expires.clone()
|
invite_expires.clone()
|
||||||
} else {
|
} else {
|
||||||
input("Invite expires after", Prefill::Default("14d".parse().map_err(|s: &str| anyhow!(s))?))?
|
input(
|
||||||
|
"Invite expires after",
|
||||||
|
Prefill::Default("14d".parse().map_err(|s: &str| anyhow!(s))?),
|
||||||
|
)?
|
||||||
};
|
};
|
||||||
|
|
||||||
let invite_save_path = if let Some(ref location) = args.save_config {
|
let invite_save_path = if let Some(ref location) = args.save_config {
|
||||||
location.clone()
|
location.clone()
|
||||||
} else {
|
} else {
|
||||||
input("Save peer invitation file to", Prefill::Default(format!("{}.toml", name)))?
|
input(
|
||||||
|
"Save peer invitation file to",
|
||||||
|
Prefill::Default(format!("{}.toml", name)),
|
||||||
|
)?
|
||||||
};
|
};
|
||||||
|
|
||||||
let default_keypair = KeyPair::generate();
|
let default_keypair = KeyPair::generate();
|
||||||
|
@ -412,7 +430,10 @@ pub fn set_listen_port(
|
||||||
) -> Result<Option<Option<u16>>, Error> {
|
) -> Result<Option<Option<u16>>, Error> {
|
||||||
let listen_port = (!unset)
|
let listen_port = (!unset)
|
||||||
.then(|| {
|
.then(|| {
|
||||||
input("Listen port", Prefill::Default(interface.listen_port.unwrap_or(51820)))
|
input(
|
||||||
|
"Listen port",
|
||||||
|
Prefill::Default(interface.listen_port.unwrap_or(51820)),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.transpose()?;
|
.transpose()?;
|
||||||
|
|
||||||
|
@ -451,10 +472,13 @@ pub fn ask_endpoint() -> Result<Endpoint, Error> {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(input("External endpoint", match external_ip {
|
Ok(input(
|
||||||
Some(ip) => Prefill::Editable(SocketAddr::new(ip, 51820).to_string()),
|
"External endpoint",
|
||||||
None => Prefill::None
|
match external_ip {
|
||||||
})?)
|
Some(ip) => Prefill::Editable(SocketAddr::new(ip, 51820).to_string()),
|
||||||
|
None => Prefill::None,
|
||||||
|
},
|
||||||
|
)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn override_endpoint(unset: bool) -> Result<Option<Option<Endpoint>>, Error> {
|
pub fn override_endpoint(unset: bool) -> Result<Option<Option<Endpoint>>, Error> {
|
||||||
|
|
Loading…
Reference in New Issue