config files: Allow to read multiple config files

This commit is contained in:
Alexander Weiss 2023-04-25 19:55:55 +02:00
parent 4e33e6f055
commit 1554a281c4
4 changed files with 14 additions and 9 deletions

View File

@ -1,6 +1,7 @@
Changes in version x.x.x:
Breaking changes:
- config file: use-config now expects an array of config profiles to read.
Bugs fixed:
- The [[backup.sources]] section in the config file was ignored 0.5.2. This has been fixed.

View File

@ -7,7 +7,7 @@
# Global options: These options are used for all commands.
[global]
use-profile = ""
use-profile = []
log-level = "info" # any of "off", "error", "warn", "info", "debug", "trace"; default: "info"
log-file = "/path/to/rustic.log" # Default: not set
no-progress = false

View File

@ -51,9 +51,8 @@ impl Config {
let mut config: Self =
toml::from_str(&data).context("error reading TOML from config file")?;
// if "use_profile" is defined in config file, merge this referenced profile first
if !config.global.use_profile.is_empty() {
let profile = config.global.use_profile.clone();
config.merge_profile(&profile)?;
for profile in &config.global.use_profile.clone() {
config.merge_profile(profile)?;
}
self.merge(config);
} else {

View File

@ -54,16 +54,16 @@ struct Args {
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
pub struct GlobalOpts {
/// Config profile to use. This parses the file `<PROFILE>.toml` in the config directory.
/// [default: "rustic"]
#[clap(
short = 'P',
long,
global = true,
value_name = "PROFILE",
default_value = "rustic",
env = "RUSTIC_USE_PROFILE"
)]
#[merge(skip)]
use_profile: String,
#[merge(strategy = merge::vec::append)]
use_profile: Vec<String>,
/// Only show what would be done without modifying anything. Does not affect read-only commands
#[clap(long, short = 'n', global = true, env = "RUSTIC_DRY_RUN")]
@ -168,10 +168,15 @@ pub fn execute() -> Result<()> {
let command: Vec<_> = std::env::args_os().collect();
let args = Args::parse_from(&command);
let mut config = args.config;
if config.global.use_profile.is_empty() {
config.global.use_profile.push("rustic".to_string());
}
// get global options from command line / env and config file
let profile = config.global.use_profile.clone();
config.merge_profile(&profile)?;
for profile in &config.global.use_profile.clone() {
config.merge_profile(profile)?;
}
// start logger
let level_filter = config.global.log_level.unwrap_or(LevelFilter::Info);