-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathdoc.go
49 lines (43 loc) · 1.1 KB
/
doc.go
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
package doc
import (
"fmt"
"net/url"
"path"
"strings"
"github.com/aquasecurity/go-version/pkg/semver"
"github.com/aquasecurity/trivy/pkg/version/app"
)
const devVersion = "dev"
// BaseURL returns the base URL for the versioned documentation
func BaseURL(ver string) *url.URL {
ver = canonicalVersion(ver)
return &url.URL{
Scheme: "https",
Host: "aquasecurity.github.io",
Path: path.Join("trivy", ver),
}
}
// URL returns the URL for the versioned documentation with the given path
func URL(rawPath, fragment string) string {
base := BaseURL(app.Version())
base.Path = path.Join(base.Path, rawPath)
base.Fragment = fragment
return base.String()
}
func canonicalVersion(ver string) string {
if ver == devVersion {
return ver
}
ver = strings.TrimPrefix(ver, "v")
v, err := semver.Parse(ver)
if err != nil {
return devVersion
}
// Replace pre-release with "dev"
// e.g. v0.34.0-beta1+snapshot-1
if v.IsPreRelease() || v.Metadata() != "" {
return devVersion
}
// Add "v" prefix and cut a patch number, "0.34.0" => "v0.34" for the URL
return fmt.Sprintf("v%d.%d", v.Major(), v.Minor())
}