Skip to content

Commit

Permalink
Extract DockerClient abstraction
Browse files Browse the repository at this point in the history
This will make easier for us to change the implmentation later.
  • Loading branch information
rafaelfranca committed Mar 6, 2024
1 parent 7e09c15 commit 6b9db6b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
42 changes: 42 additions & 0 deletions src/docker_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::process::{Command, Stdio};

pub struct DockerClient {}

impl DockerClient {
pub fn build_image(ruby_version: &str, rails_version: &str) -> 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))
.arg("-t")
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
.arg("-")
.stdin(Stdio::piped());

command
}

pub fn run_image(ruby_version: &str, rails_version: &str, args: Vec<String>) -> Command {
let binding = std::env::current_dir().unwrap();
let current_dir = binding.to_str().unwrap();

let mut command = Command::new("docker");

command
.arg("run")
.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);

command
}
}
29 changes: 5 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Write a CLI program that call the bash file rails-new inside the bin folder.

// use std::process::Command;
mod docker_client;
mod rails_new;
use rails_new::Cli;
use std::io::Write;
use std::process::{Command, Stdio};

use clap::Parser;

use crate::docker_client::DockerClient;

fn main() {
let cli = Cli::parse();

Expand All @@ -19,16 +21,7 @@ fn main() {

// 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 = Command::new("docker")
.arg("build")
.arg("--build-arg")
.arg(format!("RUBY_VERSION={}", ruby_version))
.arg("--build-arg")
.arg(format!("RAILS_VERSION={}", rails_version))
.arg("-t")
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
.arg("-")
.stdin(Stdio::piped())
let mut child = DockerClient::build_image(&ruby_version, &rails_version)
.spawn()
.expect("Failed to execute process");

Expand All @@ -40,19 +33,7 @@ fn main() {
assert!(status.success());

// Run the image with docker run -v $(pwd):/$(pwd) -w $(pwd) rails-new-$RUBY_VERSION-$RAILS_VERSION rails new $@
let binding = std::env::current_dir().unwrap();
let current_dir = binding.to_str().unwrap();

let status = Command::new("docker")
.arg("run")
.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(cli.args)
let status = DockerClient::run_image(&ruby_version, &rails_version, cli.args)
.status()
.expect("Failed to execute process");

Expand Down

0 comments on commit 6b9db6b

Please sign in to comment.