Re-unify configuration files
Unfortunately the grammar of the --config parameter is a bit of a football right now; previously you were allowed to specify it anywhere because there wasn't much in the way of a CLI, then when subcommands were added it became required to specify it after the subcommand. This change makes it so that it must be specified *before* the subcommand. Ideally it would just be required *somewhere* in the command line but clap doesn't support "global" for required arguments (and it is indeed required). On the whole, I'm okay with the change - the net win of having a unified config struct makes up for the ergonomics hit in my opinion, and it unlocks some further refactoring I will do next.
This commit is contained in:
1 parent
f10f9f9706
commit
de39e7ed2a
14 files changed
+40
-237
No files matched your search
+1
-1
@@ -6,7 +6,7 @@ use structopt::{clap::AppSettings, StructOpt};
|
||||
#[structopt(name = "neolink", setting(AppSettings::ArgRequiredElseHelp))]
|
||||
pub struct Opt {
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
pub config: Option<PathBuf>,
|
||||
pub config: PathBuf,
|
||||
#[structopt(subcommand)]
|
||||
pub cmd: Option<Command>,
|
||||
}
|
||||
|
||||
File renamed without changes.
+26
-17
@@ -7,12 +7,15 @@
|
||||
//! It contains sub commands for running an rtsp proxy which can be used on Reolink cameras
|
||||
//! that do not nativly support RTSP.
|
||||
//!
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
use env_logger::Env;
|
||||
use log::*;
|
||||
use std::fs;
|
||||
use structopt::StructOpt;
|
||||
use validator::Validate;
|
||||
|
||||
mod cmdline;
|
||||
mod config;
|
||||
mod reboot;
|
||||
mod rtsp;
|
||||
mod statusled;
|
||||
@@ -20,6 +23,7 @@ mod talk;
|
||||
mod utils;
|
||||
|
||||
use cmdline::{Command, Opt};
|
||||
use config::Config;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||
@@ -32,30 +36,35 @@ fn main() -> Result<()> {
|
||||
|
||||
let opt = Opt::from_args();
|
||||
|
||||
match (opt.cmd, opt.config) {
|
||||
(None, None) => {
|
||||
// Should be caught at the clap validation
|
||||
unreachable!();
|
||||
}
|
||||
(None, Some(config)) => {
|
||||
let config: Config = toml::from_str(
|
||||
&fs::read_to_string(&opt.config)
|
||||
.with_context(|| format!("Failed to read {:?}", &opt.config))?,
|
||||
)
|
||||
.with_context(|| format!("Failed to load {:?} as a config file", &opt.config))?;
|
||||
|
||||
config
|
||||
.validate()
|
||||
.with_context(|| format!("Failed to validate the {:?} config file", &opt.config))?;
|
||||
|
||||
match opt.cmd {
|
||||
None => {
|
||||
warn!(
|
||||
"Deprecated command line option. Please use: `neolink rtsp --config={:?}`",
|
||||
config
|
||||
);
|
||||
rtsp::main(rtsp::Opt { config })?;
|
||||
rtsp::main(rtsp::Opt {}, config)?;
|
||||
}
|
||||
(Some(_), Some(_)) => error!("--config should be given after the subcommand"),
|
||||
(Some(Command::Rtsp(opts)), None) => {
|
||||
rtsp::main(opts)?;
|
||||
Some(Command::Rtsp(opts)) => {
|
||||
rtsp::main(opts, config)?;
|
||||
}
|
||||
(Some(Command::StatusLight(opts)), None) => {
|
||||
statusled::main(opts)?;
|
||||
Some(Command::StatusLight(opts)) => {
|
||||
statusled::main(opts, config)?;
|
||||
}
|
||||
(Some(Command::Reboot(opts)), None) => {
|
||||
reboot::main(opts)?;
|
||||
Some(Command::Reboot(opts)) => {
|
||||
reboot::main(opts, config)?;
|
||||
}
|
||||
(Some(Command::Talk(opts)), None) => {
|
||||
talk::main(opts)?;
|
||||
Some(Command::Talk(opts)) => {
|
||||
talk::main(opts, config)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
/// The reboot command will reboot the camera
|
||||
#[derive(StructOpt, Debug)]
|
||||
pub struct Opt {
|
||||
/// The path to the config file
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
pub config: PathBuf,
|
||||
/// The name of the camera to change the lights of. Must be a name in the config
|
||||
pub camera: String,
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
use serde::Deserialize;
|
||||
use std::clone::Clone;
|
||||
use validator::{Validate, ValidationError};
|
||||
use validator_derive::Validate;
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
pub(crate) struct Config {
|
||||
#[validate]
|
||||
pub(crate) cameras: Vec<CameraConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
#[validate(schema(function = "validate_camera_config"))]
|
||||
pub(crate) struct CameraConfig {
|
||||
pub(crate) name: String,
|
||||
|
||||
#[serde(rename = "address")]
|
||||
pub(crate) camera_addr: Option<String>,
|
||||
|
||||
#[serde(rename = "uid")]
|
||||
pub(crate) camera_uid: Option<String>,
|
||||
|
||||
pub(crate) username: String,
|
||||
pub(crate) password: Option<String>,
|
||||
|
||||
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
|
||||
#[serde(default = "default_channel_id")]
|
||||
pub(crate) channel_id: u8,
|
||||
}
|
||||
|
||||
fn default_channel_id() -> u8 {
|
||||
0
|
||||
}
|
||||
|
||||
fn validate_camera_config(camera_config: &CameraConfig) -> Result<(), ValidationError> {
|
||||
match (&camera_config.camera_addr, &camera_config.camera_uid) {
|
||||
(None, None) => Err(ValidationError::new(
|
||||
"Either camera address or uid must be given",
|
||||
)),
|
||||
(Some(_), Some(_)) => Err(ValidationError::new(
|
||||
"Must provide either camera address or uid not both",
|
||||
)),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
+3
-17
@@ -13,30 +13,17 @@
|
||||
///
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::*;
|
||||
use std::fs;
|
||||
use validator::Validate;
|
||||
|
||||
mod cmdline;
|
||||
mod config;
|
||||
|
||||
use super::config::Config;
|
||||
use crate::utils::AddressOrUid;
|
||||
pub(crate) use cmdline::Opt;
|
||||
use config::Config;
|
||||
|
||||
/// Entry point for the reboot subcommand
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub fn main(opt: Opt) -> Result<()> {
|
||||
let config: Config = toml::from_str(
|
||||
&fs::read_to_string(&opt.config)
|
||||
.with_context(|| format!("Failed to read {:?}", &opt.config))?,
|
||||
)
|
||||
.with_context(|| format!("Failed to load {:?} as a config file", &opt.config))?;
|
||||
|
||||
config
|
||||
.validate()
|
||||
.with_context(|| format!("Failed to validate the {:?} config file", &opt.config))?;
|
||||
|
||||
pub(crate) fn main(opt: Opt, config: Config) -> Result<()> {
|
||||
let mut cam_found = false;
|
||||
for camera_config in &config.cameras {
|
||||
if opt.camera == camera_config.name {
|
||||
@@ -72,9 +59,8 @@ pub fn main(opt: Opt) -> Result<()> {
|
||||
|
||||
if !cam_found {
|
||||
Err(anyhow!(
|
||||
"Camera {} was not in the config file {:?}",
|
||||
"Camera {} not found in the config file",
|
||||
&opt.camera,
|
||||
&opt.config
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
|
||||
+1
-6
@@ -1,10 +1,5 @@
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
/// The rtsp command will serve all cameras in the config over the rtsp protocol
|
||||
#[derive(StructOpt, Debug)]
|
||||
pub struct Opt {
|
||||
/// The path to the config file
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
pub config: PathBuf,
|
||||
}
|
||||
pub struct Opt {}
|
||||
+2
-15
@@ -33,37 +33,24 @@ use log::*;
|
||||
use neolink_core::bc_protocol::{BcCamera, Stream};
|
||||
use neolink_core::Never;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use validator::Validate;
|
||||
|
||||
// mod adpcm;
|
||||
/// The command line parameters for this subcommand
|
||||
mod cmdline;
|
||||
mod config;
|
||||
/// The errors this subcommand can raise
|
||||
mod gst;
|
||||
|
||||
use super::config::{CameraConfig, Config, UserConfig};
|
||||
use crate::utils::AddressOrUid;
|
||||
pub(crate) use cmdline::Opt;
|
||||
use config::{CameraConfig, Config, UserConfig};
|
||||
use gst::{GstOutputs, RtspServer, TlsAuthenticationMode};
|
||||
|
||||
/// Entry point for the rtsp subcommand
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub fn main(opt: Opt) -> Result<()> {
|
||||
let config: Config = toml::from_str(
|
||||
&fs::read_to_string(&opt.config)
|
||||
.with_context(|| format!("Failed to read {:?}", &opt.config))?,
|
||||
)
|
||||
.with_context(|| format!("Failed to load {:?} as a config file", &opt.config))?;
|
||||
|
||||
config
|
||||
.validate()
|
||||
.with_context(|| format!("Failed to validate the {:?} config file", &opt.config))?;
|
||||
|
||||
pub(crate) fn main(_opt: Opt, config: Config) -> Result<()> {
|
||||
let rtsp = &RtspServer::new();
|
||||
|
||||
set_up_tls(&config, rtsp);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
fn onoff_parse(src: &str) -> Result<bool> {
|
||||
@@ -16,9 +15,6 @@ fn onoff_parse(src: &str) -> Result<bool> {
|
||||
/// The status-light command will control the blue status light on the camera
|
||||
#[derive(StructOpt, Debug)]
|
||||
pub struct Opt {
|
||||
/// The path to the config file
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
pub config: PathBuf,
|
||||
/// The name of the camera to change the lights of. Must be a name in the config
|
||||
pub camera: String,
|
||||
/// Whether to turn the light on or off
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
use serde::Deserialize;
|
||||
use std::clone::Clone;
|
||||
use validator::{Validate, ValidationError};
|
||||
use validator_derive::Validate;
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
pub(crate) struct Config {
|
||||
#[validate]
|
||||
pub(crate) cameras: Vec<CameraConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
#[validate(schema(function = "validate_camera_config"))]
|
||||
pub(crate) struct CameraConfig {
|
||||
pub(crate) name: String,
|
||||
|
||||
#[serde(rename = "address")]
|
||||
pub(crate) camera_addr: Option<String>,
|
||||
|
||||
#[serde(rename = "uid")]
|
||||
pub(crate) camera_uid: Option<String>,
|
||||
|
||||
pub(crate) username: String,
|
||||
pub(crate) password: Option<String>,
|
||||
|
||||
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
|
||||
#[serde(default = "default_channel_id")]
|
||||
pub(crate) channel_id: u8,
|
||||
}
|
||||
|
||||
fn default_channel_id() -> u8 {
|
||||
0
|
||||
}
|
||||
|
||||
fn validate_camera_config(camera_config: &CameraConfig) -> Result<(), ValidationError> {
|
||||
match (&camera_config.camera_addr, &camera_config.camera_uid) {
|
||||
(None, None) => Err(ValidationError::new(
|
||||
"Either camera address or uid must be given",
|
||||
)),
|
||||
(Some(_), Some(_)) => Err(ValidationError::new(
|
||||
"Must provide either camera address or uid not both",
|
||||
)),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
+3
-17
@@ -17,30 +17,17 @@
|
||||
///
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::*;
|
||||
use std::fs;
|
||||
use validator::Validate;
|
||||
|
||||
mod cmdline;
|
||||
mod config;
|
||||
|
||||
use super::config::Config;
|
||||
use crate::utils::AddressOrUid;
|
||||
pub(crate) use cmdline::Opt;
|
||||
use config::Config;
|
||||
|
||||
/// Entry point for the ledstatus subcommand
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub fn main(opt: Opt) -> Result<()> {
|
||||
let config: Config = toml::from_str(
|
||||
&fs::read_to_string(&opt.config)
|
||||
.with_context(|| format!("Failed to read {:?}", &opt.config))?,
|
||||
)
|
||||
.with_context(|| format!("Failed to load {:?} as a config file", &opt.config))?;
|
||||
|
||||
config
|
||||
.validate()
|
||||
.with_context(|| format!("Failed to validate the {:?} config file", &opt.config))?;
|
||||
|
||||
pub(crate) fn main(opt: Opt, config: Config) -> Result<()> {
|
||||
let mut cam_found = false;
|
||||
for camera_config in &config.cameras {
|
||||
if opt.camera == camera_config.name {
|
||||
@@ -77,9 +64,8 @@ pub fn main(opt: Opt) -> Result<()> {
|
||||
|
||||
if !cam_found {
|
||||
Err(anyhow!(
|
||||
"Camera {} was not in the config file {:?}",
|
||||
"Camera {} not found in the config file",
|
||||
&opt.camera,
|
||||
&opt.config
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
|
||||
@@ -9,9 +9,6 @@ use structopt::StructOpt;
|
||||
/// `gst-launch` can be used to prepare this data
|
||||
#[derive(StructOpt, Debug)]
|
||||
pub struct Opt {
|
||||
/// The path to the config file
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
pub config: PathBuf,
|
||||
/// The name of the camera to change the lights of. Must be a name in the config
|
||||
pub camera: String,
|
||||
/// The path to the audio file.
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
use serde::Deserialize;
|
||||
use std::clone::Clone;
|
||||
use validator::{Validate, ValidationError};
|
||||
use validator_derive::Validate;
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
pub(crate) struct Config {
|
||||
#[validate]
|
||||
pub(crate) cameras: Vec<CameraConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
#[validate(schema(function = "validate_camera_config"))]
|
||||
pub(crate) struct CameraConfig {
|
||||
pub(crate) name: String,
|
||||
|
||||
#[serde(rename = "address")]
|
||||
pub(crate) camera_addr: Option<String>,
|
||||
|
||||
#[serde(rename = "uid")]
|
||||
pub(crate) camera_uid: Option<String>,
|
||||
|
||||
pub(crate) username: String,
|
||||
pub(crate) password: Option<String>,
|
||||
|
||||
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
|
||||
#[serde(default = "default_channel_id")]
|
||||
pub(crate) channel_id: u8,
|
||||
}
|
||||
|
||||
fn default_channel_id() -> u8 {
|
||||
0
|
||||
}
|
||||
|
||||
fn validate_camera_config(camera_config: &CameraConfig) -> Result<(), ValidationError> {
|
||||
match (&camera_config.camera_addr, &camera_config.camera_uid) {
|
||||
(None, None) => Err(ValidationError::new(
|
||||
"Either camera address or uid must be given",
|
||||
)),
|
||||
(Some(_), Some(_)) => Err(ValidationError::new(
|
||||
"Must provide either camera address or uid not both",
|
||||
)),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
+4
-18
@@ -8,37 +8,24 @@
|
||||
/// # Usage
|
||||
///
|
||||
/// ```bash
|
||||
/// neolink status-light --config=config.toml --adpcm-file=data.adpcm --sample-rate=16000 --block-size=512 CameraName
|
||||
/// neolink talk --config=config.toml --adpcm-file=data.adpcm --sample-rate=16000 --block-size=512 CameraName
|
||||
/// ```
|
||||
///
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::*;
|
||||
use neolink_core::bc::xml::TalkConfig;
|
||||
use std::fs;
|
||||
use validator::Validate;
|
||||
|
||||
mod cmdline;
|
||||
mod config;
|
||||
mod gst;
|
||||
|
||||
use super::config::Config;
|
||||
use crate::utils::AddressOrUid;
|
||||
pub(crate) use cmdline::Opt;
|
||||
use config::Config;
|
||||
|
||||
/// Entry point for the talk subcommand
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub fn main(opt: Opt) -> Result<()> {
|
||||
let config: Config = toml::from_str(
|
||||
&fs::read_to_string(&opt.config)
|
||||
.with_context(|| format!("Failed to read {:?}", &opt.config))?,
|
||||
)
|
||||
.with_context(|| format!("Failed to load {:?} as a config file", &opt.config))?;
|
||||
|
||||
config
|
||||
.validate()
|
||||
.with_context(|| format!("Failed to validate the {:?} config file", &opt.config))?;
|
||||
|
||||
pub(crate) fn main(opt: Opt, config: Config) -> Result<()> {
|
||||
let mut cam_found = false;
|
||||
for camera_config in &config.cameras {
|
||||
if opt.camera == camera_config.name {
|
||||
@@ -128,9 +115,8 @@ pub fn main(opt: Opt) -> Result<()> {
|
||||
|
||||
if !cam_found {
|
||||
Err(anyhow!(
|
||||
"Camera {} was not in the config file {:?}",
|
||||
"Camera {} not found in the config file",
|
||||
&opt.camera,
|
||||
&opt.config
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
|
||||
Reference in new issue
Block a user