1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-07-30 13:47:46 +00:00

add capture-foreign-env module (#1099)

This is a modified version of [capture-foreign-env command from nushell
wiki](https://www.nushell.sh/cookbook/foreign_shell_scripts.html#detailed-explanation-of-capture-foreign-env).
Unlike the wiki version It relies on `null_byte` instead of `newline` as
a delimiter for environment variables dumped by the `env` command which
allows it to support multi-line foreign environment variables.
This commit is contained in:
jaredmontoya 2025-04-23 01:10:43 +02:00 committed by GitHub
parent 488b9b0bc3
commit a515c4217e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 0 deletions

View file

@ -0,0 +1,5 @@
# capture-foreign-env
This is a modified version of the [capture-foreign-env command from the nushell wiki](https://www.nushell.sh/cookbook/foreign_shell_scripts.html#detailed-explanation-of-capture-foreign-env).
Unlike the wiki version It relies on `null_byte` instead of `newline` as a delimiter for environment variables dumped by the `env` command which allows it to support multi-line foreign environment variables.

View file

@ -0,0 +1,28 @@
# Returns a record of changed env variables after running a non-nushell script's contents (passed via stdin), e.g. a bash script you want to "source"
export def main [
--shell (-s): string = /bin/sh
# The shell to run the script in
# (has to support '-c' argument and POSIX 'env', 'echo', 'eval' commands)
--arguments (-a): list<string> = []
# Additional command line arguments to pass to the foreign shell
] {
let script_contents = $in;
let env_out = with-env { SCRIPT_TO_SOURCE: $script_contents } {
^$shell ...$arguments -c `
env -0
echo -n '<ENV_CAPTURE_EVAL_FENCE>'
eval "$SCRIPT_TO_SOURCE"
echo -n '<ENV_CAPTURE_EVAL_FENCE>'
env -0 -u _ -u _AST_FEATURES -u SHLVL`
}
| split row '<ENV_CAPTURE_EVAL_FENCE>'
| {
before: ($in | first | str trim --char (char nul) | split row (char nul))
after: ($in | last | str trim --char (char nul) | split row (char nul))
}
$env_out.after
| where { |line| $line not-in $env_out.before }
| parse "{key}={value}"
| transpose --header-row --as-record
}