summaryrefslogtreecommitdiffstats
path: root/helpers
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-02-22 09:15:12 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-02-22 09:15:12 +0100
commit0602135fd44b0cfa0a51b0ec6e451ae58ac95666 (patch)
treebb045df0360b26dbacf682fa227d5d4e18d57bdc /helpers
parent55bd46a633d68f62e131457631ba839d6f876a55 (diff)
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
Diffstat (limited to 'helpers')
-rw-r--r--helpers/hugo.go31
-rw-r--r--helpers/hugo_test.go5
2 files changed, 36 insertions, 0 deletions
diff --git a/helpers/hugo.go b/helpers/hugo.go
index c4c3f4a76..b9b42e6cf 100644
--- a/helpers/hugo.go
+++ b/helpers/hugo.go
@@ -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
diff --git a/helpers/hugo_test.go b/helpers/hugo_test.go
index 1f5e5193f..78742c705 100644
--- a/helpers/hugo_test.go
+++ b/helpers/hugo_test.go
@@ -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())
}