Skip to content

Commit

Permalink
fix(pet-monitor-app): switch from clap to xflags
Browse files Browse the repository at this point in the history
We don't need most of clap's features and xflags is much lighter.
  • Loading branch information
Stonks3141 committed Nov 2, 2023
1 parent 7742587 commit 3bd9967
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 153 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
root = true

[*]
charset = utf-8
line_endings = lf
insert_final_newline = true
indent_style = tab
indent_size = 4

[*.rs]
indent_style = space

[*.yml]
indent_style = space
indent_size = 2

[*.css,*.html]
indent_style = space
indent_size = 2
117 changes: 16 additions & 101 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-tree = "0.2"
tracing-error = "0.2"
include_dir = "0.7"
clap = { version = "4.3", features = ["derive"] }
xflags = "0.3"
confy = "0.5"
termion = "2.0"
serde_with = { version = "3.3", features = ["base64"] }
Expand Down
13 changes: 8 additions & 5 deletions crates/mp4-stream/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,26 @@ pub enum Rotation {
}

#[cfg(feature = "serde")]
impl Serialize for Format {
impl Serialize for Rotation {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
(self as u32).serialize(s)
(*self as u32).serialize(s)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Rotation {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
match u32::deserialize(d) {
Ok(match u32::deserialize(d)? {
0 => Self::R0,
90 => Self::R90,
180 => Self::R180,
270 => Self::R270,
x => {
D::Error::invalid_value(Unexpected::Unsigned(x as u64), "one of 0, 90, 180, or 270")
return Err(D::Error::invalid_value(
Unexpected::Unsigned(x as u64),
&"one of 0, 90, 180, or 270",
));
}
}
})
}
}
4 changes: 2 additions & 2 deletions crates/pet-monitor-app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pet-monitor-app"
version = "0.3.1"
description = "A simple and secure pet monitor for Raspberry Pi"
description = "A simple and secure pet monitor for Linux"
documentation = "https://github.com/Stonks3141/pet-monitor-app"
readme = "../../README.md"
keywords = ["cli", "video", "server", "streaming", "media"]
Expand All @@ -27,7 +27,7 @@ tracing-subscriber.workspace = true
tracing-tree.workspace = true
tracing-error.workspace = true
include_dir.workspace = true
clap.workspace = true
xflags.workspace = true
confy.workspace = true
termion.workspace = true
serde.workspace = true
Expand Down
29 changes: 0 additions & 29 deletions crates/pet-monitor-app/src/bin/pet-monitor-app/cli.rs

This file was deleted.

58 changes: 44 additions & 14 deletions crates/pet-monitor-app/src/bin/pet-monitor-app/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#![warn(clippy::unimplemented)]
#![warn(clippy::dbg_macro)]

mod cli;

use clap::Parser;
use color_eyre::eyre;
use pet_monitor_app::config;
use ring::rand::{SecureRandom, SystemRandom};
use std::io::{stdin, stdout};
use std::{
io::{stdin, stdout},
path::PathBuf,
};
use termion::input::TermRead;
use tokio::task::spawn_blocking;
use tracing_error::ErrorLayer;
Expand Down Expand Up @@ -46,7 +46,36 @@ const ARGON2_CONFIG: argon2::Config = argon2::Config {
#[tokio::main(flavor = "current_thread")]
async fn main() -> eyre::Result<()> {
color_eyre::install()?;
let cmd = cli::Cmd::parse();

xflags::xflags! {
/// A simple and secure pet monitor for Linux
cmd pet-monitor-app {
/// Path to the configuration file to use
optional -c, --config path: PathBuf

/// Print the version and exit
cmd version {}
/// Set the password (reads from stdin)
cmd set-password {}
/// Regenerate the authentication secret
cmd regen-secret {}
/// Start the server
cmd start {
/// Set the port to listen on
optional -p, --port port: u16
/// Disable video streaming
optional --no-stream
}
}
}

let flags = PetMonitorApp::from_env_or_exit();

if let PetMonitorAppCmd::Version(_) = flags.subcommand {
println!("{}", env!("CARGO_PKG_VERSION"));
return Ok(());
}

Registry::default()
.with(EnvFilter::from_default_env())
.with(
Expand All @@ -60,20 +89,21 @@ async fn main() -> eyre::Result<()> {

let rng = SystemRandom::new();

let mut ctx = config::load(cmd.conf_path.clone()).await?;
let mut ctx = config::load(flags.config.clone()).await?;

if ctx.jwt_secret == [0; 32] {
rng.fill(&mut ctx.jwt_secret)?;
config::store(cmd.conf_path.clone(), ctx.clone()).await?;
config::store(flags.config.clone(), ctx.clone()).await?;
}

match cmd.command {
cli::SubCmd::RegenSecret => {
match flags.subcommand {
PetMonitorAppCmd::Version(_) => unreachable!(),
PetMonitorAppCmd::RegenSecret(_) => {
rng.fill(&mut ctx.jwt_secret)?;
config::store(cmd.conf_path.clone(), ctx.clone()).await?;
config::store(flags.config.clone(), ctx.clone()).await?;
eprintln!("Successfully regenerated JWT signing secret.");
}
cli::SubCmd::SetPassword => {
PetMonitorAppCmd::SetPassword(_) => {
eprint!("Enter password: ");
let Some(password) = stdin().read_passwd(&mut stdout())? else {
eprintln!("\nNo password entered.");
Expand All @@ -90,14 +120,14 @@ async fn main() -> eyre::Result<()> {
argon2::hash_encoded(password.as_bytes(), &buf, &ARGON2_CONFIG)
})
.await??;
config::store(cmd.conf_path.clone(), ctx.clone()).await?;
config::store(flags.config.clone(), ctx.clone()).await?;
eprintln!("Successfully reset password.");
}
cli::SubCmd::Start { stream, port } => {
PetMonitorAppCmd::Start(Start { no_stream, port }) => {
if let Some(port) = port {
ctx.port = port;
}
pet_monitor_app::start(cmd.conf_path, ctx, stream).await?;
pet_monitor_app::start(flags.config, ctx, !no_stream).await?;
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.69.0"
channel = "1.72.0"
components = [ "rustfmt", "clippy" ]

0 comments on commit 3bd9967

Please sign in to comment.