Skip to content

Commit

Permalink
feat: add max_path_depth() function
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed May 6, 2024
1 parent 2e2e216 commit 1febd54
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
18 changes: 9 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
.direnv
.env
.env.*
!.env.example

dist/
.idea/
.scm-engine.yml
.task/
3rd-party/
bin/
completions/
coverage.txt
dist/
manpages/
scm-engine
scm-engine.exe
.task/
.idea/
.direnv

manpages/
completions/
3rd-party/
!.env.example
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ linters-settings:
- tt
- err
- i
- id
- ok

tagalign:
# Align and sort can be used together or separately.
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- [`duration`](#duration)
- [`uniq`](#uniq)
- [`filepath_dir`](#filepath_dir)
- [`max_path_depth`](#max_path_depth)

## Installation

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

```expr
filepath_dir("/example/directory/file.go") == "/example/directory"
filepath_dir("example/directory/file.go") == "example/directory"
```

#### `max_path_depth`

`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.

```expr
max_path_depth("path1/path2/path3/path4", 2), == "path1/path2"
max_path_depth("path1/path2", 3), == "path1/path2"
```
27 changes: 27 additions & 0 deletions pkg/stdlib/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"reflect"
"slices"
"strings"

"github.com/expr-lang/expr"
"github.com/xhit/go-str2duration/v2"
Expand Down Expand Up @@ -74,3 +75,29 @@ var Duration = expr.Function(
},
str2duration.ParseDuration,
)

var MaxPathDepth = expr.Function(
"max_path_depth",
func(args ...any) (any, error) {
if len(args) != 2 {
return nil, errors.New("max_path_depth() expect exactly two arguments")
}

input, ok := args[0].(string)
if !ok {
return nil, errors.New("first input to max_path_depth() must be of type 'string'")
}

length, ok := args[1].(int)
if !ok {
return nil, errors.New("second input to max_path_depth() must be of type 'int'")
}

chunks := strings.Split(input, "/")
if len(chunks) <= length {
return input, nil
}

return strings.Join(chunks[0:length-1], "/"), nil
},
)
3 changes: 3 additions & 0 deletions pkg/stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var Functions = []expr.Option{
// filepath.Dir
FilepathDir,

// some/deep/path/ok => some/deep
MaxPathDepth,

// slices.Sort + slices.Compact
Uniq,
}

0 comments on commit 1febd54

Please sign in to comment.