This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semver.go
77 lines (65 loc) · 1.59 KB
/
semver.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Ⓒ 2017 Matthew Stobbs <[email protected]>
//
// Licensed under the MIT license. See the LICENSE file for details
// Package semver helps create semantic Version numbers according to the
// semver specification
package version
import (
"bytes"
"fmt"
"unicode"
)
// Version holds the structure of the semantic version information
type Version struct {
major uint
minor uint
patch uint
release string
build string
}
var version Version
// Set creates and returns a pointer to a semver Version number
func Set(major, minor, patch uint, release string, build string) {
version = Version{
major: major,
minor: minor,
patch: patch,
release: release,
build: build,
}
}
// Get returns the String() of the internal version
func Get() string {
return version.String()
}
// String returns a string of the Version
func (v *Version) String() string {
var Version = fmt.Sprintf("%d.%d.%d", v.major, v.minor, v.patch)
if v.release != "" {
v.release = checkString(v.release)
Version = fmt.Sprintf("%s-%s", Version, v.release)
}
if v.build != "" {
Version = fmt.Sprintf("%s+%s", Version, v.build)
}
return Version
}
// TODO: Fix to make static build numbers possible
func (v *Version) setbuild() {
if v.build == "" {
v.build = "nil"
}
}
// Checks a provided string to make sure it meets the requirements as a character in the Version number.
func checkString(s string) string {
var result bytes.Buffer
for _, r := range s {
if unicode.IsLetter(r) {
result.WriteRune(r)
}
if unicode.IsDigit(r) {
result.WriteRune(r)
}
}
return result.String()
}