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

[stdlib-candidate] set-env #787

Merged
merged 2 commits into from
Mar 12, 2024
Merged
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
37 changes: 37 additions & 0 deletions stdlib-candidate/set-env.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Gracefully set an environment variable or merge a nested option.
#
# Examples:
# Set $env.NUPM_HOME
# > set-env NUPM_HOME $'($nu.home-path)/.local/share/nupm'
#
# Add to $env.NU_LIB_DIRS
# > set-env --append NU_LIB_DIRS $'($env.NUPM_HOME)/modules'
#
# Set a nested config option
# > set-env config.filesize.metric true
#
# Add a config hook
# > set-env -a config.hooks.pre_prompt 'ellie | print'
export def --env main [
field: cell-path # The environment variable name or nested option cell path
value: any # The value to set or append
--append (-a) # Append to the previous value or wrap in a new list
]: nothing -> nothing {
def 'get or' [default field] {
get --ignore-errors $field | default $default
}
let value = if $append {
$env | get or [] $field | append $value
} else {
$value
}
let field = $field | to text | split row .
let value = match $field {
[_] => $value
[$root, ..$field] => {
let field = $field | into cell-path
$env | get or {} $root | upsert $field $value
}
}
load-env { ($field | first): $value }
}