Add debug mode to rupsc. Fixes to parsing.

This commit is contained in:
Aram 🍐 2021-07-31 03:28:32 -04:00
parent d1d6b4e1a8
commit 52afe6bbd1
7 changed files with 129 additions and 79 deletions

View file

@ -4,11 +4,11 @@ use core::convert::TryInto;
use nut_client::blocking::Connection;
/// Lists each UPS on the upsd server, one per line.
pub fn list_devices(server: UpsdName, verbose: bool) -> anyhow::Result<()> {
let mut conn = connect(server)?;
pub fn list_devices(server: UpsdName, with_description: bool, debug: bool) -> anyhow::Result<()> {
let mut conn = connect(server, debug)?;
for (name, description) in conn.list_ups()? {
if verbose {
if with_description {
println!("{}: {}", name, description);
} else {
println!("{}", name);
@ -18,11 +18,11 @@ pub fn list_devices(server: UpsdName, verbose: bool) -> anyhow::Result<()> {
Ok(())
}
pub fn print_variable(server: UpsdName, variable: &str) -> anyhow::Result<()> {
pub fn print_variable(server: UpsdName, variable: &str, debug: bool) -> anyhow::Result<()> {
let ups_name = server
.upsname
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?;
let mut conn = connect(server)?;
let mut conn = connect(server, debug)?;
let variable = conn.get_var(ups_name, variable)?;
println!("{}", variable.value());
@ -30,11 +30,11 @@ pub fn print_variable(server: UpsdName, variable: &str) -> anyhow::Result<()> {
Ok(())
}
pub fn list_variables(server: UpsdName) -> anyhow::Result<()> {
pub fn list_variables(server: UpsdName, debug: bool) -> anyhow::Result<()> {
let ups_name = server
.upsname
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?;
let mut conn = connect(server)?;
let mut conn = connect(server, debug)?;
for var in conn.list_vars(ups_name)? {
println!("{}", var);
@ -43,8 +43,11 @@ pub fn list_variables(server: UpsdName) -> anyhow::Result<()> {
Ok(())
}
fn connect(server: UpsdName) -> anyhow::Result<Connection> {
fn connect(server: UpsdName, debug: bool) -> anyhow::Result<Connection> {
let host = server.try_into()?;
let config = nut_client::ConfigBuilder::new().with_host(host).build();
let config = nut_client::ConfigBuilder::new()
.with_host(host)
.with_debug(debug)
.build();
Connection::new(config).with_context(|| format!("Failed to connect to upsd: {}", server))
}

View file

@ -37,6 +37,12 @@ fn main() -> anyhow::Result<()> {
.takes_value(false)
.help("Lists each client connected on <upsname>, one per line."),
)
.arg(
Arg::with_name("debug")
.short("D")
.takes_value(false)
.help("Enables debug mode (logs network commands to stderr)."),
)
.arg(
Arg::with_name("upsd-server")
.required(false)
@ -56,12 +62,14 @@ fn main() -> anyhow::Result<()> {
|s| s.try_into().with_context(|| "Invalid upsd server name"),
)?;
let debug = args.is_present("debug");
if args.is_present("list") {
return cmd::list_devices(server, false);
return cmd::list_devices(server, false, debug);
}
if args.is_present("list-full") {
return cmd::list_devices(server, true);
return cmd::list_devices(server, true, debug);
}
if args.is_present("clients") {
@ -70,8 +78,8 @@ fn main() -> anyhow::Result<()> {
// Fallback: prints one variable (or all of them)
if let Some(variable) = args.value_of("variable") {
cmd::print_variable(server, variable)
cmd::print_variable(server, variable, debug)
} else {
cmd::list_variables(server)
cmd::list_variables(server, debug)
}
}

View file

@ -3,7 +3,7 @@ use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::net::ToSocketAddrs;
pub const DEFAULT_HOSTNAME: &str = "localhost";
pub const DEFAULT_HOSTNAME: &str = "127.0.0.1";
pub const DEFAULT_PORT: u16 = 3493;
/// Connection information for a upsd server.
@ -131,7 +131,7 @@ mod tests {
port: DEFAULT_PORT
}
);
assert_eq!(format!("{}", name), "ups0@localhost:3493");
assert_eq!(format!("{}", name), "ups0@127.0.0.1:3493");
}
#[test]