@@ -6,7 +6,7 @@ use std::collections::HashMap;
66use std:: fs;
77use std:: io;
88use std:: path:: { Path , PathBuf } ;
9- use std:: process:: { Command , ExitCode , ExitStatus } ;
9+ use std:: process:: { Command , ExitCode , ExitStatus , Output } ;
1010
1111/// The result from trying to shell out to `micromamba`.
1212///
@@ -20,8 +20,11 @@ use std::process::{Command, ExitCode, ExitStatus};
2020pub enum MicromambaResult {
2121 /// We were run in no-op mode, so we didn't actually call out to it
2222 Noop ,
23- /// We were able to successfully call it and get a result
24- Ok ( ExitStatus ) ,
23+ /// We were able to successfully call it and get a result, though we streamed
24+ /// the output and did not save it.
25+ StreamedOutput ( ExitStatus ) ,
26+ /// We were able to successfully call it and get a result, capturing output.
27+ CapturedOutput ( Output ) ,
2528 /// We were unable to find or create a working `micromamba`
2629 NotFound ,
2730 /// We found a micromamba binary, but could not run it
@@ -30,11 +33,16 @@ pub enum MicromambaResult {
3033
3134impl MicromambaResult {
3235 pub fn exit_code ( & self ) -> ExitCode {
33- match self {
34- Self :: Ok ( exit_status ) => exit_status
36+ let to_code = | status : & ExitStatus | {
37+ status
3538 . code ( )
3639 . map ( |c| ExitCode :: from ( c as u8 ) )
37- . unwrap_or ( ExitCode :: FAILURE ) ,
40+ . unwrap_or ( ExitCode :: FAILURE )
41+ } ;
42+
43+ match self {
44+ Self :: StreamedOutput ( exit_status) => to_code ( exit_status) ,
45+ Self :: CapturedOutput ( output) => to_code ( & output. status ) ,
3846 Self :: Noop => ExitCode :: SUCCESS ,
3947 _ => ExitCode :: FAILURE ,
4048 }
@@ -102,7 +110,7 @@ fn block_on_child_exit(child: &mut std::process::Child) -> MicromambaResult {
102110 match child. wait ( ) {
103111 Ok ( exit_status) => {
104112 debug ! ( "micromamba exited with status: {}" , exit_status) ;
105- MicromambaResult :: Ok ( exit_status)
113+ MicromambaResult :: StreamedOutput ( exit_status)
106114 }
107115 Err ( e) => {
108116 error ! ( "We found a micromamba binary, but failed to wait for it to run" ) ;
@@ -112,17 +120,32 @@ fn block_on_child_exit(child: &mut std::process::Child) -> MicromambaResult {
112120 }
113121}
114122
115- fn exec_micromamba ( cmd : & mut Command ) -> MicromambaResult {
116- match cmd. spawn ( ) {
117- Ok ( mut child) => block_on_child_exit ( & mut child) ,
118- Err ( e) if e. kind ( ) == io:: ErrorKind :: NotFound => {
119- debug ! ( "Could not run micromamba at specified path: {}" , e) ;
120- MicromambaResult :: NotFound
123+ fn exec_micromamba ( cmd : & mut Command , stream_output : bool ) -> MicromambaResult {
124+ if stream_output {
125+ match cmd. spawn ( ) {
126+ Ok ( mut child) => block_on_child_exit ( & mut child) ,
127+ Err ( e) if e. kind ( ) == io:: ErrorKind :: NotFound => {
128+ debug ! ( "Could not run micromamba at specified path: {}" , e) ;
129+ MicromambaResult :: NotFound
130+ }
131+ Err ( e) => {
132+ error ! ( "We found a micromamba binary, but failed to run it" ) ;
133+ error ! ( "Error was: {}" , e) ;
134+ MicromambaResult :: CouldNotRun
135+ }
121136 }
122- Err ( e) => {
123- error ! ( "We found a micromamba binary, but failed to run it" ) ;
124- error ! ( "Error was: {}" , e) ;
125- MicromambaResult :: CouldNotRun
137+ } else {
138+ match cmd. output ( ) {
139+ Ok ( output) => MicromambaResult :: CapturedOutput ( output) ,
140+ Err ( e) if e. kind ( ) == io:: ErrorKind :: NotFound => {
141+ debug ! ( "Could not run micromamba at specified path: {}" , e) ;
142+ MicromambaResult :: NotFound
143+ }
144+ Err ( e) => {
145+ error ! ( "We found a micromamba binary, but failed to run it" ) ;
146+ error ! ( "Error was: {}" , e) ;
147+ MicromambaResult :: CouldNotRun
148+ }
126149 }
127150 }
128151}
@@ -144,7 +167,7 @@ fn exec_micromamba(cmd: &mut Command) -> MicromambaResult {
144167/// - We *could* embed the micromamba binary in our binary (Windows or Linux
145168/// based on compile target) and write it to the user cache directory rather
146169/// than downloading it. But this inflates our binary size.
147- pub fn micromamba ( config : & Config , args : Vec < & str > ) -> MicromambaResult {
170+ pub fn micromamba ( config : & Config , args : Vec < & str > , stream_output : bool ) -> MicromambaResult {
148171 let mut cmd = micromamba_at ( "micromamba" , config, & args) ;
149172
150173 if config. noop_mode {
@@ -154,8 +177,8 @@ pub fn micromamba(config: &Config, args: Vec<&str>) -> MicromambaResult {
154177
155178 // If we were able to get a result using micromamba found in $PATH, then
156179 // we're done.
157- match exec_micromamba ( & mut cmd) {
158- ok @ MicromambaResult :: Ok ( _ ) => {
180+ match exec_micromamba ( & mut cmd, stream_output ) {
181+ ok @ ( MicromambaResult :: StreamedOutput ( _ ) | MicromambaResult :: CapturedOutput ( _ ) ) => {
159182 debug ! ( "Ran micromamba found in $PATH" ) ;
160183 return ok;
161184 }
@@ -179,8 +202,8 @@ pub fn micromamba(config: &Config, args: Vec<&str>) -> MicromambaResult {
179202 }
180203 } ;
181204 let mut cmd = micromamba_at ( & downloaded_path. to_string_lossy ( ) , config, & args) ;
182- match exec_micromamba ( & mut cmd) {
183- ok @ MicromambaResult :: Ok ( _ ) => {
205+ match exec_micromamba ( & mut cmd, stream_output ) {
206+ ok @ ( MicromambaResult :: StreamedOutput ( _ ) | MicromambaResult :: CapturedOutput ( _ ) ) => {
184207 debug ! (
185208 "Ran downloaded/cached micromamba at {}" ,
186209 downloaded_path. display( )
0 commit comments