From d380e2fc3e9845f3dcf2a12cebc36e5762229279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 27 Mar 2024 22:38:18 +0000 Subject: [PATCH] Compile OS specific code in a separate module based on the OS --- src/main.rs | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5f5a6fd..584d14f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,35 +10,30 @@ use clap::Parser; use crate::docker_client::DockerClient; +#[cfg_attr(not(windows), path = "unix.rs")] +#[cfg_attr(windows, path = "windows.rs")] +mod os_specific; + fn main() { let cli = Cli::parse(); - let dockerfile: &[u8]; - let user_id: Option; - let group_id: Option; - - if cfg!(windows) { - user_id = None; - group_id = None; - dockerfile = include_bytes!("../Dockerfile.windows"); - } else { - user_id = Some(users::get_current_uid()); - group_id = Some(users::get_current_gid()); - dockerfile = include_bytes!("../Dockerfile"); - } - let ruby_version = cli.ruby_version; let rails_version = cli.rails_version; // Run docker build --build-arg RUBY_VERSION=$RUBY_VERSION --build-arg RAILS_VERSION=$RAILS_VERSION -t rails-new-$RUBY_VERSION-$RAILS_VERSION // passing the content of DOCKERFILE to the command stdin - let mut child = DockerClient::build_image(&ruby_version, &rails_version, user_id, group_id) - .spawn() - .expect("Failed to execute process"); + let mut child = DockerClient::build_image( + &ruby_version, + &rails_version, + os_specific::get_user_id(), + os_specific::get_group_id(), + ) + .spawn() + .expect("Failed to execute process"); let mut stdin = child.stdin.take().expect("Failed to open stdin"); std::thread::spawn(move || { - stdin.write_all(dockerfile).unwrap(); + stdin.write_all(os_specific::dockerfile_content()).unwrap(); }); let status = child.wait().expect("failed to wait on child");