mirror of
https://github.com/aramperes/nut-rs.git
synced 2025-09-08 21:18:31 -04:00
parent
d78fd8c141
commit
d36999db6d
16 changed files with 494 additions and 104 deletions
|
@ -19,3 +19,4 @@ anyhow = "1"
|
|||
[dependencies.nut-client]
|
||||
version = "0.1.0"
|
||||
path = "../nut-client"
|
||||
features = ["ssl"]
|
||||
|
|
|
@ -15,6 +15,7 @@ Written using the [nut-client](https://github.com/aramperes/nut-client-rs) crate
|
|||
- List variables for a UPS device
|
||||
- Get variable value of a UPS device
|
||||
- List clients connected to a UPS device
|
||||
- Connect securely with SSL
|
||||
|
||||
## Installation
|
||||
|
||||
|
@ -58,6 +59,9 @@ However, there are also some additions:
|
|||
```bash
|
||||
# Enable network debugging (global flag).
|
||||
ruspc -D
|
||||
|
||||
# Enable SSL
|
||||
rupsc -S
|
||||
```
|
||||
|
||||
## Pronunciation
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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>]]")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue