Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,31 @@ func ShuffleURLParameters(request string) string {

return p.String()
}

// A path join that chooses its separator based on what is already present in the given parts rather
// than what OS it is currently running under.
func UniversalPathJoin(parts ...string) string {
if len(parts) == 0 {
return ""
}

sep := "/"
for _, p := range parts {
if strings.Contains(p, "\\") {
sep = "\\"

break
}
}

for i, p := range parts {
if i == 0 {
// keep the root
parts[i] = strings.TrimRight(p, "/\\")
continue
}
parts[i] = strings.Trim(p, "/\\")
}

return strings.Join(parts, sep)
}
16 changes: 16 additions & 0 deletions transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,19 @@ func TestShuffleParametersInvalid(t *testing.T) {
t.Fatal("Invalid URL should be empty", s)
}
}

func TestUniversalPathJoin(t *testing.T) {
// unix check
want := "/apples/and/potatoes"
got := UniversalPathJoin("/apples/and", "potatoes")
if got != want {
t.Fatalf("Incorrect path recieved: wanted: %s, got: %s", want, got)
}

// windows check
want = "C:\\thanks\\for\\fish"
got = UniversalPathJoin("C:\\thanks\\for", "fish")
if got != want {
t.Fatalf("Incorrect path recieved: wanted: %s, got: %s", want, got)
}
}
Loading