Add SSL support (#7)

Fixes #1
This commit is contained in:
Aram Peres 2021-07-31 08:43:26 -04:00 committed by GitHub
parent d78fd8c141
commit d36999db6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 494 additions and 104 deletions

View file

@ -1,11 +1,11 @@
use crate::parser::UpsdName;
use anyhow::Context;
use core::convert::TryInto;
use nut_client::blocking::Connection;
use nut_client::Config;
/// Lists each UPS on the upsd server, one per line.
pub fn list_devices(server: UpsdName, with_description: bool, debug: bool) -> anyhow::Result<()> {
let mut conn = connect(server, debug)?;
pub fn list_devices(config: Config, with_description: bool) -> anyhow::Result<()> {
let mut conn = connect(config)?;
for (name, description) in conn.list_ups()? {
if with_description {
@ -18,11 +18,8 @@ pub fn list_devices(server: UpsdName, with_description: bool, debug: bool) -> an
Ok(())
}
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, debug)?;
pub fn print_variable(config: Config, ups_name: &str, variable: &str) -> anyhow::Result<()> {
let mut conn = connect(config)?;
let variable = conn.get_var(ups_name, variable)?;
println!("{}", variable.value());
@ -30,11 +27,8 @@ pub fn print_variable(server: UpsdName, variable: &str, debug: bool) -> anyhow::
Ok(())
}
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, debug)?;
pub fn list_variables(config: Config, ups_name: &str) -> anyhow::Result<()> {
let mut conn = connect(config)?;
for var in conn.list_vars(ups_name)? {
println!("{}", var);
@ -43,11 +37,8 @@ pub fn list_variables(server: UpsdName, debug: bool) -> anyhow::Result<()> {
Ok(())
}
pub fn list_clients(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, debug)?;
pub fn list_clients(config: Config, ups_name: &str) -> anyhow::Result<()> {
let mut conn = connect(config)?;
for client_ip in conn.list_clients(ups_name)? {
println!("{}", client_ip);
@ -56,11 +47,6 @@ pub fn list_clients(server: UpsdName, debug: bool) -> anyhow::Result<()> {
Ok(())
}
fn connect(server: UpsdName, debug: bool) -> anyhow::Result<Connection> {
let host = server.try_into()?;
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))
fn connect(config: Config) -> anyhow::Result<Connection> {
Connection::new(&config).with_context(|| format!("Failed to connect to upsd: {:?}", &config))
}

View file

@ -3,13 +3,14 @@
///! This a Rust clone of [upsc](https://github.com/networkupstools/nut/blob/master/clients/upsc.c).
///!
///! P.S.: pronounced "r-oopsie".
mod cmd;
mod parser;
use core::convert::TryInto;
use crate::parser::UpsdName;
use anyhow::Context;
use clap::{App, Arg};
use core::convert::TryInto;
use crate::parser::UpsdName;
mod cmd;
mod parser;
fn main() -> anyhow::Result<()> {
let args = App::new(clap::crate_name!())
@ -43,6 +44,12 @@ fn main() -> anyhow::Result<()> {
.takes_value(false)
.help("Enables debug mode (logs network commands to stderr)."),
)
.arg(
Arg::with_name("ssl")
.short("S")
.takes_value(false)
.help("Enables SSL on the connection with upsd."),
)
.arg(
Arg::with_name("upsd-server")
.required(false)
@ -63,23 +70,37 @@ fn main() -> anyhow::Result<()> {
)?;
let debug = args.is_present("debug");
let ssl = args.is_present("ssl");
let host = server.try_into()?;
let config = nut_client::ConfigBuilder::new()
.with_host(host)
.with_debug(debug)
.with_ssl(ssl)
.build();
if args.is_present("list") {
return cmd::list_devices(server, false, debug);
return cmd::list_devices(config, false);
}
if args.is_present("list-full") {
return cmd::list_devices(server, true, debug);
return cmd::list_devices(config, true);
}
if args.is_present("clients") {
return cmd::list_clients(server, debug);
return cmd::list_clients(config, get_ups_name(&server)?);
}
// Fallback: prints one variable (or all of them)
if let Some(variable) = args.value_of("variable") {
cmd::print_variable(server, variable, debug)
cmd::print_variable(config, get_ups_name(&server)?, variable)
} else {
cmd::list_variables(server, debug)
cmd::list_variables(config, get_ups_name(&server)?)
}
}
fn get_ups_name<'a>(server: &'a UpsdName) -> anyhow::Result<&'a str> {
server
.upsname
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")
}