A place to share Nushell scripts with each other
Go to file
Texas Toland 74ba060f55
[stdlib-candidate] set-env (#787)
Rewrite of nushell/nushell#12156 for jdx/mise#1763.

### Why?

Nushell philosophically omits a `set` list mutation. But `$env` is
inherently mutable leading to issues described in nushell/nushell#12148.
`set-env` provides such an operation exclusively for `$env`.

### What changed?

1. Explicit flag instead of implicit list concatenation
2. Expands updates to any `$env` field not only `$env.config`

### How is it used?

```yaml
❯ set-env -h
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'

Usage:
  > main {flags} <field> <value>

Flags:
  -a, --append - Append to the previous value or wrap in a new list
  -h, --help - Display the help message for this command

Parameters:
  field <cell-path>: The environment variable name or nested option cell path
  value <any>: The value to set or append

Input/output types:
  ╭───┬─────────┬─────────╮
  │ # │  input  │ output  │
  ├───┼─────────┼─────────┤
  │ 0 │ nothing │ nothing │
  ╰───┴─────────┴─────────╯
```

### How does it work?

```nushell
export def --env main [
  field: cell-path
  value: any
  --append (-a)
]: nothing -> nothing {

  # just an alias
  def 'get or' [default field] {
    get --ignore-errors $field | default $default
  }

  let value = if $append {
    # append to the previous value or empty list
    $env | get or [] $field | append $value
  } else {
    $value
  }

  # work around nushell/nushell#12168
  let field = $field | to text | split row .
  let value = match $field {

    [_] => $value
    # if cell path is nested
    [$root, ..$field] => {
      let field = $field | into cell-path

      # reassigning $env would be an error
      # merging reserved names like PWD would be an error
      # so merge from 1 level deep instead
      $env | get or {} $root | upsert $field $value
    }
  }

  # avoid issues noted above
  load-env { ($field | first): $value }
}
```

### Where are the tests?

Pending next PR for nupm integration.
2024-03-12 10:55:07 -05:00
aliases change the string interpolation in git aliases (#735) 2024-01-14 18:05:35 -06:00
assets move assets so they're more accessible (#445) 2023-04-12 08:29:47 -05:00
before_v0.60 🐛 fix a couple of parser errors (#782) 2024-03-10 14:05:01 -05:00
benchmarks fix removed commands (#645) 2023-10-19 19:35:23 +02:00
custom-completions 🐛 fix a couple of parser errors (#782) 2024-03-10 14:05:01 -05:00
custom-menus move the extra menus of Nushell into custom-menus/extra/ (#550) 2023-07-21 10:44:27 +02:00
example-config export env is not in nushell (#529) 2023-06-17 07:48:08 -05:00
make_release Add option list-merged-pr --table (#774) 2024-03-07 06:19:29 -06:00
modules standardized parameter naming for --help and fix regex capture (#786) 2024-03-12 06:45:50 -05:00
nu-hooks rename package files (#701) 2024-02-18 16:23:47 +01:00
sourced 🐛 fix a couple of parser errors (#782) 2024-03-10 14:05:01 -05:00
stdlib-candidate [stdlib-candidate] set-env (#787) 2024-03-12 10:55:07 -05:00
themes Support for catppuccin-latte (#741) 2024-03-07 06:18:50 -06:00
.gitattributes Add Nushell Language detect for linguist (#532) 2023-06-21 11:36:01 +03:00
.gitignore update gitignore (#270) 2022-07-30 08:04:58 -05:00
LICENSE Initial commit 2021-01-23 07:33:45 +13:00
README.md fix broken link on README.md (#716) 2023-12-23 10:37:24 +01:00

Nushell Scripts

This is a place to share Nushell scripts with each other. If you'd like to share your scripts, fork this repository, and create a PR that adds it to the repo.

Sections

Running Scripts

You can run nushell scripts in a few different ways.

  1. You can type nu <script name>.
  2. From with nushell, you can type source <script name> and if the script is just a bunch of commands it will run the script. If the script is a custom command it will load those custom commands into your current scope so you can run them like any other command.