mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
Make ge, le etc. work with the Hugo Version number
This means that you can do something ala: ```html {{ if ge .Hugo.Version "0.36" }}Reasonable new Hugo version!{{ end }} ``` The intented use is feature toggling, but please note that it will take some time and Hugo versions until this can be trusted. It does not work in older Hugo versions. Fixes #4443
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gohugoio/hugo/compare"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
@@ -34,10 +35,40 @@ type HugoVersion struct {
|
||||
Suffix string
|
||||
}
|
||||
|
||||
var (
|
||||
_ compare.Eqer = (*HugoVersionString)(nil)
|
||||
_ compare.Comparer = (*HugoVersionString)(nil)
|
||||
)
|
||||
|
||||
type HugoVersionString string
|
||||
|
||||
func (v HugoVersion) String() string {
|
||||
return hugoVersion(v.Number, v.PatchLevel, v.Suffix)
|
||||
}
|
||||
|
||||
func (v HugoVersion) Version() HugoVersionString {
|
||||
return HugoVersionString(v.String())
|
||||
}
|
||||
|
||||
func (h HugoVersionString) String() string {
|
||||
return string(h)
|
||||
}
|
||||
|
||||
// Implements compare.Comparer
|
||||
func (h HugoVersionString) Compare(other interface{}) int {
|
||||
v := MustParseHugoVersion(h.String())
|
||||
return compareVersions(v.Number, v.PatchLevel, other)
|
||||
}
|
||||
|
||||
// Implements compare.Eqer
|
||||
func (h HugoVersionString) Eq(other interface{}) bool {
|
||||
s, err := cast.ToStringE(other)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return s == h.String()
|
||||
}
|
||||
|
||||
// ParseHugoVersion parses a version string.
|
||||
func ParseHugoVersion(s string) (HugoVersion, error) {
|
||||
var vv HugoVersion
|
||||
|
@@ -29,6 +29,11 @@ func TestHugoVersion(t *testing.T) {
|
||||
require.Equal(t, v.ReleaseVersion().String(), "0.21")
|
||||
require.Equal(t, "0.21-DEV", v.String())
|
||||
require.Equal(t, "0.22", v.Next().String())
|
||||
nextVersionString := v.Next().Version()
|
||||
require.Equal(t, "0.22", nextVersionString.String())
|
||||
require.True(t, nextVersionString.Eq("0.22"))
|
||||
require.False(t, nextVersionString.Eq("0.21"))
|
||||
require.True(t, nextVersionString.Eq(nextVersionString))
|
||||
require.Equal(t, "0.20.3", v.NextPatchLevel(3).String())
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user