diff --git a/crates/nu-std/std/util/mod.nu b/crates/nu-std/std/util/mod.nu index c25af22542..3003802d63 100644 --- a/crates/nu-std/std/util/mod.nu +++ b/crates/nu-std/std/util/mod.nu @@ -7,14 +7,14 @@ path add "returned" --ret } } --result [returned bar baz foo fooo] -@example "adding paths based on the operating system" { - path add {linux: "foo", windows: "bar", darwin: "baz"} +@example "adding paths based on $nu.os-info.name" { + path add {linux: "foo", windows: "bar", macos: "baz"} } export def --env "path add" [ - --ret (-r) # return $env.PATH, useful in pipelines to avoid scoping. + --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. -] { + ...paths: any # the paths to add to $env.PATH. +]: [nothing -> nothing, nothing -> list] { let span = (metadata $paths).span let paths = $paths | flatten @@ -25,34 +25,36 @@ export def --env "path add" [ }} } + for path in $paths { + if ($path | describe -d).type not-in ['string', 'record'] { + error make {msg: 'Invalid input', label: { + text: 'Path must be a string or record', + span: (metadata $path).span + }} + } + } + let path_name = if "PATH" in $env { "PATH" } else { "Path" } let paths = $paths | each {|p| - let p = match ($p | describe | str replace --regex '<.*' '') { - "string" => $p, - "record" => { $p | get --ignore-errors $nu.os-info.name }, + match ($p | describe -d).type { + 'string' => { $p | path expand --no-symlink }, + 'record' => { + if $nu.os-info.name in ($p | columns) { + $p | get $nu.os-info.name | path expand --no-symlink + } + } } - - $p | path expand --no-symlink - } - - 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", - span: $span - }} - } + } | compact load-env {$path_name: ( - $env - | get $path_name - | split row (char esep) - | if $append { append $paths } else { prepend $paths } + $env | get $path_name + | split row (char esep) + | if $append { append $paths } else { prepend $paths } + | uniq )} - if $ret { - $env | get $path_name - } + if $ret { $env | get $path_name } } # The cute and friendly mascot of Nushell :) diff --git a/crates/nu-std/tests/test_std_util.nu b/crates/nu-std/tests/test_std_util.nu index 2b3203d667..40d7945234 100644 --- a/crates/nu-std/tests/test_std_util.nu +++ b/crates/nu-std/tests/test_std_util.nu @@ -39,6 +39,12 @@ def path_add [] { path add $target_paths assert equal (get_path) ([($target_paths | get $nu.os-info.name)] | path expand) + load-env {$path_name: []} + path add {} + assert equal (get_path) [] + + assert error {|| path add 1 } + load-env {$path_name: [$"(["/foo", "/bar"] | path expand | str join (char esep))"]} path add "~/foo" assert equal (get_path) (["~/foo", "/foo", "/bar"] | path expand) diff --git a/crates/nu-std/tests/test_util.nu b/crates/nu-std/tests/test_util.nu index ca99e2f654..97b0fe5d6f 100644 --- a/crates/nu-std/tests/test_util.nu +++ b/crates/nu-std/tests/test_util.nu @@ -39,6 +39,12 @@ def path_add [] { path add $target_paths assert equal (get_path) ([($target_paths | get $nu.os-info.name)] | path expand) + load-env {$path_name: []} + path add {} + assert equal (get_path) ([]) + + assert error {|| path add 1 } + load-env {$path_name: [$"(["/foo", "/bar"] | path expand | str join (char esep))"]} path add "~/foo" assert equal (get_path) (["~/foo", "/foo", "/bar"] | path expand)