Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Make configuration file optional #523

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions rumqttd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use rumqttd::{Broker, Config};
use std::io::Write;

use config::FileFormat;
use rumqttd::Broker;

use structopt::StructOpt;

pub static RUMQTTD_DEFAULT_CONFIG: &str = include_str!("../demo.toml");
h3nill marked this conversation as resolved.
Show resolved Hide resolved

#[derive(StructOpt)]
#[structopt(name = "rumqttd")]
#[structopt(about = "A high performance, lightweight and embeddable MQTT broker written in Rust.")]
Expand All @@ -21,15 +26,30 @@ struct CommandLine {
commit_date: String,
/// path to config file
#[structopt(short, long)]
config: String,
config: Option<String>,
#[structopt(subcommand)]
command: Option<Command>,
/// log level (v: info, vv: debug, vvv: trace)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
verbose: u8,
}

#[derive(StructOpt)]
enum Command {
/// Write default configuration file to stdout
GenerateConfig,
}

fn main() {
let commandline: CommandLine = CommandLine::from_args();

if let Some(Command::GenerateConfig) = commandline.command {
std::io::stdout()
.write_all(RUMQTTD_DEFAULT_CONFIG.as_bytes())
.unwrap();
std::process::exit(0);
}

banner(&commandline);
let level = match commandline.verbose {
0 => "rumqttd=warn",
Expand All @@ -54,17 +74,31 @@ fn main() {
.try_init()
.expect("initialized subscriber succesfully");

let config = config::Config::builder()
.add_source(config::File::with_name(&commandline.config))
.build()
.unwrap();
let mut configs: rumqttd::Config;
if let Some(config) = &commandline.config {
configs = config::Config::builder()
.add_source(config::File::with_name(config))
.build()
.unwrap()
.try_deserialize()
.unwrap();
} else {
configs = config::Config::builder()
.add_source(config::File::from_str(
RUMQTTD_DEFAULT_CONFIG,
FileFormat::Toml,
))
.build()
.unwrap()
.try_deserialize()
.unwrap();
}

let mut config: Config = config.try_deserialize().unwrap();
config.console.set_filter_reload_handle(reload_handle);
configs.console.set_filter_reload_handle(reload_handle);

// println!("{:#?}", config);

let mut broker = Broker::new(config);
let mut broker = Broker::new(configs);
broker.start().unwrap();
}

Expand Down