forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
36 lines (29 loc) · 822 Bytes
/
version.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
package builder
import (
"github.com/Masterminds/semver"
"github.com/pkg/errors"
)
// Version is an extension to semver.Version to make it marshalable.
type Version struct {
semver.Version
}
func VersionMustParse(v string) *Version {
return &Version{Version: *semver.MustParse(v)}
}
func (v *Version) String() string {
return v.Version.String()
}
// MarshalText makes Version satisfy the encoding.TextMarshaler interface.
func (v *Version) MarshalText() ([]byte, error) {
return []byte(v.Version.Original()), nil
}
// UnmarshalText makes Version satisfy the encoding.TextUnmarshaler interface.
func (v *Version) UnmarshalText(text []byte) error {
s := string(text)
w, err := semver.NewVersion(s)
if err != nil {
return errors.Wrapf(err, "invalid semantic version %s", s)
}
v.Version = *w
return nil
}