Add a flag for specifying alternative configuration (fix #368) (#391)

This commit is contained in:
Roey Darwish Dror
2020-05-08 06:30:20 +03:00
committed by GitHub
parent c858a18bdd
commit 0311f955b0

View File

@@ -118,8 +118,8 @@ impl ConfigFile {
/// Read the configuration file. /// Read the configuration file.
/// ///
/// If the configuration file does not exist the function returns the default ConfigFile. /// If the configuration file does not exist the function returns the default ConfigFile.
fn read(base_dirs: &BaseDirs) -> Result<ConfigFile> { fn read(base_dirs: &BaseDirs, config_path: Option<PathBuf>) -> Result<ConfigFile> {
let config_path = Self::ensure(base_dirs)?; let config_path = config_path.unwrap_or_else(|| Self::ensure(base_dirs).unwrap());
let contents = fs::read_to_string(&config_path).map_err(|e| { let contents = fs::read_to_string(&config_path).map_err(|e| {
log::error!("Unable to read {}", config_path.display()); log::error!("Unable to read {}", config_path.display());
@@ -202,6 +202,10 @@ pub struct CommandLineArgs {
/// Don't pull the predefined git repos /// Don't pull the predefined git repos
#[structopt(long = "disable-predefined-git-repos")] #[structopt(long = "disable-predefined-git-repos")]
disable_predefined_git_repos: bool, disable_predefined_git_repos: bool,
/// Alternative configuration file
#[structopt(long = "config")]
config: Option<PathBuf>,
} }
impl CommandLineArgs { impl CommandLineArgs {
@@ -234,7 +238,7 @@ impl Config {
builder.init(); builder.init();
let config_file = ConfigFile::read(base_dirs).unwrap_or_else(|e| { let config_file = ConfigFile::read(base_dirs, opt.config.clone()).unwrap_or_else(|e| {
// Inform the user about errors when loading the configuration, // Inform the user about errors when loading the configuration,
// but fallback to the default config to at least attempt to do something // but fallback to the default config to at least attempt to do something
log::error!("failed to load configuration: {}", e); log::error!("failed to load configuration: {}", e);