Skip to content

Commit 407b71f

Browse files
committed
feat: integrate doublestar for enhanced file matching in content workflow
- Added the `doublestar` package to support recursive glob pattern matching in the `ContentWorkflowResults` function. - Refactored file matching logic to utilize `findMatchingFiles`, improving the accuracy of file retrieval based on specified patterns. - Updated `go.mod` and `go.sum` to include the new dependency. These changes enhance the functionality of the content workflow by allowing for more flexible file matching capabilities.
1 parent 143e209 commit 407b71f

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

components/backend/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
)
1717

1818
require (
19+
github.com/bmatcuk/doublestar/v4 v4.9.1 // indirect
1920
github.com/bytedance/sonic v1.13.3 // indirect
2021
github.com/bytedance/sonic/loader v0.2.4 // indirect
2122
github.com/cloudwego/base64x v0.1.5 // indirect

components/backend/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/bmatcuk/doublestar/v4 v4.9.1 h1:X8jg9rRZmJd4yRy7ZeNDRnM+T3ZfHv15JiBJ/avrEXE=
2+
github.com/bmatcuk/doublestar/v4 v4.9.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
13
github.com/bytedance/sonic v1.13.3 h1:MS8gmaH16Gtirygw7jV91pDCN33NyMrPbN7qiYhEsF0=
24
github.com/bytedance/sonic v1.13.3/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
35
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=

components/backend/handlers/content.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"ambient-code-backend/git"
1717

18+
"github.com/bmatcuk/doublestar/v4"
1819
"github.com/gin-gonic/gin"
1920
)
2021

@@ -675,18 +676,7 @@ func ContentWorkflowResults(c *gin.Context) {
675676
results := []ResultFile{}
676677

677678
for displayName, pattern := range ambientConfig.Results {
678-
absPattern := filepath.Join(workspaceBase, pattern)
679-
matches, err := filepath.Glob(absPattern)
680-
681-
if err != nil {
682-
results = append(results, ResultFile{
683-
DisplayName: displayName,
684-
Path: pattern,
685-
Exists: false,
686-
Error: fmt.Sprintf("Invalid pattern: %v", err),
687-
})
688-
continue
689-
}
679+
matches := findMatchingFiles(workspaceBase, pattern)
690680

691681
if len(matches) == 0 {
692682
results = append(results, ResultFile{
@@ -719,6 +709,25 @@ func ContentWorkflowResults(c *gin.Context) {
719709
c.JSON(http.StatusOK, gin.H{"results": results})
720710
}
721711

712+
// findMatchingFiles finds files matching a glob pattern with ** support for recursive matching
713+
func findMatchingFiles(baseDir, pattern string) []string {
714+
// Use doublestar for glob matching with ** support
715+
fsys := os.DirFS(baseDir)
716+
matches, err := doublestar.Glob(fsys, pattern)
717+
if err != nil {
718+
log.Printf("findMatchingFiles: glob error for pattern %q: %v", pattern, err)
719+
return []string{}
720+
}
721+
722+
// Convert relative paths to absolute paths
723+
var absolutePaths []string
724+
for _, match := range matches {
725+
absolutePaths = append(absolutePaths, filepath.Join(baseDir, match))
726+
}
727+
728+
return absolutePaths
729+
}
730+
722731
// findActiveWorkflowDir finds the active workflow directory for a session
723732
func findActiveWorkflowDir(sessionName string) string {
724733
// Workflows are stored at {StateBaseDir}/sessions/{session-name}/workspace/workflows/{workflow-name}

0 commit comments

Comments
 (0)