Scott Lamb 0ffda11d4b
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
2023-02-15 08:32:48 -08:00

65 lines
1.9 KiB
Rust

// This file is part of Moonfire NVR, a security camera network video recorder.
// Copyright (C) 2017 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
//! Text-based configuration interface.
//!
//! This code is a bit messy, but it's essentially a prototype. Eventually Moonfire NVR's
//! configuration will likely be almost entirely done through a web-based UI.
use base::clock;
use bpaf::Bpaf;
use cursive::views;
use cursive::Cursive;
use failure::Error;
use std::path::PathBuf;
use std::sync::Arc;
mod cameras;
mod dirs;
mod users;
/// Interactively edits configuration.
#[derive(Bpaf, Debug)]
#[bpaf(options)]
pub struct Args {
#[bpaf(external(crate::parse_db_dir))]
db_dir: PathBuf,
}
pub fn subcommand() -> impl bpaf::Parser<Args> {
crate::subcommand(args(), "config")
}
pub fn run(args: Args) -> Result<i32, Error> {
let (_db_dir, conn) = super::open_conn(&args.db_dir, super::OpenMode::ReadWrite)?;
let clocks = clock::RealClocks {};
let db = Arc::new(db::Database::new(clocks, conn, true)?);
// This runtime is needed by the "Test" button in the camera config.
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_io()
.enable_time()
.build()?;
let _enter = rt.enter();
let mut siv = cursive::default();
//siv.add_global_callback('q', |s| s.quit());
siv.add_layer(
views::Dialog::around(
views::SelectView::<fn(&Arc<db::Database>, &mut Cursive)>::new()
.on_submit(move |siv, item| item(&db, siv))
.item("Cameras and streams".to_string(), cameras::top_dialog)
.item("Directories and retention".to_string(), dirs::top_dialog)
.item("Users".to_string(), users::top_dialog),
)
.button("Quit", |siv| siv.quit())
.title("Main menu"),
);
siv.run();
Ok(0)
}