@@ -5,16 +5,23 @@ pub struct DockerClient {}
5
5
impl DockerClient {
6
6
pub fn build_image (
7
7
ruby_version : & str ,
8
- rails_version : & str ,
8
+ maybe_rails_version : Option < & str > ,
9
9
user_id : Option < u32 > ,
10
10
group_id : Option < u32 > ,
11
+ rebuild : bool ,
11
12
) -> Command {
12
13
let mut command = Command :: new ( "docker" ) ;
13
14
14
15
command. arg ( "build" ) ;
15
16
17
+ if rebuild {
18
+ command. arg ( "--no-cache" ) ;
19
+ }
20
+
16
21
Self :: set_build_arg ( & mut command, "RUBY_VERSION" , ruby_version) ;
17
- Self :: set_build_arg ( & mut command, "RAILS_VERSION" , rails_version) ;
22
+ if let Some ( rails_version) = maybe_rails_version {
23
+ Self :: set_build_arg ( & mut command, "RAILS_VERSION" , rails_version) ;
24
+ }
18
25
19
26
if let Some ( id) = user_id {
20
27
Self :: set_build_arg ( & mut command, "USER_ID" , & id. to_string ( ) )
@@ -25,14 +32,18 @@ impl DockerClient {
25
32
26
33
command. arg ( "-t" ) ;
27
34
28
- Self :: set_image_name ( & mut command, ruby_version, rails_version ) ;
35
+ Self :: set_image_name ( & mut command, ruby_version, maybe_rails_version ) ;
29
36
30
37
command. arg ( "-" ) . stdin ( Stdio :: piped ( ) ) ;
31
38
32
39
command
33
40
}
34
41
35
- pub fn run_image ( ruby_version : & str , rails_version : & str , args : Vec < String > ) -> Command {
42
+ pub fn run_image (
43
+ ruby_version : & str ,
44
+ rails_version : Option < & str > ,
45
+ args : Vec < String > ,
46
+ ) -> Command {
36
47
let mut command = Self :: run ( ) ;
37
48
38
49
Self :: set_workdir ( & mut command) ;
@@ -42,7 +53,7 @@ impl DockerClient {
42
53
command
43
54
}
44
55
45
- pub fn get_help ( ruby_version : & str , rails_version : & str ) -> Command {
56
+ pub fn get_help ( ruby_version : & str , rails_version : Option < & str > ) -> Command {
46
57
let mut command = Self :: run ( ) ;
47
58
48
59
Self :: set_image_name ( & mut command, ruby_version, rails_version) ;
@@ -76,8 +87,16 @@ impl DockerClient {
76
87
. args ( [ "-w" , current_dir] ) ;
77
88
}
78
89
79
- fn set_image_name ( command : & mut Command , ruby_version : & str , rails_version : & str ) {
80
- command. arg ( format ! ( "rails-new-{}-{}" , ruby_version, rails_version) ) ;
90
+ fn set_image_name (
91
+ command : & mut Command ,
92
+ ruby_version : & str ,
93
+ maybe_rails_version : Option < & str > ,
94
+ ) {
95
+ if let Some ( rails_version) = maybe_rails_version {
96
+ command. arg ( format ! ( "rails-new-{}-{}" , ruby_version, rails_version) ) ;
97
+ } else {
98
+ command. arg ( format ! ( "rails-new-{}" , ruby_version) ) ;
99
+ }
81
100
}
82
101
83
102
fn set_rails_new ( command : & mut Command , args : Vec < String > ) {
@@ -116,7 +135,7 @@ mod tests {
116
135
117
136
#[ test]
118
137
fn build_image ( ) {
119
- let command = DockerClient :: build_image ( "3.2.3" , "7.1.3" , None , None ) ;
138
+ let command = DockerClient :: build_image ( "3.2.3" , Some ( "7.1.3" ) , None , None , false ) ;
120
139
121
140
assert_eq ! ( command. get_program( ) , "docker" ) ;
122
141
@@ -139,7 +158,7 @@ mod tests {
139
158
140
159
#[ test]
141
160
fn build_image_with_user_id ( ) {
142
- let command = DockerClient :: build_image ( "3.2.3" , "7.1.3" , Some ( 1000 ) , None ) ;
161
+ let command = DockerClient :: build_image ( "3.2.3" , Some ( "7.1.3" ) , Some ( 1000 ) , None , false ) ;
143
162
144
163
assert_eq ! ( command. get_program( ) , "docker" ) ;
145
164
@@ -164,7 +183,7 @@ mod tests {
164
183
165
184
#[ test]
166
185
fn build_image_with_group_id ( ) {
167
- let command = DockerClient :: build_image ( "3.2.3" , "7.1.3" , None , Some ( 1000 ) ) ;
186
+ let command = DockerClient :: build_image ( "3.2.3" , Some ( "7.1.3" ) , None , Some ( 1000 ) , false ) ;
168
187
169
188
assert_eq ! ( command. get_program( ) , "docker" ) ;
170
189
@@ -187,9 +206,75 @@ mod tests {
187
206
) ;
188
207
}
189
208
209
+ #[ test]
210
+ fn build_image_with_rebuild_flag ( ) {
211
+ let command = DockerClient :: build_image ( "3.2.3" , Some ( "7.1.3" ) , None , None , true ) ;
212
+
213
+ let args: Vec < & OsStr > = command. get_args ( ) . collect ( ) ;
214
+
215
+ assert_eq ! (
216
+ args,
217
+ & [
218
+ "build" ,
219
+ "--no-cache" ,
220
+ "--build-arg" ,
221
+ "RUBY_VERSION=3.2.3" ,
222
+ "--build-arg" ,
223
+ "RAILS_VERSION=7.1.3" ,
224
+ "-t" ,
225
+ "rails-new-3.2.3-7.1.3" ,
226
+ "-" ,
227
+ ]
228
+ ) ;
229
+ }
230
+
231
+ #[ test]
232
+ fn build_image_without_rails_version ( ) {
233
+ let command = DockerClient :: build_image ( "3.2.3" , None , None , None , false ) ;
234
+
235
+ let args: Vec < & OsStr > = command. get_args ( ) . collect ( ) ;
236
+
237
+ assert_eq ! (
238
+ args,
239
+ & [
240
+ "build" ,
241
+ "--build-arg" ,
242
+ "RUBY_VERSION=3.2.3" ,
243
+ "-t" ,
244
+ "rails-new-3.2.3" ,
245
+ "-" ,
246
+ ]
247
+ ) ;
248
+ }
249
+
250
+ #[ test]
251
+ fn build_image_with_both_ids ( ) {
252
+ let command = DockerClient :: build_image ( "3.2.3" , Some ( "7.1.3" ) , Some ( 1000 ) , Some ( 1000 ) , false ) ;
253
+
254
+ let args: Vec < & OsStr > = command. get_args ( ) . collect ( ) ;
255
+
256
+ assert_eq ! (
257
+ args,
258
+ & [
259
+ "build" ,
260
+ "--build-arg" ,
261
+ "RUBY_VERSION=3.2.3" ,
262
+ "--build-arg" ,
263
+ "RAILS_VERSION=7.1.3" ,
264
+ "--build-arg" ,
265
+ "USER_ID=1000" ,
266
+ "--build-arg" ,
267
+ "GROUP_ID=1000" ,
268
+ "-t" ,
269
+ "rails-new-3.2.3-7.1.3" ,
270
+ "-" ,
271
+ ]
272
+ ) ;
273
+ }
274
+
190
275
#[ test]
191
276
fn run_image ( ) {
192
- let command = DockerClient :: run_image ( "3.2.3" , "7.1.3" , vec ! [ "my_app" . to_string( ) ] ) ;
277
+ let command = DockerClient :: run_image ( "3.2.3" , Some ( "7.1.3" ) , vec ! [ "my_app" . to_string( ) ] ) ;
193
278
194
279
assert_eq ! ( command. get_program( ) , "docker" ) ;
195
280
@@ -216,9 +301,36 @@ mod tests {
216
301
) ;
217
302
}
218
303
304
+ #[ test]
305
+ fn run_image_without_rails_version ( ) {
306
+ let command = DockerClient :: run_image ( "3.2.3" , None , vec ! [ "my_app" . to_string( ) ] ) ;
307
+
308
+ let binding = current_dir ( ) . unwrap ( ) ;
309
+ let absolute_path = canonicalize_os_path ( & binding) . unwrap ( ) ;
310
+ let current_dir = absolute_path. to_str ( ) . unwrap ( ) ;
311
+
312
+ let args: Vec < & OsStr > = command. get_args ( ) . collect ( ) ;
313
+
314
+ assert_eq ! (
315
+ args,
316
+ & [
317
+ "run" ,
318
+ "--rm" ,
319
+ "-v" ,
320
+ & format!( "{}:{}" , current_dir, current_dir) ,
321
+ "-w" ,
322
+ current_dir,
323
+ "rails-new-3.2.3" ,
324
+ "rails" ,
325
+ "new" ,
326
+ "my_app" ,
327
+ ]
328
+ ) ;
329
+ }
330
+
219
331
#[ test]
220
332
fn get_help ( ) {
221
- let command = DockerClient :: get_help ( "3.2.3" , "7.1.3" ) ;
333
+ let command = DockerClient :: get_help ( "3.2.3" , Some ( "7.1.3" ) ) ;
222
334
223
335
assert_eq ! ( command. get_program( ) , "docker" ) ;
224
336
0 commit comments