Skip to content

Commit 6b9db6b

Browse files
committed
Extract DockerClient abstraction
This will make easier for us to change the implmentation later.
1 parent 7e09c15 commit 6b9db6b

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

src/docker_client.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::process::{Command, Stdio};
2+
3+
pub struct DockerClient {}
4+
5+
impl DockerClient {
6+
pub fn build_image(ruby_version: &str, rails_version: &str) -> Command {
7+
let mut command = Command::new("docker");
8+
9+
command
10+
.arg("build")
11+
.arg("--build-arg")
12+
.arg(format!("RUBY_VERSION={}", ruby_version))
13+
.arg("--build-arg")
14+
.arg(format!("RAILS_VERSION={}", rails_version))
15+
.arg("-t")
16+
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
17+
.arg("-")
18+
.stdin(Stdio::piped());
19+
20+
command
21+
}
22+
23+
pub fn run_image(ruby_version: &str, rails_version: &str, args: Vec<String>) -> Command {
24+
let binding = std::env::current_dir().unwrap();
25+
let current_dir = binding.to_str().unwrap();
26+
27+
let mut command = Command::new("docker");
28+
29+
command
30+
.arg("run")
31+
.arg("-v")
32+
.arg(format!("{}:{}", current_dir, current_dir))
33+
.arg("-w")
34+
.arg(current_dir)
35+
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
36+
.arg("rails")
37+
.arg("new")
38+
.args(args);
39+
40+
command
41+
}
42+
}

src/main.rs

+5-24
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Write a CLI program that call the bash file rails-new inside the bin folder.
22

33
// use std::process::Command;
4+
mod docker_client;
45
mod rails_new;
56
use rails_new::Cli;
67
use std::io::Write;
7-
use std::process::{Command, Stdio};
88

99
use clap::Parser;
1010

11+
use crate::docker_client::DockerClient;
12+
1113
fn main() {
1214
let cli = Cli::parse();
1315

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

2022
// Run docker build --build-arg RUBY_VERSION=$RUBY_VERSION --build-arg RAILS_VERSION=$RAILS_VERSION -t rails-new-$RUBY_VERSION-$RAILS_VERSION
2123
// passing the content of DOCKERFILE to the command stdin
22-
let mut child = Command::new("docker")
23-
.arg("build")
24-
.arg("--build-arg")
25-
.arg(format!("RUBY_VERSION={}", ruby_version))
26-
.arg("--build-arg")
27-
.arg(format!("RAILS_VERSION={}", rails_version))
28-
.arg("-t")
29-
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
30-
.arg("-")
31-
.stdin(Stdio::piped())
24+
let mut child = DockerClient::build_image(&ruby_version, &rails_version)
3225
.spawn()
3326
.expect("Failed to execute process");
3427

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

4235
// Run the image with docker run -v $(pwd):/$(pwd) -w $(pwd) rails-new-$RUBY_VERSION-$RAILS_VERSION rails new $@
43-
let binding = std::env::current_dir().unwrap();
44-
let current_dir = binding.to_str().unwrap();
45-
46-
let status = Command::new("docker")
47-
.arg("run")
48-
.arg("-v")
49-
.arg(format!("{}:{}", current_dir, current_dir))
50-
.arg("-w")
51-
.arg(current_dir)
52-
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
53-
.arg("rails")
54-
.arg("new")
55-
.args(cli.args)
36+
let status = DockerClient::run_image(&ruby_version, &rails_version, cli.args)
5637
.status()
5738
.expect("Failed to execute process");
5839

0 commit comments

Comments
 (0)