Skip to content

Commit

Permalink
added "en" command
Browse files Browse the repository at this point in the history
Fixes #1655
  • Loading branch information
jdx committed Feb 23, 2024
1 parent 17e6351 commit dba028b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/cli/en.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::path::PathBuf;
use crate::cli::exec::Exec;

use crate::env;

/// starts a new shell with the mise environment built from the current configuration
///
/// This is an alternative to `mise activate` that allows you to explicitly start a mise session.
/// It will have the tools and environment variables in the configs loaded.
/// Note that changing directories will not update the mise environment.
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct En {
/// Directory to start the shell in
#[clap(default_value = ".", verbatim_doc_comment)]
pub dir: PathBuf,

/// Shell to start
///
/// Defaults to $SHELL
#[clap(verbatim_doc_comment, long, short = 's')]
pub shell: Option<String>,
}

impl En {
pub fn run(self) -> eyre::Result<()> {
let shell = self.shell.or_else(|| {
match env::var("SHELL") {
Ok(shell) => {
let shell_type = shell.split('/').last().unwrap_or(&shell);
let shell = match shell_type {
"fish" => format!("{shell} -N"),
_ => shell,
};
Some(shell)
}
Err(_) => None,
}
}).unwrap_or("sh".into());
env::set_current_dir(&self.dir)?;
Exec {
tool: vec![],
raw: false,
jobs: None,
c: Some(shell.into()),
command: None,
}.run()
}
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>mise en .</bold>
$ <bold>node -v</bold>
v20.0.0
"#
);
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod current;
mod deactivate;
mod direnv;
mod doctor;
mod en;
mod env;
pub mod exec;
mod external;
Expand Down Expand Up @@ -70,6 +71,7 @@ pub enum Commands {
Deactivate(deactivate::Deactivate),
Direnv(direnv::Direnv),
Doctor(doctor::Doctor),
En(en::En),
Env(env::Env),
Exec(exec::Exec),
Global(global::Global),
Expand Down Expand Up @@ -125,6 +127,7 @@ impl Commands {
Self::Deactivate(cmd) => cmd.run(),
Self::Direnv(cmd) => cmd.run(),
Self::Doctor(cmd) => cmd.run(),
Self::En(cmd) => cmd.run(),
Self::Env(cmd) => cmd.run(),
Self::Exec(cmd) => cmd.run(),
Self::Global(cmd) => cmd.run(),
Expand Down

0 comments on commit dba028b

Please sign in to comment.