diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-08-19 09:35:39 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-08-19 09:35:39 +0300 |
commit | 7d806df78b19376dbd95305a27d8f1f7e837d5ca (patch) | |
tree | 368b4625f1b7103cea3aeb791f9beaf7892e8730 | |
parent | c82f316d140eb33cbe722c4526db6f4e11c01b84 (diff) | |
download | ambient-run-7d806df78b19376dbd95305a27d8f1f7e837d5ca.tar.gz |
refactor: load configuration files centrally, not in sub-commands
Sponsored-by: author
-rw-r--r-- | src/bin/ambient-run.rs | 22 | ||||
-rw-r--r-- | src/config.rs | 5 |
2 files changed, 18 insertions, 9 deletions
diff --git a/src/bin/ambient-run.rs b/src/bin/ambient-run.rs index 81dbe68..d81c96a 100644 --- a/src/bin/ambient-run.rs +++ b/src/bin/ambient-run.rs @@ -22,9 +22,10 @@ fn main() { fn fallible_main() -> Result<(), AmbientRunError> { let args = Args::parse(); + let config = args.load_config()?; match &args.cmd { - Command::Config(x) => x.run(&args), - Command::Project(x) => x.run(&args), + Command::Config(x) => x.run(&args, &config), + Command::Project(x) => x.run(&args, &config), } } @@ -38,6 +39,12 @@ struct Args { cmd: Command, } +impl Args { + fn load_config(&self) -> Result<Config, AmbientRunError> { + Ok(Config::load(default_config_file()?, &self.config)?) + } +} + #[derive(Debug, Parser)] enum Command { Config(ConfigCommand), @@ -54,18 +61,17 @@ struct ConfigCommand { } impl ConfigCommand { - fn run(&self, global: &Args) -> Result<(), AmbientRunError> { + fn run(&self, global: &Args, config: &Config) -> Result<(), AmbientRunError> { if self.default && self.filename { Self::show(default_config_file()?.as_path())?; } else if self.filename { - Self::show_if(default_config_file()?.as_path())?; + if let Ok(default) = default_config_file() { + Self::show_if(&default)?; + } for filename in &global.config { Self::show_if(filename)?; } } else { - let mut filenames = global.config.clone(); - filenames.insert(0, default_config_file()?); - let config = Config::load(&filenames)?; println!("{}", config.as_yaml()?); } Ok(()) @@ -91,7 +97,7 @@ struct ProjectCommand { } impl ProjectCommand { - fn run(&self, _global: &Args) -> Result<(), AmbientRunError> { + fn run(&self, _global: &Args, _config: &Config) -> Result<(), AmbientRunError> { let project = Project::load(&self.filename)?; println!("{}", project.as_yaml()?); Ok(()) diff --git a/src/config.rs b/src/config.rs index 610a44b..f87b347 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,8 +17,11 @@ pub struct Config { impl Config { /// Load full configuration from defaults and all files. - pub fn load(filenames: &[PathBuf]) -> Result<Self, ConfigError> { + pub fn load(default_file: PathBuf, filenames: &[PathBuf]) -> Result<Self, ConfigError> { let mut config = Self::default(); + if default_file.exists() { + config.add_from(&default_file)?; + } for filename in filenames { config.add_from(filename)?; } |