summaryrefslogtreecommitdiff
path: root/internal/datafs/fsurl.go
blob: 27ee0eb0f894dae44d616a86fc4f6d1fda596729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package datafs

import (
	"net/url"
	"strings"
)

// SplitFSMuxURL splits a URL into a filesystem URL and a relative file path
func SplitFSMuxURL(in *url.URL) (*url.URL, string) {
	u := *in

	// git URLs are special - they have double-slashes that separate a repo
	// from a path in the repo. A missing double-slash means the path is the
	// root.
	switch u.Scheme {
	case "git", "git+file", "git+http", "git+https", "git+ssh":
		repo, base, _ := strings.Cut(u.Path, "//")
		u.Path = repo
		if base == "" {
			base = "."
		}

		return &u, base
	case "aws+sm":
		// An aws+sm URL can either be opaque or have a path with a leading
		// slash. If it's opaque, the URL must not contain a leading slash. If
		// it has a path, the URL must begin with a slash.
		if u.Opaque != "" {
			return &url.URL{Scheme: u.Scheme}, u.Opaque
		}

		return &url.URL{Scheme: u.Scheme, Path: "/"}, strings.TrimLeft(u.Path, "/")
	}

	// trim leading and trailing slashes - they are not part of a valid path
	// according to [io/fs.ValidPath]
	base := strings.Trim(u.Path, "/")

	if base == "" && u.Opaque != "" {
		base = u.Opaque
		u.Opaque = ""
	}

	if base == "" {
		base = "."
	}

	u.Path = "/"

	return &u, base
}