Skip to content

Commit 1febd54

Browse files
committed
feat: add max_path_depth() function
1 parent 2e2e216 commit 1febd54

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

.gitignore

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
.direnv
12
.env
23
.env.*
3-
!.env.example
4-
5-
dist/
4+
.idea/
5+
.scm-engine.yml
6+
.task/
7+
3rd-party/
68
bin/
9+
completions/
710
coverage.txt
11+
dist/
12+
manpages/
813
scm-engine
914
scm-engine.exe
10-
.task/
11-
.idea/
12-
.direnv
1315

14-
manpages/
15-
completions/
16-
3rd-party/
16+
!.env.example

.golangci.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ linters-settings:
177177
- tt
178178
- err
179179
- i
180+
- id
181+
- ok
180182

181183
tagalign:
182184
# Align and sort can be used together or separately.

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
- [`duration`](#duration)
5151
- [`uniq`](#uniq)
5252
- [`filepath_dir`](#filepath_dir)
53+
- [`max_path_depth`](#max_path_depth)
5354

5455
## Installation
5556

@@ -509,5 +510,14 @@ If the path is empty, `filepath_dir` returns ".". If the path consists entirely
509510
The returned path does not end in a separator unless it is the root directory.
510511

511512
```expr
512-
filepath_dir("/example/directory/file.go") == "/example/directory"
513+
filepath_dir("example/directory/file.go") == "example/directory"
514+
```
515+
516+
#### `max_path_depth`
517+
518+
`max_path_depth` takes a path structure, and limits it to the configured maximum depth. Particularly useful when using `generated` labels from a directory structure, and want to to have a label naming scheme that only uses path of the path.
519+
520+
```expr
521+
max_path_depth("path1/path2/path3/path4", 2), == "path1/path2"
522+
max_path_depth("path1/path2", 3), == "path1/path2"
513523
```

pkg/stdlib/functions.go

+27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"reflect"
99
"slices"
10+
"strings"
1011

1112
"github.com/expr-lang/expr"
1213
"github.com/xhit/go-str2duration/v2"
@@ -74,3 +75,29 @@ var Duration = expr.Function(
7475
},
7576
str2duration.ParseDuration,
7677
)
78+
79+
var MaxPathDepth = expr.Function(
80+
"max_path_depth",
81+
func(args ...any) (any, error) {
82+
if len(args) != 2 {
83+
return nil, errors.New("max_path_depth() expect exactly two arguments")
84+
}
85+
86+
input, ok := args[0].(string)
87+
if !ok {
88+
return nil, errors.New("first input to max_path_depth() must be of type 'string'")
89+
}
90+
91+
length, ok := args[1].(int)
92+
if !ok {
93+
return nil, errors.New("second input to max_path_depth() must be of type 'int'")
94+
}
95+
96+
chunks := strings.Split(input, "/")
97+
if len(chunks) <= length {
98+
return input, nil
99+
}
100+
101+
return strings.Join(chunks[0:length-1], "/"), nil
102+
},
103+
)

pkg/stdlib/stdlib.go

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ var Functions = []expr.Option{
1010
// filepath.Dir
1111
FilepathDir,
1212

13+
// some/deep/path/ok => some/deep
14+
MaxPathDepth,
15+
1316
// slices.Sort + slices.Compact
1417
Uniq,
1518
}

0 commit comments

Comments
 (0)