From 75d67e5cad357b5d7ebecb4297b8251dba1dee84 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 +++++++++++++------------------ src/unix.rs | 11 +++++++++++ src/windows.rs | 11 +++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 src/unix.rs create mode 100644 src/windows.rs 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"); diff --git a/src/unix.rs b/src/unix.rs new file mode 100644 index 0000000..e24e232 --- /dev/null +++ b/src/unix.rs @@ -0,0 +1,11 @@ +pub fn dockerfile_content() -> &'static [u8] { + include_bytes!("../Dockerfile") +} + +pub fn get_user_id() -> Option { + Some(users::get_current_uid()) +} + +pub fn get_group_id() -> Option { + Some(users::get_current_gid()) +} diff --git a/src/windows.rs b/src/windows.rs new file mode 100644 index 0000000..a9a3c49 --- /dev/null +++ b/src/windows.rs @@ -0,0 +1,11 @@ +pub fn dockerfile_content() -> &'static [u8] { + include_bytes!("../Dockerfile.windows") +} + +pub fn get_user_id() -> Option { + None +} + +pub fn get_group_id() -> Option { + None +}