diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b4f51830f..dd5c5a0630 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ internal API changes are not present. Main (unreleased) ----------------- +### Features + +- Add the function `path_join` to the stdlib. (@wildum) + v1.1.0-rc.0 ----------- diff --git a/docs/sources/reference/stdlib/path_join.md b/docs/sources/reference/stdlib/path_join.md new file mode 100644 index 0000000000..649239b039 --- /dev/null +++ b/docs/sources/reference/stdlib/path_join.md @@ -0,0 +1,20 @@ +--- +canonical: https://grafana.com/docs/alloy/latest/reference/stdlib/path_join/ +description: Learn about path_join +title: path_join +--- + +# path_join + +`path_join` joins any number of path elements into a single path, separating them with an OS specific separator. + +## Examples + +```alloy +> path_join("this/is", "a/path") +"this/is/a/path" +> path_join("empty/path", "") +"empty/path" +> join("foo/", "/bar/", "foo/bar", "foo") +"foo/bar/foo/bar/foo" +``` diff --git a/syntax/internal/stdlib/stdlib.go b/syntax/internal/stdlib/stdlib.go index 1ec1d02cb5..745211b494 100644 --- a/syntax/internal/stdlib/stdlib.go +++ b/syntax/internal/stdlib/stdlib.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" "strings" "github.com/grafana/alloy/syntax/alloytypes" @@ -129,4 +130,5 @@ var Identifiers = map[string]interface{}{ "trim_prefix": strings.TrimPrefix, "trim_suffix": strings.TrimSuffix, "trim_space": strings.TrimSpace, + "path_join": filepath.Join, } diff --git a/syntax/vm/vm_stdlib_test.go b/syntax/vm/vm_stdlib_test.go index 0c91fb88a5..a55c9dfe5e 100644 --- a/syntax/vm/vm_stdlib_test.go +++ b/syntax/vm/vm_stdlib_test.go @@ -163,6 +163,7 @@ func TestStdlib_StringFunc(t *testing.T) { {"trim2", `trim(" hello! world.! ", "! ")`, "hello! world."}, {"trim_prefix", `trim_prefix("helloworld", "hello")`, "world"}, {"trim_suffix", `trim_suffix("helloworld", "world")`, "hello"}, + {"path_join", `path_join("this/is", "a/path")`, "this/is/a/path"}, } for _, tc := range tt {