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 4adac60 commit adebad7
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
12 changes: 12 additions & 0 deletions completions/_mise
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ _mise() {
(deactivate) __mise_deactivate_cmd && ret=0 ;;
(direnv) __mise_direnv_cmd && ret=0 ;;
(dr|doctor) __mise_doctor_cmd && ret=0 ;;
(en) __mise_en_cmd && ret=0 ;;
(e|env) __mise_env_cmd && ret=0 ;;
(x|exec) __mise_exec_cmd && ret=0 ;;
(g|global) __mise_global_cmd && ret=0 ;;
Expand Down Expand Up @@ -313,6 +314,16 @@ __mise_doctor_cmd() {
'*'{-v,--verbose}'[Show extra output (use -vv for even more)]' \
'(-y --yes)'{-y,--yes}'[Answer yes to all confirmation prompts]'
}
(( $+functions[__mise_en_cmd] )) ||
__mise_en_cmd() {
_arguments -s -S \
'::dir:_files' \
'(-s --shell)'{-s,--shell}'=[Shell to start]:shell:' \
'(-C --cd)'{-C,--cd}'=[Change directory before running command]:cd:_directories' \
'(-q --quiet)'{-q,--quiet}'[Suppress non-error messages]' \
'*'{-v,--verbose}'[Show extra output (use -vv for even more)]' \
'(-y --yes)'{-y,--yes}'[Answer yes to all confirmation prompts]'
}
(( $+functions[__mise_env_cmd] )) ||
__mise_env_cmd() {
_arguments -s -S \
Expand Down Expand Up @@ -928,6 +939,7 @@ __mise_cmds() {
'deactivate:Disable mise for current shell session'
'direnv:Output direnv function to use mise inside direnv'
{dr,doctor}':Check mise installation for possible problems'
'en:\[experimental\] starts a new shell with the mise environment built from the current configuration'
{e,env}':Exports env vars to activate mise a single time'
{x,exec}':Execute a command with tool(s) set'
'implode:Removes mise CLI and all related data'
Expand Down
36 changes: 36 additions & 0 deletions docs/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,42 @@ Examples:
[WARN] plugin node is not installed
```

## `mise en [OPTIONS] [DIR]`

```text
[experimental] 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.
Usage: en [OPTIONS] [DIR]
Arguments:
[DIR]
Directory to start the shell in
[default: .]
Options:
-s, --shell <SHELL>
Shell to start
Defaults to $SHELL
Examples:
$ mise en .
$ node -v
v20.0.0
Skip loading bashrc:
$ mise en -s "bash --norc"
Skip loading zshrc:
$ mise en -s "zsh -f"
```

## `mise env [OPTIONS] [TOOL@VERSION]...`

**Aliases:** `e`
Expand Down
3 changes: 3 additions & 0 deletions man/man1/mise.1
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ Output direnv function to use mise inside direnv
mise\-doctor(1)
Check mise installation for possible problems
.TP
mise\-en(1)
[experimental] starts a new shell with the mise environment built from the current configuration
.TP
mise\-env(1)
Exports env vars to activate mise a single time
.TP
Expand Down
24 changes: 24 additions & 0 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,30 @@ cmd "doctor" help="Check mise installation for possible problems" {
[WARN] plugin node is not installed
"
}
cmd "en" help="[experimental] starts a new shell with the mise environment built from the current configuration" {
long_help r"[experimental] 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."
after_long_help r#"Examples:

$ mise en .
$ node -v
v20.0.0

Skip loading bashrc:
$ mise en -s "bash --norc"

Skip loading zshrc:
$ mise en -s "zsh -f"
"#
flag "-s --shell" help="Shell to start" {
long_help "Shell to start\n\nDefaults to $SHELL"
arg "<SHELL>"
}
arg "[DIR]" help="Directory to start the shell in" default="."
}
cmd "env" help="Exports env vars to activate mise a single time" {
alias "e"
long_help r"Exports env vars to activate mise a single time
Expand Down
60 changes: 60 additions & 0 deletions src/cli/en.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::cli::exec::Exec;
use crate::config::Settings;
use std::ffi::OsString;
use std::path::PathBuf;

use crate::env;

/// [experimental] 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 settings = Settings::get();
settings.ensure_experimental("en")?;

env::set_current_dir(&self.dir)?;
let shell = self.shell.or(env::var("SHELL").ok()).unwrap_or("sh".into());
let command = vec!["sh".into(), "-c".into(), OsString::from(shell)];

Exec {
tool: vec![],
raw: false,
jobs: None,
c: None,
command: Some(command),
}
.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
Skip loading bashrc:
$ <bold>mise en -s "bash --norc"</bold>
Skip loading zshrc:
$ <bold>mise en -s "zsh -f"</bold>
"#
);
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 adebad7

Please sign in to comment.