Skip to content

Commit

Permalink
🔧 merge config from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
chriamue committed Sep 29, 2023
1 parent 2799d0a commit 44ea2f3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
22 changes: 21 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[derive(StructOpt, Debug, Default)]
#[structopt(name = "ornithology-pi", about = "Ornithology Pi Application.")]
pub struct Cli {
/// Activate server mode
Expand All @@ -18,6 +18,26 @@ pub struct Cli {
/// Activate detect mode
#[structopt(short, long)]
pub detect: Option<bool>,

/// Set the camera width
#[structopt(long)]
pub width: Option<u32>,

/// Set the camera height
#[structopt(long)]
pub height: Option<u32>,

/// Set the camera fps
#[structopt(long)]
pub fps: Option<u32>,

/// Set the server port
#[structopt(long)]
pub port: Option<u16>,

/// Set the server address
#[structopt(long)]
pub address: Option<String>,
}

impl Cli {
Expand Down
62 changes: 58 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use figment::{
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
use crate::cli::Cli;

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Shutdown {
pub ctrlc: bool,
pub signals: Vec<String>,
Expand All @@ -19,7 +21,7 @@ impl Default for Shutdown {
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Camera {
pub width: u32,
pub height: u32,
Expand All @@ -36,7 +38,7 @@ impl Default for Camera {
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Server {
pub port: u16,
pub address: String,
Expand All @@ -51,7 +53,7 @@ impl Default for Server {
}
}

#[derive(Default, Debug, Deserialize, Serialize)]
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct Config {
pub shutdown: Shutdown,
pub camera: Camera,
Expand All @@ -65,3 +67,55 @@ pub fn load_config() -> Config {
.extract()
.unwrap_or_default()
}

pub fn merge_cli_config(config: &Config, cli: &Cli) -> Config {
let mut config: Config = config.clone();

if let Some(width) = cli.width {
config.camera.width = width;
}

if let Some(height) = cli.height {
config.camera.height = height;
}

if let Some(fps) = cli.fps {
config.camera.fps = fps;
}

if let Some(port) = cli.port {
config.server.port = port;
}

if let Some(address) = &cli.address {
config.server.address = address.clone();
}

config
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_merge_cli_config() {
let config = Config::default();
let cli = Cli {
width: Some(1920),
height: Some(1080),
fps: Some(15),
port: Some(8080),
address: Some("localhost".into()),
..Default::default()
};

let merged = merge_cli_config(&config, &cli);

assert_eq!(merged.camera.width, cli.width.unwrap());
assert_eq!(merged.camera.height, cli.height.unwrap());
assert_eq!(merged.camera.fps, cli.fps.unwrap());
assert_eq!(merged.server.port, cli.port.unwrap());
assert_eq!(merged.server.address, cli.address.unwrap());
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ async fn main() {
cli.evaluate();

let config = config::load_config();
let config = config::merge_cli_config(&config, &cli);

let sightings: Arc<Mutex<Vec<Sighting>>> = Arc::new(Mutex::new(
ornithology_pi::sighting::load_from_file("sightings/sightings.db").unwrap_or_default(),
));
Expand Down

0 comments on commit 44ea2f3

Please sign in to comment.