From 08ce4d59baf65dd91aad64187e8c665472438a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 28 Mar 2024 21:09:00 +0000 Subject: [PATCH] Refactor docker_client to avoid duplication --- src/docker_client.rs | 78 +++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/docker_client.rs b/src/docker_client.rs index c7ebd11..589afac 100644 --- a/src/docker_client.rs +++ b/src/docker_client.rs @@ -11,58 +11,70 @@ impl DockerClient { ) -> Command { let mut command = Command::new("docker"); - command - .arg("build") - .arg("--build-arg") - .arg(format!("RUBY_VERSION={}", ruby_version)) - .arg("--build-arg") - .arg(format!("RAILS_VERSION={}", rails_version)); + command.arg("build"); - user_id.map(|id| command.args(["--build-arg", &format!("USER_ID={}", id)])); - group_id.map(|id| command.args(["--build-arg", &format!("GROUP_ID={}", id)])); + Self::set_build_arg(&mut command, "RUBY_VERSION", ruby_version); + Self::set_build_arg(&mut command, "RAILS_VERSION", rails_version); - command - .arg("-t") - .arg(format!("rails-new-{}-{}", ruby_version, rails_version)) - .arg("-") - .stdin(Stdio::piped()); + user_id.map(|id| Self::set_build_arg(&mut command, "USER_ID", &id.to_string())); + group_id.map(|id| Self::set_build_arg(&mut command, "GROUP_ID", &id.to_string())); + + command.arg("-t"); + + Self::set_image_name(&mut command, ruby_version, rails_version); + + command.arg("-").stdin(Stdio::piped()); command } pub fn run_image(ruby_version: &str, rails_version: &str, args: Vec) -> Command { - let binding = std::env::current_dir().unwrap(); - let current_dir = binding.to_str().unwrap(); + let mut command = Self::run(); - let mut command = Command::new("docker"); + Self::set_workdir(&mut command); + Self::set_image_name(&mut command, ruby_version, rails_version); + Self::set_rails_new(&mut command, args); command - .arg("run") - .arg("--rm") - .arg("-v") - .arg(format!("{}:{}", current_dir, current_dir)) - .arg("-w") - .arg(current_dir) - .arg(format!("rails-new-{}-{}", ruby_version, rails_version)) - .arg("rails") - .arg("new") - .args(args); + } + + pub fn get_help(ruby_version: &str, rails_version: &str) -> Command { + let mut command = Self::run(); + + Self::set_image_name(&mut command, ruby_version, rails_version); + Self::set_rails_new(&mut command, vec!["--help".to_string()]); command } - pub fn get_help(ruby_version: &str, rails_version: &str) -> Command { + fn run() -> Command { let mut command = Command::new("docker"); + command.args(["run", "--rm"]); + command - .arg("run") - .arg("--rm") - .arg(format!("rails-new-{}-{}", ruby_version, rails_version)) - .arg("rails") - .arg("new") - .arg("--help"); + } + + fn set_build_arg(command: &mut Command, key: &str, value: &str) { + command.args(["--build-arg", &format!("{}={}", key, value)]); + } + + fn set_workdir(command: &mut Command) { + let binding = std::env::current_dir().unwrap(); + let current_dir = binding.to_str().unwrap(); command + .arg("-v") + .arg(format!("{}:{}", current_dir, current_dir)) + .args(["-w", current_dir]); + } + + fn set_image_name(command: &mut Command, ruby_version: &str, rails_version: &str) { + command.arg(format!("rails-new-{}-{}", ruby_version, rails_version)); + } + + fn set_rails_new(command: &mut Command, args: Vec) { + command.args(["rails", "new"]).args(args); } }