-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use version-specific URLs for documentation references (#6966)
Signed-off-by: knqyf263 <[email protected]>
- Loading branch information
Showing
9 changed files
with
165 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
package app | ||
|
||
var ( | ||
ver = "dev" | ||
) | ||
var ver = "dev" | ||
|
||
func Version() string { | ||
return ver | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package doc_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aquasecurity/trivy/pkg/version/doc" | ||
) | ||
|
||
func TestBaseURL(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ver string | ||
want string | ||
}{ | ||
{ | ||
name: "dev", | ||
ver: "dev", | ||
want: "https://aquasecurity.github.io/trivy/dev", | ||
}, | ||
{ | ||
name: "semver", | ||
ver: "0.52.0", | ||
want: "https://aquasecurity.github.io/trivy/v0.52", | ||
}, | ||
{ | ||
name: "with v prefix", | ||
ver: "v0.52.0", | ||
want: "https://aquasecurity.github.io/trivy/v0.52", | ||
}, | ||
{ | ||
name: "pre-release", | ||
ver: "0.52.0-beta1", | ||
want: "https://aquasecurity.github.io/trivy/dev", | ||
}, | ||
{ | ||
name: "non-semver", | ||
ver: "1", | ||
want: "https://aquasecurity.github.io/trivy/dev", | ||
}, | ||
{ | ||
name: "empty", | ||
ver: "", | ||
want: "https://aquasecurity.github.io/trivy/dev", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := doc.BaseURL(tt.ver) | ||
require.Equal(t, tt.want, got.String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestURL(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
rawPath string | ||
fragment string | ||
want string | ||
}{ | ||
{ | ||
name: "path without slash", | ||
rawPath: "foo", | ||
want: "https://aquasecurity.github.io/trivy/dev/foo", | ||
}, | ||
{ | ||
name: "path with leading slash", | ||
rawPath: "/foo", | ||
want: "https://aquasecurity.github.io/trivy/dev/foo", | ||
}, | ||
{ | ||
name: "path with slash", | ||
rawPath: "foo/bar", | ||
want: "https://aquasecurity.github.io/trivy/dev/foo/bar", | ||
}, | ||
{ | ||
name: "path with fragment", | ||
rawPath: "foo", | ||
fragment: "bar", | ||
want: "https://aquasecurity.github.io/trivy/dev/foo#bar", | ||
}, | ||
{ | ||
name: "empty", | ||
rawPath: "", | ||
want: "https://aquasecurity.github.io/trivy/dev", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := doc.URL(tt.rawPath, tt.fragment) | ||
require.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters