Skip to content

Commit

Permalink
feat(config): add cli parameter to specify config file path or load f…
Browse files Browse the repository at this point in the history
…rom platform config dir
  • Loading branch information
maxjoehnk committed Dec 11, 2020
1 parent 82eab88 commit 8e8577c
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 6 deletions.
116 changes: 115 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ serde_json = "1"
serde_yaml = "0.8"
anyhow = "1"
futures-util = "0.3"
structopt = "0.3"
directories = "3"

[package.metadata.deb]
section = "utility"
Expand Down
34 changes: 34 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::options::CliOptions;
use directories::ProjectDirs;
use serde::Deserialize;
use std::fs::File;
use std::path::{Path, PathBuf};

const DEFAULT_POLL_RATE: u64 = 5;

Expand Down Expand Up @@ -41,3 +45,33 @@ impl Default for BacklightProvider {
fn default_poll_rate() -> u64 {
DEFAULT_POLL_RATE
}

pub(crate) fn get_config(options: &CliOptions) -> anyhow::Result<Config> {
let path = get_config_file_path(options);
log::debug!("Loading config file from {:?}", &path);
let config_file = File::open(path)?;
let config: Config = serde_yaml::from_reader(&config_file)?;

Ok(config)
}

fn get_config_file_path(options: &CliOptions) -> PathBuf {
let default_file = Path::new("config.yml");
let user_dir_file = get_user_dir_path();

match (&options.config, default_file, user_dir_file) {
(Some(config), _, _) => config.clone(),
(None, config, _) if config.exists() => config.to_path_buf(),
(None, _, Some(config)) if config.exists() => config,
_ => panic!("No config file found"),
}
}

fn get_user_dir_path() -> Option<PathBuf> {
if let Some(project_dirs) = ProjectDirs::from("me", "maxjoehnk", "desktop2mqtt") {
let config_dir = project_dirs.config_dir();
Some(config_dir.join("config.yml"))
} else {
None
}
}
28 changes: 23 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
mod config;
mod modules;
mod options;

use crate::config::Config;
use crate::config::get_config;
use crate::modules::*;
use std::fs::File;
use crate::options::CliOptions;
use log::LevelFilter;
use structopt::StructOpt;
use tokio::sync::{broadcast, mpsc};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
let config_file = File::open("config.yml")?;
let config: Config = serde_yaml::from_reader(&config_file)?;
let options = CliOptions::from_args();
setup_logging(options.verbose);

let config = get_config(&options)?;

let (mqtt_sender, mqtt_receiver) = mpsc::unbounded_channel();
let (mqtt_event_sender, mqtt_event_receiver) = broadcast::channel(10);
Expand All @@ -23,6 +27,8 @@ async fn main() -> anyhow::Result<()> {
let mut backlight_module =
get_backlight_module(state_sender, mqtt_event_receiver, config.backlight);

log::info!("Starting desktop2mqtt...");

tokio::try_join!(
mqtt_module.run(&config),
hass_discovery_module.run(&config),
Expand All @@ -33,3 +39,15 @@ async fn main() -> anyhow::Result<()> {

Ok(())
}

fn setup_logging(verbose: u8) {
let log_level = match verbose {
0 => LevelFilter::Info,
1 => LevelFilter::Debug,
_ => LevelFilter::Trace,
};

env_logger::Builder::from_default_env()
.filter(None, log_level)
.init();
}
14 changes: 14 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::path::PathBuf;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
pub(crate) struct CliOptions {
/// Verbosity (-v = debug, -vv = trace)
#[structopt(short, long, parse(from_occurrences))]
pub(crate) verbose: u8,

/// Config file
#[structopt(short, long, parse(from_os_str))]
pub(crate) config: Option<PathBuf>,
}

0 comments on commit 8e8577c

Please sign in to comment.