@@ -4,6 +4,7 @@ use itertools::Itertools;
44use simplelog:: { debug, trace} ;
55use std:: fs:: File ;
66use std:: io:: { BufReader , Read , Write } ;
7+ use std:: iter:: repeat_with;
78use std:: path:: { Path , PathBuf } ;
89use tempfile:: tempdir_in;
910use zip;
@@ -61,73 +62,76 @@ pub async fn extract_asset(
6162 Ok ( vec ! [ archive_name. clone( ) ] )
6263 }
6364
65+ // handle all container events together to manage container, then match again
6466 ProvideConfig :: FromContainer {
6567 container : container_name,
66- files,
67- } => {
68- let tag = chal. container_tag_for_pod ( profile_name, container_name) ?;
69-
70- let name = format ! (
71- "asset-container-{}-{}" ,
72- chal. directory. to_string_lossy( ) . replace( "/" , "-" ) ,
73- container_name
74- ) ;
75- let container = docker:: create_container ( & tag, & name) . await ?;
76-
77- let files = extract_files ( chal, & container, files) . await ;
78-
79- docker:: remove_container ( container) . await ?;
80-
81- files
68+ ..
8269 }
83- . with_context ( || format ! ( "could not copy files {files:?} from container {container_name}" ) ) ,
84-
85- ProvideConfig :: FromContainerRename {
70+ | ProvideConfig :: FromContainerRename {
8671 container : container_name,
87- from,
88- to,
89- } => {
90- let tag = chal. container_tag_for_pod ( profile_name, container_name) ?;
91-
92- let name = format ! (
93- "asset-container-{}-{}" ,
94- chal. directory. to_string_lossy( ) . replace( "/" , "-" ) ,
95- container_name
96- ) ;
97- let container = docker:: create_container ( & tag, & name) . await ?;
98-
99- let files = extract_rename ( chal, & container, from, & chal. directory . join ( to) ) . await ;
100-
101- docker:: remove_container ( container) . await ?;
102-
103- files
72+ ..
10473 }
105- . with_context ( || format ! ( "could not copy file {from:?} from container {container_name}" ) ) ,
106-
107- ProvideConfig :: FromContainerArchive {
74+ | ProvideConfig :: FromContainerArchive {
10875 container : container_name,
109- files,
110- archive_name,
76+ ..
11177 } => {
11278 let tag = chal. container_tag_for_pod ( profile_name, container_name) ?;
11379
11480 let name = format ! (
115- "asset-container-{}-{}" ,
81+ "asset-container-{}-{}-{} " ,
11682 chal. directory. to_string_lossy( ) . replace( "/" , "-" ) ,
117- container_name
83+ container_name,
84+ // include random discriminator to avoid name collisions
85+ repeat_with( fastrand:: alphanumeric)
86+ . take( 6 )
87+ . collect:: <String >( )
11888 ) ;
89+
11990 let container = docker:: create_container ( & tag, & name) . await ?;
12091
121- let files =
122- extract_archive ( chal, & container, files, & chal. directory . join ( archive_name) ) . await ;
92+ // match on `provide` enum again to handle each container type
93+ let files = match provide {
94+ ProvideConfig :: FromContainer {
95+ container : container_name,
96+ files,
97+ } => extract_files ( chal, & container, files)
98+ . await
99+ . with_context ( || {
100+ format ! ( "could not copy files {files:?} from container {container_name}" )
101+ } ) ,
102+
103+ ProvideConfig :: FromContainerRename {
104+ container : container_name,
105+ from,
106+ to,
107+ } => extract_rename ( chal, & container, from, & chal. directory . join ( to) )
108+ . await
109+ . with_context ( || {
110+ format ! ( "could not copy file {from:?} from container {container_name}" )
111+ } ) ,
112+
113+ ProvideConfig :: FromContainerArchive {
114+ container : container_name,
115+ files,
116+ archive_name,
117+ } => extract_archive ( chal, & container, files, & chal. directory . join ( archive_name) )
118+ . await
119+ . with_context ( || {
120+ // rustfmt chokes silently if these format args are inlined... ???
121+ format ! (
122+ "could not create archive {:?} with files {:?} from container {}" ,
123+ archive_name, files, container_name
124+ )
125+ } ) ,
126+
127+ // non-container variants handled by outer match
128+ _ => unreachable ! ( ) ,
129+ } ;
123130
124131 docker:: remove_container ( container) . await ?;
125132
126133 files
127134 }
128- . with_context ( || {
129- format ! ( "could not create archive {archive_name:?} from container {container_name}" )
130- } ) ,
131135 }
132136}
133137
0 commit comments