-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow any number of subdirectories in /etc/crowdsec/{hubtype}
- Loading branch information
Showing
4 changed files
with
168 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cwhub | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// relativePathComponents returns the list of path components after baseDir. | ||
// If path is not inside baseDir, it returns an empty slice. | ||
func relativePathComponents(path string, baseDir string) []string { | ||
absPath, err := filepath.Abs(path) | ||
if err != nil { | ||
return []string{} | ||
} | ||
absBaseDir, err := filepath.Abs(baseDir) | ||
if err != nil { | ||
return []string{} | ||
} | ||
|
||
// is path inside baseDir? | ||
relPath, err := filepath.Rel(absBaseDir, absPath) | ||
if err != nil || strings.HasPrefix(relPath, "..") || relPath == "." { | ||
return []string{} | ||
} | ||
|
||
return strings.Split(relPath, string(filepath.Separator)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cwhub | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRelativePathComponents(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
baseDir string | ||
expected []string | ||
}{ | ||
{ | ||
name: "Path within baseDir", | ||
path: "/home/user/project/src/file.go", | ||
baseDir: "/home/user/project", | ||
expected: []string{"src", "file.go"}, | ||
}, | ||
{ | ||
name: "Path is baseDir", | ||
path: "/home/user/project", | ||
baseDir: "/home/user/project", | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "Path outside baseDir", | ||
path: "/home/user/otherproject/src/file.go", | ||
baseDir: "/home/user/project", | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "Path is subdirectory of baseDir", | ||
path: "/home/user/project/src/", | ||
baseDir: "/home/user/project", | ||
expected: []string{"src"}, | ||
}, | ||
{ | ||
name: "Relative paths", | ||
path: "project/src/file.go", | ||
baseDir: "project", | ||
expected: []string{"src", "file.go"}, | ||
}, | ||
{ | ||
name: "BaseDir with trailing slash", | ||
path: "/home/user/project/src/file.go", | ||
baseDir: "/home/user/project/", | ||
expected: []string{"src", "file.go"}, | ||
}, | ||
{ | ||
name: "Empty baseDir", | ||
path: "/home/user/project/src/file.go", | ||
baseDir: "", | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "Empty path", | ||
path: "", | ||
baseDir: "/home/user/project", | ||
expected: []string{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := relativePathComponents(tt.path, tt.baseDir) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters