Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(env): Allow exporting env vars as dotenv format #3185

Merged
merged 5 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/cli/env.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `mise env`

- **Usage**: `mise env [-J --json] [-s --shell <SHELL>] [TOOL@VERSION]...`
- **Usage**: `mise env [FLAGS] [TOOL@VERSION]...`
- **Aliases**: `e`
- **Source code**: [`src/cli/env.rs`](https://github.com/jdx/mise/blob/main/src/cli/env.rs)

Expand All @@ -21,6 +21,10 @@ Tool(s) to use

Output in JSON format

### `-D --dotenv`

Output in dotenv format

### `-s --shell <SHELL>`

Shell type to generate environment variables for
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 env [-J --json] [-s --shell <SHELL>] [TOOL@VERSION]...`](/cli/env.md)
- [`mise env [FLAGS] [TOOL@VERSION]...`](/cli/env.md)
- [`mise exec [FLAGS] [TOOL@VERSION]... [COMMAND]...`](/cli/exec.md)
- [`mise generate <SUBCOMMAND>`](/cli/generate.md)
- [`mise generate git-pre-commit [FLAGS]`](/cli/generate/git-pre-commit.md)
Expand Down
8 changes: 8 additions & 0 deletions e2e/cli/test_env_dotenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

cat >.mise.toml <<EOF
[env]
FOO = "bar"
EOF

assert "mise env --dotenv | grep FOO" "FOO=bar"
1 change: 1 addition & 0 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ use this if you have `mise activate` in your shell rc file."
$ execx($(mise env -s xonsh))
"#
flag "-J --json" help="Output in JSON format"
flag "-D --dotenv" help="Output in dotenv format"
flag "-s --shell" help="Shell type to generate environment variables for" {
arg "<SHELL>" {
choices "bash" "elvish" "fish" "nu" "xonsh" "zsh"
Expand Down
15 changes: 15 additions & 0 deletions src/cli/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub struct Env {
#[clap(long, short = 'J', overrides_with = "shell")]
json: bool,

/// Output in dotenv format
#[clap(long, short = 'D', overrides_with = "shell")]
dotenv: bool,

/// Shell type to generate environment variables for
#[clap(long, short, overrides_with = "json")]
shell: Option<ShellType>,
Expand All @@ -34,6 +38,8 @@ impl Env {

if self.json {
self.output_json(&config, ts)
} else if self.dotenv {
self.output_dotenv(&config, ts)
} else {
self.output_shell(&config, ts)
}
Expand All @@ -55,6 +61,15 @@ impl Env {
}
Ok(())
}

fn output_dotenv(&self, config: &Config, ts: Toolset) -> Result<()> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs an e2e test

for (k, v) in ts.env(config)? {
let k = k.to_string();
let v = v.to_string();
miseprint!("{}={}\n", k, v)?;
}
Ok(())
}
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
Expand Down
8 changes: 8 additions & 0 deletions tasks/fig/src/mise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,14 @@ const completionSpec: Fig.Spec = {
"description": "Output in JSON format",
"isRepeatable": false
},
{
"name": [
"-D",
"--dotenv"
],
"description": "Output in dotenv format",
"isRepeatable": false
},
{
"name": [
"-s",
Expand Down
Loading