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]]
name = "bpaf"
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 = [
"bpaf_derive",
"owo-colors",
@ -156,7 +157,8 @@ dependencies = [
[[package]]
name = "bpaf_derive"
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 = [
"proc-macro2",
"quote",

View File

@ -24,7 +24,7 @@ members = ["base", "db"]
base = { package = "moonfire-base", path = "base" }
base64 = "0.13.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"
byteorder = "1.0"
cursive = "0.20.0"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,10 +20,7 @@ use std::process::Command;
#[derive(Bpaf, Debug, PartialEq, Eq)]
#[bpaf(options)]
pub struct Args {
/// Directory holding the SQLite3 index database.
///
/// default: `/var/lib/moonfire-nvr/db`.
#[bpaf(fallback_with(crate::default_db_dir))]
#[bpaf(external(crate::parse_db_dir))]
db_dir: PathBuf,
/// 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)]
#[bpaf(options)]
pub struct Args {
/// Directory holding the SQLite3 index database.
///
///
/// default: `/var/lib/moonfire-nvr/db`.
#[bpaf(argument("PATH"), fallback_with(crate::default_db_dir))]
#[bpaf(external(crate::parse_db_dir))]
db_dir: std::path::PathBuf,
/// 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))]
use bpaf::Bpaf;
use bpaf::{Bpaf, Parser};
use log::{debug, error};
use std::fmt::Write;
use std::str::FromStr;
@ -43,6 +43,8 @@ fn subcommand<T: 'static>(
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.
#[derive(Bpaf, Debug)]
#[bpaf(options, version)]
@ -73,9 +75,14 @@ impl Args {
}
}
/// Returns the default database dir, for use in argument parsing with `bpaf(fallback_with(...))`.
fn default_db_dir() -> Result<std::path::PathBuf, std::convert::Infallible> {
Ok("/var/lib/moonfire-nvr/db".into())
fn parse_db_dir() -> impl Parser<std::path::PathBuf> {
bpaf::long("db-dir")
.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.