bpaf improvements

* use latest published crate. This is a build fix: it was broken
  when the `exit_code` branch was deleted from the `bpaf` repo, even
  though the commit still exists!
  https://github.com/scottlamb/moonfire-nvr/pull/259

* clean up `db-dir` parsing redundancy, as suggested here:
  https://github.com/pacak/bpaf/discussions/165#discussioncomment-4983158
This commit is contained in:
Scott Lamb 2023-02-15 08:17:55 -08:00
parent ad48cf2e10
commit 0ffda11d4b
No known key found for this signature in database
10 changed files with 24 additions and 34 deletions

6
server/Cargo.lock generated
View File

@ -147,7 +147,8 @@ dependencies = [
[[package]] [[package]]
name = "bpaf" name = "bpaf"
version = "0.7.9" version = "0.7.9"
source = "git+https://github.com/pacak/bpaf.git?branch=exit_code#5238a6069abca0d61cf1c000e56bbac946e7ee18" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31f4c9de9c67618395106c81fb9461290a8910af29aa0188daec29001a1181ae"
dependencies = [ dependencies = [
"bpaf_derive", "bpaf_derive",
"owo-colors", "owo-colors",
@ -156,7 +157,8 @@ dependencies = [
[[package]] [[package]]
name = "bpaf_derive" name = "bpaf_derive"
version = "0.3.4" version = "0.3.4"
source = "git+https://github.com/pacak/bpaf.git?branch=exit_code#5238a6069abca0d61cf1c000e56bbac946e7ee18" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "223f3c9e7034f98c9f315d9945fcc22831b3f03d9f4c42c96a7ab6abd209a195"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -24,7 +24,7 @@ members = ["base", "db"]
base = { package = "moonfire-base", path = "base" } base = { package = "moonfire-base", path = "base" }
base64 = "0.13.0" base64 = "0.13.0"
blake3 = "1.0.0" blake3 = "1.0.0"
bpaf = { git = "https://github.com/pacak/bpaf.git", branch = "exit_code", features = ["autocomplete", "bright-color", "derive"]} bpaf = { version = "0.7.9", features = ["autocomplete", "bright-color", "derive"]}
bytes = "1" bytes = "1"
byteorder = "1.0" byteorder = "1.0"
cursive = "0.20.0" cursive = "0.20.0"

View File

@ -13,10 +13,7 @@ use std::path::PathBuf;
#[derive(Bpaf, Debug)] #[derive(Bpaf, Debug)]
#[bpaf(options)] #[bpaf(options)]
pub struct Args { pub struct Args {
/// Directory holding the SQLite3 index database. #[bpaf(external(crate::parse_db_dir))]
///
/// default: `/var/lib/moonfire-nvr/db`.
#[bpaf(argument("PATH"), fallback_with(crate::default_db_dir))]
db_dir: PathBuf, db_dir: PathBuf,
/// Compares sample file lengths on disk to the database. /// Compares sample file lengths on disk to the database.

View File

@ -23,10 +23,7 @@ mod users;
#[derive(Bpaf, Debug)] #[derive(Bpaf, Debug)]
#[bpaf(options)] #[bpaf(options)]
pub struct Args { pub struct Args {
/// Directory holding the SQLite3 index database. #[bpaf(external(crate::parse_db_dir))]
///
/// default: `/var/lib/moonfire-nvr/db`.
#[bpaf(argument("PATH"), fallback_with(crate::default_db_dir))]
db_dir: PathBuf, db_dir: PathBuf,
} }

View File

@ -11,10 +11,7 @@ use std::path::PathBuf;
#[derive(Bpaf, Debug)] #[derive(Bpaf, Debug)]
#[bpaf(options)] #[bpaf(options)]
pub struct Args { pub struct Args {
/// Directory holding the SQLite3 index database. #[bpaf(external(crate::parse_db_dir))]
///
/// default: `/var/lib/moonfire-nvr/db`.
#[bpaf(argument("PATH"), fallback_with(crate::default_db_dir))]
db_dir: PathBuf, db_dir: PathBuf,
} }

View File

@ -33,10 +33,7 @@ fn parse_flags(flags: String) -> Result<Vec<SessionFlag>, Error> {
#[derive(Bpaf, Debug, PartialEq, Eq)] #[derive(Bpaf, Debug, PartialEq, Eq)]
#[bpaf(options)] #[bpaf(options)]
pub struct Args { pub struct Args {
/// Directory holding the SQLite3 index database. #[bpaf(external(crate::parse_db_dir))]
///
/// default: `/var/lib/moonfire-nvr/db`.
#[bpaf(argument("PATH"), fallback_with(crate::default_db_dir))]
db_dir: PathBuf, db_dir: PathBuf,
/// Creates a session with the given permissions, as a JSON object. /// Creates a session with the given permissions, as a JSON object.
@ -173,7 +170,7 @@ mod tests {
assert_eq!( assert_eq!(
args, args,
Args { Args {
db_dir: crate::default_db_dir().unwrap(), db_dir: crate::DEFAULT_DB_DIR.into(),
domain: None, domain: None,
curl_cookie_jar: None, curl_cookie_jar: None,
permissions: Some(crate::json::Permissions { permissions: Some(crate::json::Permissions {

View File

@ -12,7 +12,7 @@ use serde::Deserialize;
use crate::json::Permissions; use crate::json::Permissions;
fn default_db_dir() -> PathBuf { fn default_db_dir() -> PathBuf {
"/var/lib/moonfire-nvr/db".into() crate::DEFAULT_DB_DIR.into()
} }
fn default_ui_dir() -> PathBuf { fn default_ui_dir() -> PathBuf {

View File

@ -20,10 +20,7 @@ use std::process::Command;
#[derive(Bpaf, Debug, PartialEq, Eq)] #[derive(Bpaf, Debug, PartialEq, Eq)]
#[bpaf(options)] #[bpaf(options)]
pub struct Args { pub struct Args {
/// Directory holding the SQLite3 index database. #[bpaf(external(crate::parse_db_dir))]
///
/// default: `/var/lib/moonfire-nvr/db`.
#[bpaf(fallback_with(crate::default_db_dir))]
db_dir: PathBuf, db_dir: PathBuf,
/// Opens the database in read-only mode and locks it only for shared access. /// Opens the database in read-only mode and locks it only for shared access.

View File

@ -12,11 +12,7 @@ use failure::Error;
#[derive(Bpaf, Debug)] #[derive(Bpaf, Debug)]
#[bpaf(options)] #[bpaf(options)]
pub struct Args { pub struct Args {
/// Directory holding the SQLite3 index database. #[bpaf(external(crate::parse_db_dir))]
///
///
/// default: `/var/lib/moonfire-nvr/db`.
#[bpaf(argument("PATH"), fallback_with(crate::default_db_dir))]
db_dir: std::path::PathBuf, db_dir: std::path::PathBuf,
/// When upgrading from schema version 1 to 2, the sample file directory. /// When upgrading from schema version 1 to 2, the sample file directory.

View File

@ -4,7 +4,7 @@
#![cfg_attr(all(feature = "nightly", test), feature(test))] #![cfg_attr(all(feature = "nightly", test), feature(test))]
use bpaf::Bpaf; use bpaf::{Bpaf, Parser};
use log::{debug, error}; use log::{debug, error};
use std::fmt::Write; use std::fmt::Write;
use std::str::FromStr; use std::str::FromStr;
@ -43,6 +43,8 @@ fn subcommand<T: 'static>(
parser.usage(Box::leak(usage.into_boxed_str())).command(cmd) parser.usage(Box::leak(usage.into_boxed_str())).command(cmd)
} }
const DEFAULT_DB_DIR: &str = "/var/lib/moonfire-nvr/db";
/// Moonfire NVR: security camera network video recorder. /// Moonfire NVR: security camera network video recorder.
#[derive(Bpaf, Debug)] #[derive(Bpaf, Debug)]
#[bpaf(options, version)] #[bpaf(options, version)]
@ -73,9 +75,14 @@ impl Args {
} }
} }
/// Returns the default database dir, for use in argument parsing with `bpaf(fallback_with(...))`. fn parse_db_dir() -> impl Parser<std::path::PathBuf> {
fn default_db_dir() -> Result<std::path::PathBuf, std::convert::Infallible> { bpaf::long("db-dir")
Ok("/var/lib/moonfire-nvr/db".into()) .help(format!(
"Directory holding the SQLite3 index database.\nDefault: `{}`",
DEFAULT_DB_DIR
))
.argument::<std::path::PathBuf>("PATH")
.fallback_with(|| Ok::<_, std::convert::Infallible>(DEFAULT_DB_DIR.into()))
} }
/// Custom panic hook that logs instead of directly writing to stderr. /// Custom panic hook that logs instead of directly writing to stderr.