summaryrefslogtreecommitdiff
path: root/funcs/semver.go
blob: 0212c998711e579f957c7cf913683b4fc56db8b9 (plain)
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
package funcs

import (
	"context"

	"github.com/Masterminds/semver/v3"
)

// CreateSemverFuncs -
func CreateSemverFuncs(ctx context.Context) map[string]interface{} {
	ns := &SemverFuncs{ctx}
	return map[string]interface{}{
		"semver": func() interface{} { return ns },
	}
}

// SemverFuncs -
type SemverFuncs struct {
	ctx context.Context
}

// Semver -
func (SemverFuncs) Semver(version string) (*semver.Version, error) {
	return semver.NewVersion(version)
}

// CheckConstraint -
func (SemverFuncs) CheckConstraint(constraint, in string) (bool, error) {
	c, err := semver.NewConstraint(constraint)
	if err != nil {
		return false, err
	}

	v, err := semver.NewVersion(in)
	if err != nil {
		return false, err
	}

	return c.Check(v), nil
}