Skip to content

Commit

Permalink
fix: keep the drive letter prefix in the path for windows
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Rohozneanu <[email protected]>
  • Loading branch information
Alex Rohozneanu committed Oct 3, 2024
1 parent 69cd388 commit 5dbefd7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
22 changes: 19 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -575,11 +576,26 @@ func Dirs(paths []string) []string {
// SplitPrefix returns a tuple specifying the document prefix and the file
// path.
func SplitPrefix(path string) ([]string, string) {
// Non-prefixed URLs can be returned without modification and their contents
// can be rooted directly under data.
if strings.Index(path, "://") == strings.Index(path, ":") {
pathRegex := "^(?:[a-zA-Z]:[^:]+|file:///.+|[^:]+|https?://[^\\s]+)$"

// Matches:

// Strings like "foo/bar" that do not contain a colon at all, linux/macOS style paths.
// Windows-style paths like "c:a/b/c" where the colon is after a single letter (indicating a drive).
// URLs like "file:///a/b/c" and "file:///c:/a/b/c".
// HTTP or HTTPS urls like "http://openpolicyagent.org

// Rejects:

// Strings like "foo:/bar", "foo.bar:/baz" where there is a non-drive prefix before a colon.
// Strings like "x.y:file:///a/b/c" where there is a prefix with dots and a colon before a valid URL.

match, err := regexp.MatchString(pathRegex, path)

if match && err == nil {
return nil, path
}

parts := strings.SplitN(path, ":", 2)
if len(parts) == 2 && len(parts[0]) > 0 {
return strings.Split(parts[0], "."), parts[1]
Expand Down
4 changes: 4 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,10 @@ func TestSplitPrefix(t *testing.T) {
wantParts: []string{"foo"},
wantPath: "/bar",
},
{
input: "c:/a/b/c",
wantPath: "c:/a/b/c",
},
{
input: "foo.bar:/baz",
wantParts: []string{"foo", "bar"},
Expand Down

0 comments on commit 5dbefd7

Please sign in to comment.