From 9e667cc879935a36f46c066b2855bbb29fbc76a1 Mon Sep 17 00:00:00 2001 From: Mel Massadian Date: Fri, 26 May 2023 09:24:53 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20(stdlib)=20add=20os=20recor?= =?UTF-8?q?d=20support=20to=20path=20add=20(#9238)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Add "os record" support. # User-Facing Changes This don't change how path add works but just adds support for "os records" aka records whose key are at least one of: - linux - macos - windows Check the [test](https://github.com/melMass/nushell/blob/a917f1a9244a8bdc0d2739f9d7c8dd295e899b68/crates/nu-std/tests/test_std.nu#L31-L32) if that's not clear enough # Tests + Formatting # After Submitting --------- Co-authored-by: amtoine --- crates/nu-std/std/mod.nu | 31 +++++++++++++++++++++++++++++++ crates/nu-std/tests/test_std.nu | 8 ++++++++ 2 files changed, 39 insertions(+) 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)] + + } }