From dba028b3dc4812a2ad0a2771455eb4f69c5d8baf Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdx@users.noreply.github.com> Date: Thu, 22 Feb 2024 19:45:02 -0600 Subject: [PATCH] added "en" command Fixes #1655 --- src/cli/en.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/cli/mod.rs | 3 +++ 2 files changed, 61 insertions(+) create mode 100644 src/cli/en.rs diff --git a/src/cli/en.rs b/src/cli/en.rs new file mode 100644 index 000000000..cd6e76ba6 --- /dev/null +++ b/src/cli/en.rs @@ -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, +} + +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#"Examples: + + $ mise en . + $ node -v + v20.0.0 +"# +); diff --git a/src/cli/mod.rs b/src/cli/mod.rs index f23b65ac8..e4330a735 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -16,6 +16,7 @@ mod current; mod deactivate; mod direnv; mod doctor; +mod en; mod env; pub mod exec; mod external; @@ -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), @@ -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(),