Skip to content

Commit 41c718d

Browse files
authored
Support multiple config files in command args (#2)
1 parent c28586e commit 41c718d

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/config.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ pub struct Configuration {
3333
}
3434

3535
impl Configuration {
36-
pub fn get(filename: &str) -> Result<Configuration, String> {
37-
let toml_content = match fs::read_to_string(filename) {
38-
Ok(v) => v,
39-
Err(err) => return Err(format!("read config file error: {}", err).to_string()),
40-
};
36+
pub fn get(filenames: Vec<String>) -> Result<Configuration, String> {
37+
let mut content: String = String::new();
38+
39+
for file_name in &filenames {
40+
content.push_str(&match fs::read_to_string(file_name) {
41+
Ok(v) => v,
42+
Err(err) => return Err(format!("read config file error: {}", err).to_string()),
43+
});
44+
}
4145

42-
let config: Configuration = match toml::from_str(&toml_content) {
46+
let config: Configuration = match toml::from_str(&content) {
4347
Ok(v) => v,
4448
Err(err) => return Err(format!("parse config file error: {}", err)),
4549
};

src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ fn main() {
2929
.short("c")
3030
.long("config")
3131
.value_name("FILE")
32+
.multiple(true)
33+
.number_of_values(1)
3234
.help("Path to configuration file")
3335
.takes_value(true),
3436
)
3537
.get_matches();
3638

37-
let config_file = matches.value_of_lossy("config").unwrap();
38-
let config = config::Configuration::get(&config_file).expect("read configuration error");
39+
let config_files = matches.values_of_lossy("config").unwrap_or(vec![]);
40+
let config = config::Configuration::get(config_files).expect("read configuration error");
3941
let log_level =
4042
log::Level::from_str(&config.udp_bridge.log_level).expect("parse log_level error");
4143

0 commit comments

Comments
 (0)