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 Nov 28, 2024
1 parent 46e9b8a commit c6330ad
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/.vitepress/cli_commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export const commands: { [key: string]: Command } = {
doctor: {
hide: false,
},
en: {
hide: false,
},
env: {
hide: false,
},
Expand Down
38 changes: 38 additions & 0 deletions docs/cli/en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# `mise en`

- **Usage**: `mise en [-s --shell <SHELL>] [DIR]`
- **Source code**: [`src/cli/en.rs`](https://github.com/jdx/mise/blob/main/src/cli/en.rs)

[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.

## Arguments

### `[DIR]`

Directory to start the shell in

**Default:** `.`

## Flags

### `-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"
1 change: 1 addition & 0 deletions docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Answer yes to all confirmation prompts
- [`mise direnv <SUBCOMMAND>`](/cli/direnv.md)
- [`mise direnv activate`](/cli/direnv/activate.md)
- [`mise doctor`](/cli/doctor.md)
- [`mise en [-s --shell <SHELL>] [DIR]`](/cli/en.md)
- [`mise env [FLAGS] [TOOL@VERSION]...`](/cli/env.md)
- [`mise exec [FLAGS] [TOOL@VERSION]... [COMMAND]...`](/cli/exec.md)
- [`mise generate <SUBCOMMAND>`](/cli/generate.md)
Expand Down
3 changes: 3 additions & 0 deletions man/man1/mise.1
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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 @@ -343,6 +343,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 @@ -20,6 +20,7 @@ mod current;
mod deactivate;
mod direnv;
mod doctor;
mod en;
mod env;
pub mod exec;
mod external;
Expand Down Expand Up @@ -159,6 +160,7 @@ pub enum Commands {
Deactivate(deactivate::Deactivate),
Direnv(direnv::Direnv),
Doctor(doctor::Doctor),
En(en::En),
Env(env::Env),
Exec(exec::Exec),
Generate(generate::Generate),
Expand Down Expand Up @@ -219,6 +221,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::Generate(cmd) => cmd.run(),
Expand Down
30 changes: 30 additions & 0 deletions xtasks/fig/src/mise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,36 @@ const completionSpec: Fig.Spec = {
],
"description": "Check mise installation for possible problems"
},
{
"name": [
"en"
],
"description": "[experimental] starts a new shell with the mise environment built from the current configuration",
"options": [
{
"name": [
"-s",
"--shell"
],
"description": "Shell to start",
"isRepeatable": false,
"args": {
"name": "shell",
"isOptional": false,
"isVariadic": false
}
}
],
"args": [
{
"name": "dir",
"description": "Directory to start the shell in",
"isOptional": true,
"isVariadic": false,
"template": "folders"
}
]
},
{
"name": [
"env",
Expand Down

0 comments on commit c6330ad

Please sign in to comment.