From 39db4b8d1c4de1f7822d3d691ee3445470c17fbd Mon Sep 17 00:00:00 2001 From: kjuulh Date: Tue, 1 Aug 2023 15:36:24 +0200 Subject: [PATCH] refactor: move config building out of main execution loop Signed-off-by: kjuulh --- crates/cuddle-please-commands/src/command.rs | 37 +++++++++++--------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/crates/cuddle-please-commands/src/command.rs b/crates/cuddle-please-commands/src/command.rs index a42cac3..8f4231a 100644 --- a/crates/cuddle-please-commands/src/command.rs +++ b/crates/cuddle-please-commands/src/command.rs @@ -83,22 +83,7 @@ impl Command { } pub fn execute(self, current_dir: Option<&Path>) -> anyhow::Result<()> { - // 0. Get config - let mut builder = &mut PleaseConfigBuilder::new(); - - if self.global.config_stdin { - if let Some(stdin_fn) = self.stdin.clone() { - let output = (stdin_fn.lock().unwrap().deref())(); - builder = builder.with_stdin(output?); - } - } - // 1. Parse the current directory - let current_dir = get_current_path(current_dir, self.config.source.clone())?; - let config = builder - .with_config_file(¤t_dir) - .with_execution_env(std::env::vars()) - .with_cli(self.config.clone()) - .build()?; + let config = self.build_config(current_dir)?; match &self.commands { Some(Commands::Release {}) => { @@ -317,6 +302,26 @@ impl Command { Ok(()) } + fn build_config( + &self, + current_dir: Option<&Path>, + ) -> Result { + let mut builder = &mut PleaseConfigBuilder::new(); + if self.global.config_stdin { + if let Some(stdin_fn) = self.stdin.clone() { + let output = (stdin_fn.lock().unwrap().deref())(); + builder = builder.with_stdin(output?); + } + } + let current_dir = get_current_path(current_dir, self.config.source.clone())?; + let config = builder + .with_config_file(¤t_dir) + .with_execution_env(std::env::vars()) + .with_cli(self.config.clone()) + .build()?; + Ok(config) + } + fn get_git(&self, current_dir: &Path) -> anyhow::Result { VcsClient::new_git(current_dir) }