Skip to content

Commit

Permalink
Compile OS specific code in a separate module based on the OS
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Mar 27, 2024
1 parent 8901a75 commit 75d67e5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>;
let group_id: Option<u32>;

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");
Expand Down
11 changes: 11 additions & 0 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub fn dockerfile_content() -> &'static [u8] {
include_bytes!("../Dockerfile")
}

pub fn get_user_id() -> Option<u32> {
Some(users::get_current_uid())
}

pub fn get_group_id() -> Option<u32> {
Some(users::get_current_gid())
}
11 changes: 11 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub fn dockerfile_content() -> &'static [u8] {
include_bytes!("../Dockerfile.windows")
}

pub fn get_user_id() -> Option<u32> {
None
}

pub fn get_group_id() -> Option<u32> {
None
}

0 comments on commit 75d67e5

Please sign in to comment.