expand paths and split PATH in std path add (#10710)

related to
-
https://discord.com/channels/601130461678272522/614593951969574961/1162406310155923626

# Description
this PR
- does a bit of minor refactoring
- makes sure the input paths get expanded
- makes sure the input PATH gets split on ":"
- adds a test
- fixes the other tests

# User-Facing Changes
should give a better overall experience with `std path add`

# Tests + Formatting
adds a new test case to the `path_add` test and fixes the others.

# After Submitting
This commit is contained in:
Antoine Stevan
2023-10-25 16:43:27 +02:00
committed by GitHub
parent f3656f7822
commit a11e41332c
2 changed files with 22 additions and 16 deletions

View File

@ -40,7 +40,7 @@ export def --env "path add" [
...paths # the paths to add to $env.PATH.
] {
let span = (metadata $paths).span
let paths = ($paths | flatten)
let paths = $paths | flatten
if ($paths | is-empty) or ($paths | length) == 0 {
error make {msg: "Empty input", label: {
@ -52,13 +52,14 @@ export def --env "path add" [
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
let paths = $paths | each {|p|
let p = match ($p | describe | str replace --regex '<.*' '') {
"string" => $p,
"record" => { $p | get --ignore-errors $nu.os-info.name },
}
})
$p | path expand
}
if null in $paths or ($paths | is-empty) {
error make {msg: "Empty input", label: {
@ -70,9 +71,10 @@ export def --env "path add" [
load-env {$path_name: (
$env
| get $path_name
| if $append { append $paths }
else { prepend $paths }
| get $path_name
| split row (char esep)
| path expand
| if $append { append $paths } else { prepend $paths }
)}
if $ret {