pbsbanner: use match and also return error

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2020-02-07 18:30:41 +01:00
parent 274b0c7bb7
commit e2b5e75a3e
1 changed files with 13 additions and 16 deletions

View File

@ -18,7 +18,8 @@ configure this server - connect to:
"" ""
); );
if let Ok(saddrs) = addr.to_socket_addrs() { let msg = match addr.to_socket_addrs() {
Ok(saddrs) => {
let saddrs: Vec<_> = saddrs let saddrs: Vec<_> = saddrs
.filter_map(|s| match !s.ip().is_loopback() { .filter_map(|s| match !s.ip().is_loopback() {
true => Some(format!(" https://{}/", s)), true => Some(format!(" https://{}/", s)),
@ -27,22 +28,18 @@ configure this server - connect to:
.collect(); .collect();
if !saddrs.is_empty() { if !saddrs.is_empty() {
writeln!(&mut banner, "{}", saddrs.join("\n")).unwrap(); saddrs.join("\n")
} else { } else {
writeln!( format!("hostname '{}' does not resolves to any non-loopback address", nodename)
&mut banner,
"hostname '{}' does not resolves to any non-loopback address",
nodename
)
.unwrap();
}
} else {
writeln!(&mut banner, "could not resolve hostname '{}'", nodename).unwrap();
} }
},
Err(e) => format!("could not resolve hostname '{}': {}", nodename, e),
};
banner += &msg;
// unwrap will never fail for write!: // unwrap will never fail for write!:
// https://github.com/rust-lang/rust/blob/1.39.0/src/liballoc/string.rs#L2318-L2331 // https://github.com/rust-lang/rust/blob/1.39.0/src/liballoc/string.rs#L2318-L2331
write!(&mut banner, "\n{:-<78}\n\n", "").unwrap(); write!(&mut banner, "\n\n{:-<78}\n\n", "").unwrap();
fs::write("/etc/issue", banner.as_bytes()).expect("Unable to write banner to issue file"); fs::write("/etc/issue", banner.as_bytes()).expect("Unable to write banner to issue file");
} }