diff --git a/Cargo.toml b/Cargo.toml index a85b9af..730f630 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] clap = { version = "4.5.1", features = ["derive"] } +[target.'cfg(unix)'.dependencies] users = "0.11.0" [dev-dependencies] diff --git a/src/main.rs b/src/main.rs index 5f5a6fd..e018574 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,35 +10,30 @@ use clap::Parser; use crate::docker_client::DockerClient; +#[cfg_attr(unix, 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 +}