path.go

v1.3.6
Doc Versions Source
1
package server
2
3
import "strings"
4
5
// ModuleName extracts the top-level module name from a URL path.
6
// Strips any @version suffix (e.g., "/curator@v1.0.0/..." → "curator").
7
func ModuleName(path string) string {
8
	path = strings.TrimPrefix(path, "/")
9
	if i := strings.Index(path, "/"); i != -1 {
10
		path = path[:i]
11
	}
12
	if mod, _, ok := strings.Cut(path, "@"); ok {
13
		return mod
14
	}
15
	return path
16
}
17
18
// ParseProxyPath parses "/<module>/@v/<file>" paths.
19
// Returns the full module path and file component (e.g. "list", "v1.0.0.info").
20
func ParseProxyPath(urlPath string) (modPath, file string, ok bool) {
21
	path := strings.TrimPrefix(urlPath, "/")
22
23
	idx := strings.Index(path, "/@v/")
24
	if idx == -1 {
25
		return "", "", false
26
	}
27
28
	modPath = path[:idx]
29
	file = path[idx+len("/@v/"):]
30
31
	if modPath == "" || file == "" {
32
		return "", "", false
33
	}
34
35
	return modPath, file, true
36
}
37
38
// ParseDocPath parses documentation URLs: "/<module>@<version>/<subpath>".
39
// Returns the module name, version (empty if not specified), and sub-path.
40
func ParseDocPath(urlPath string) (modName, version, subpath string) {
41
	path := strings.TrimPrefix(urlPath, "/")
42
43
	// Split on first "/" to get the first segment (which may contain @version).
44
	first, rest, _ := strings.Cut(path, "/")
45
46
	// Check for @version in first segment.
47
	if mod, ver, ok := strings.Cut(first, "@"); ok {
48
		return mod, ver, rest
49
	}
50
51
	return first, "", rest
52
}
53
54
// ParseLatestPath parses "/<module>/@latest" paths.
55
// Returns the escaped module path if the URL matches, or empty string if not.
56
func ParseLatestPath(urlPath string) (escapedModPath string, ok bool) {
57
	path := strings.TrimPrefix(urlPath, "/")
58
59
	if !strings.HasSuffix(path, "/@latest") {
60
		return "", false
61
	}
62
63
	modPath := strings.TrimSuffix(path, "/@latest")
64
	if modPath == "" {
65
		return "", false
66
	}
67
68
	return modPath, true
69
}
70
71
// StorageKey returns the S3 key for a proxy artifact.
72
func StorageKey(modName, file string) string {
73
	return modName + "/@v/" + file
74
}
75

Source Files