diff --git a/crates/nu-std/std/mod.nu b/crates/nu-std/std/mod.nu index 5d6c7fb147..8e8308df7f 100644 --- a/crates/nu-std/std/mod.nu +++ b/crates/nu-std/std/mod.nu @@ -30,13 +30,44 @@ use dt.nu [datetime-diff, pretty-print-duration] # │ 4 │ fooo │ # ╰───┴──────────╯ # ``` +# - adding paths based on the operating system +# ```nushell +# >_ std path add {linux: "foo", windows: "bar", darwin: "baz"} +# ``` export def-env "path add" [ --ret (-r) # return $env.PATH, useful in pipelines to avoid scoping. --append (-a) # append to $env.PATH instead of prepending to. ...paths # the paths to add to $env.PATH. ] { + let span = (metadata $paths).span + let paths = ($paths | flatten) + + if ($paths | is-empty) or ($paths | length) == 0 { + error make {msg: "Empty input", label: { + text: "Provide at least one string or a record", + start: $span.start, + end: $span.end + }} + } + let path_name = if "PATH" in $env { "PATH" } else { "Path" } + let paths = ($paths | each {|p| + if ($p | describe) == "string" { + $p + } else if ($p | describe | str starts-with "record") { + $p | get -i $nu.os-info.name + } + }) + + if null in $paths or ($paths | is-empty) { + error make {msg: "Empty input", label: { + text: $"Received a record, that does not contain a ($nu.os-info.name) key", + start: $span.start, + end: $span.end + }} + } + let-env $path_name = ( $env | get $path_name diff --git a/crates/nu-std/tests/test_std.nu b/crates/nu-std/tests/test_std.nu index 9a7c1452f4..f0e3e7e50b 100644 --- a/crates/nu-std/tests/test_std.nu +++ b/crates/nu-std/tests/test_std.nu @@ -24,6 +24,14 @@ export def test_path_add [] { assert equal (std path add "fooooo" --ret) ["fooooo", "foo", "bar", "baz"] assert equal (get_path) ["fooooo", "foo", "bar", "baz"] + + let-env $path_name = [] + let target_paths = {linux: "foo", windows: "bar", macos: "baz"} + + std path add $target_paths + assert equal (get_path) [($target_paths | get $nu.os-info.name)] + + } }