Skip to content

Commit 08ce4d5

Browse files
committed
Refactor docker_client to avoid duplication
1 parent 06a20cc commit 08ce4d5

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

src/docker_client.rs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,70 @@ impl DockerClient {
1111
) -> Command {
1212
let mut command = Command::new("docker");
1313

14-
command
15-
.arg("build")
16-
.arg("--build-arg")
17-
.arg(format!("RUBY_VERSION={}", ruby_version))
18-
.arg("--build-arg")
19-
.arg(format!("RAILS_VERSION={}", rails_version));
14+
command.arg("build");
2015

21-
user_id.map(|id| command.args(["--build-arg", &format!("USER_ID={}", id)]));
22-
group_id.map(|id| command.args(["--build-arg", &format!("GROUP_ID={}", id)]));
16+
Self::set_build_arg(&mut command, "RUBY_VERSION", ruby_version);
17+
Self::set_build_arg(&mut command, "RAILS_VERSION", rails_version);
2318

24-
command
25-
.arg("-t")
26-
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
27-
.arg("-")
28-
.stdin(Stdio::piped());
19+
user_id.map(|id| Self::set_build_arg(&mut command, "USER_ID", &id.to_string()));
20+
group_id.map(|id| Self::set_build_arg(&mut command, "GROUP_ID", &id.to_string()));
21+
22+
command.arg("-t");
23+
24+
Self::set_image_name(&mut command, ruby_version, rails_version);
25+
26+
command.arg("-").stdin(Stdio::piped());
2927

3028
command
3129
}
3230

3331
pub fn run_image(ruby_version: &str, rails_version: &str, args: Vec<String>) -> Command {
34-
let binding = std::env::current_dir().unwrap();
35-
let current_dir = binding.to_str().unwrap();
32+
let mut command = Self::run();
3633

37-
let mut command = Command::new("docker");
34+
Self::set_workdir(&mut command);
35+
Self::set_image_name(&mut command, ruby_version, rails_version);
36+
Self::set_rails_new(&mut command, args);
3837

3938
command
40-
.arg("run")
41-
.arg("--rm")
42-
.arg("-v")
43-
.arg(format!("{}:{}", current_dir, current_dir))
44-
.arg("-w")
45-
.arg(current_dir)
46-
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
47-
.arg("rails")
48-
.arg("new")
49-
.args(args);
39+
}
40+
41+
pub fn get_help(ruby_version: &str, rails_version: &str) -> Command {
42+
let mut command = Self::run();
43+
44+
Self::set_image_name(&mut command, ruby_version, rails_version);
45+
Self::set_rails_new(&mut command, vec!["--help".to_string()]);
5046

5147
command
5248
}
5349

54-
pub fn get_help(ruby_version: &str, rails_version: &str) -> Command {
50+
fn run() -> Command {
5551
let mut command = Command::new("docker");
5652

53+
command.args(["run", "--rm"]);
54+
5755
command
58-
.arg("run")
59-
.arg("--rm")
60-
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
61-
.arg("rails")
62-
.arg("new")
63-
.arg("--help");
56+
}
57+
58+
fn set_build_arg(command: &mut Command, key: &str, value: &str) {
59+
command.args(["--build-arg", &format!("{}={}", key, value)]);
60+
}
61+
62+
fn set_workdir(command: &mut Command) {
63+
let binding = std::env::current_dir().unwrap();
64+
let current_dir = binding.to_str().unwrap();
6465

6566
command
67+
.arg("-v")
68+
.arg(format!("{}:{}", current_dir, current_dir))
69+
.args(["-w", current_dir]);
70+
}
71+
72+
fn set_image_name(command: &mut Command, ruby_version: &str, rails_version: &str) {
73+
command.arg(format!("rails-new-{}-{}", ruby_version, rails_version));
74+
}
75+
76+
fn set_rails_new(command: &mut Command, args: Vec<String>) {
77+
command.args(["rails", "new"]).args(args);
6678
}
6779
}
6880

0 commit comments

Comments
 (0)