Skip to content

Commit eeda766

Browse files
committed
De-duplicate logic for determining mm env name
1 parent ed7f23d commit eeda766

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

src/env.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,20 @@ pub fn determine_env_name(explicit_name: Option<String>) -> Option<String> {
105105
}
106106
}
107107

108-
pub fn run(config: Config, subcommand: Subcommand) -> ExitCode {
108+
fn env_name(explicit_name: Option<String>) -> Result<String, ExitCode> {
109+
match determine_env_name(explicit_name) {
110+
Some(name) => Ok(name),
111+
None => {
112+
error!("No environment name could be determined. You can specify one with --name");
113+
Err(ExitCode::FAILURE)
114+
}
115+
}
116+
}
117+
118+
pub fn run(config: Config, subcommand: Subcommand) -> Result<(), ExitCode> {
109119
match subcommand {
110120
Subcommand::Create(args) => {
111-
let Some(env_name) = determine_env_name(args.name) else {
112-
error!("No environment name could be determined. You can specify one with --name");
113-
return ExitCode::FAILURE;
114-
};
121+
let env_name = env_name(args.name)?;
115122
info!(
116123
"Creating environment '{}' - this may take some time...",
117124
env_name
@@ -131,7 +138,7 @@ pub fn run(config: Config, subcommand: Subcommand) -> ExitCode {
131138
);
132139
let rc = result.exit_code();
133140
match result {
134-
MicromambaResult::CapturedOutput(output) if rc != ExitCode::SUCCESS => {
141+
MicromambaResult::CapturedOutput(ref output) if rc != ExitCode::SUCCESS => {
135142
error!("Got a non-zero exit code from micromamba, dumping output:");
136143
error!("micromamba stdout:");
137144
println!("{}", String::from_utf8_lossy(&output.stdout));
@@ -141,31 +148,23 @@ pub fn run(config: Config, subcommand: Subcommand) -> ExitCode {
141148
MicromambaResult::CapturedOutput(_) if rc == ExitCode::SUCCESS => info!("Done."),
142149
_ => {}
143150
}
144-
rc
151+
result.into()
145152
}
146-
Subcommand::List => micromamba(&config, vec!["env", "list"], true).exit_code(),
147-
Subcommand::Info => micromamba(&config, vec!["info"], true).exit_code(),
153+
Subcommand::List => micromamba(&config, vec!["env", "list"], true).into(),
154+
Subcommand::Info => micromamba(&config, vec!["info"], true).into(),
148155
Subcommand::Run(args) => {
149-
let Some(env_name) = determine_env_name(args.name) else {
150-
error!("No environment name could be determined. You can specify one with --name");
151-
return ExitCode::FAILURE;
152-
};
156+
let env_name = env_name(args.name)?;
153157
let mut micromamba_args = vec!["run", "--name", &env_name, &args.command];
154158
micromamba_args.extend(args.arguments.iter().map(|s| s.as_str()));
155-
micromamba(&config, micromamba_args, true).exit_code()
159+
micromamba(&config, micromamba_args, true).into()
156160
}
157161
Subcommand::Activate(args) => {
158162
let Some(shell) = SupportedShell::from_csm_hook() else {
159163
error!("Your shell does not appear to have the csm hook enabled");
160164
error!("See 'csm init' for information on how to set up the hook");
161-
return ExitCode::FAILURE;
162-
};
163-
164-
let Some(env_name) = determine_env_name(args.name) else {
165-
error!("No environment name could be determined. You can specify one with --name");
166-
return ExitCode::FAILURE;
165+
return Err(ExitCode::FAILURE);
167166
};
168-
167+
let env_name = env_name(args.name)?;
169168
info!("Activating environment '{}'...", env_name);
170169

171170
// NOTE: Anything to stdout here is *evaluated by the user's shell*
@@ -177,14 +176,14 @@ pub fn run(config: Config, subcommand: Subcommand) -> ExitCode {
177176
"Could not determine binary path for environment '{}'",
178177
env_name
179178
);
180-
return ExitCode::FAILURE;
179+
return Err(ExitCode::FAILURE);
181180
};
182181
println!("{}", shell.prepend_path(&bin_path));
183182

184183
// And a few conda-specific vars
185184
let Some(env_path) = micromamba::path_for_env(&config, &env_name) else {
186185
error!("Could not determine path for environment '{}'", env_name);
187-
return ExitCode::FAILURE;
186+
return Err(ExitCode::FAILURE);
188187
};
189188
println!("{}", shell.set_env_var("CONDA_DEFAULT_ENV", &env_name));
190189
println!(
@@ -193,24 +192,24 @@ pub fn run(config: Config, subcommand: Subcommand) -> ExitCode {
193192
);
194193
println!("{}", shell.set_env_var("CONDA_SHLVL", "1"));
195194

196-
ExitCode::SUCCESS
195+
Ok(())
197196
}
198197
Subcommand::Deactivate => {
199198
let Some(shell) = SupportedShell::from_csm_hook() else {
200199
error!("Your shell does not appear to have the csm hook enabled");
201200
error!("See 'csm init' for information on how to set up the hook");
202-
return ExitCode::FAILURE;
201+
return Err(ExitCode::FAILURE);
203202
};
204203
println!("{}", shell.restore_and_unset_env_var("PATH"));
205204
println!("{}", shell.restore_and_unset_env_var("CONDA_DEFAULT_ENV"));
206205
println!("{}", shell.restore_and_unset_env_var("CONDA_PREFIX"));
207206
println!("{}", shell.restore_and_unset_env_var("CONDA_SHLVL"));
208-
ExitCode::SUCCESS
207+
Ok(())
209208
}
210209
_ => {
211210
println!("{:?}", config);
212211
println!("{:?}", subcommand);
213-
ExitCode::SUCCESS
212+
Ok(())
214213
}
215214
}
216215
}

src/micromamba.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ impl MicromambaResult {
5050
}
5151
}
5252

53+
impl From<MicromambaResult> for Result<(), ExitCode> {
54+
fn from(result: MicromambaResult) -> Self {
55+
match result.exit_code() {
56+
ExitCode::SUCCESS => Ok(()),
57+
e => Err(e),
58+
}
59+
}
60+
}
61+
5362
enum DownloadError {
5463
IncompatibleOS,
5564
BinNotInArchive,

0 commit comments

Comments
 (0)