11use crate :: csmrc:: Config ;
2- use crate :: micromamba:: { MicromambaResult , micromamba} ;
2+ use crate :: micromamba:: { self , MicromambaResult , micromamba} ;
33use crate :: shell:: SupportedShell ;
44
55use log:: { debug, error, info} ;
@@ -11,11 +11,11 @@ use std::process::ExitCode;
1111#[ derive( Debug , clap:: Subcommand ) ]
1212pub enum Subcommand {
1313 /// Create an environment
14- Create ( CreateArgs ) ,
14+ Create ( CommonEnvArgs ) ,
1515 /// Activate an environment
16- Activate ,
16+ Activate ( CommonEnvArgs ) ,
1717 /// Deactivate an environment
18- Deactivate ,
18+ Deactivate ( CommonEnvArgs ) ,
1919 /// Run an executable in an environment
2020 Run ( RunArgs ) ,
2121 /// ???
@@ -29,7 +29,7 @@ pub enum Subcommand {
2929}
3030
3131#[ derive( Debug , clap:: Args ) ]
32- pub struct CreateArgs {
32+ pub struct CommonEnvArgs {
3333 /// If specified, the name of the environment. If not specified, csm will
3434 /// look to robotmk-env.yaml for a "name" field to use instead. As a last
3535 /// resort, the current directory name will be used
@@ -154,21 +154,39 @@ pub fn run(config: Config, subcommand: Subcommand) -> ExitCode {
154154 micromamba_args. extend ( args. arguments . iter ( ) . map ( |s| s. as_str ( ) ) ) ;
155155 micromamba ( & config, micromamba_args, true ) . exit_code ( )
156156 }
157- Subcommand :: Activate => {
158- // TODO: handle env name similar to run/create
159-
157+ Subcommand :: Activate ( args) => {
160158 let Some ( shell) = SupportedShell :: from_csm_hook ( ) else {
161159 error ! ( "Your shell does not appear to have the csm hook enabled" ) ;
162160 error ! ( "See 'csm init' for information on how to set up the hook" ) ;
163161 return ExitCode :: FAILURE ;
164162 } ;
165163
166- info ! ( "Activating..." ) ;
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 ;
167+ } ;
168+
169+ info ! ( "Activating environment '{}'..." , env_name) ;
167170
168171 // NOTE: Anything to stdout here is *evaluated by the user's shell*
169172 // Use the logging macros instead for user-facing output!
170- println ! ( "{}" , shell. set_env_var( "CSM_TEST" , "it_works" ) ) ;
171- println ! ( "{}" , shell. set_env_var( "CSM_ANOTHER" , "it_works_too" ) ) ;
173+
174+ // Start by adding the mamba prefix bin to PATH
175+ let Some ( mut env_path) = micromamba:: path_for_env ( & config, & env_name) else {
176+ error ! ( "Could not determine path for environment '{}'" , env_name) ;
177+ return ExitCode :: FAILURE ;
178+ } ;
179+ env_path. push ( "bin" ) ;
180+ println ! ( "{}" , shell. prepend_path( & env_path) ) ;
181+
182+ // And a few conda-specific vars
183+ println ! ( "{}" , shell. set_env_var( "CONDA_DEFAULT_ENV" , & env_name) ) ;
184+ println ! (
185+ "{}" ,
186+ shell. set_env_var( "CONDA_PREFIX" , & env_path. to_string_lossy( ) )
187+ ) ;
188+ println ! ( "{}" , shell. set_env_var( "CONDA_SHLVL" , "1" ) ) ;
189+
172190 ExitCode :: SUCCESS
173191 }
174192 _ => {
0 commit comments