-
Notifications
You must be signed in to change notification settings - Fork 390
/
version.go
180 lines (149 loc) · 4.26 KB
/
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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package version
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/golang/protobuf/ptypes"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/pkg/pb"
)
var (
mon = monkit.Package()
// the following fields are set by linker flags. if any of them
// are set and fail to parse, the program will fail to start
buildTimestamp string // unix seconds since epoch
buildCommitHash string
buildVersion string // semantic version format
buildRelease string // true/false
// Build is a struct containing all relevant build information associated with the binary
Build Info
)
// Info is the versioning information for a binary
type Info struct {
// sync/atomic cache
commitHashCRC uint32
Timestamp time.Time `json:"timestamp,omitempty"`
CommitHash string `json:"commitHash,omitempty"`
Version SemVer `json:"version"`
Release bool `json:"release,omitempty"`
}
// SemVer represents a semantic version
type SemVer struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Patch int64 `json:"patch"`
}
// AllowedVersions provides a list of SemVer per Service
type AllowedVersions struct {
Bootstrap []SemVer
Satellite []SemVer
Storagenode []SemVer
Uplink []SemVer
Gateway []SemVer
Identity []SemVer
}
// SemVerRegex is the regular expression used to parse a semantic version.
// https://github.com/Masterminds/semver/blob/master/LICENSE.txt
const SemVerRegex string = `v?([0-9]+)\.([0-9]+)\.([0-9]+)`
var versionRegex = regexp.MustCompile("^" + SemVerRegex + "$")
// NewSemVer parses a given version and returns an instance of SemVer or
// an error if unable to parse the version.
func NewSemVer(v string) (*SemVer, error) {
m := versionRegex.FindStringSubmatch(v)
if m == nil {
return nil, errors.New("invalid semantic version for build")
}
sv := SemVer{}
var err error
// first entry of m is the entire version string
sv.Major, err = strconv.ParseInt(m[1], 10, 64)
if err != nil {
return nil, err
}
sv.Minor, err = strconv.ParseInt(m[2], 10, 64)
if err != nil {
return nil, err
}
sv.Patch, err = strconv.ParseInt(m[3], 10, 64)
if err != nil {
return nil, err
}
return &sv, nil
}
// String converts the SemVer struct to a more easy to handle string
func (sem *SemVer) String() (version string) {
return fmt.Sprintf("v%d.%d.%d", sem.Major, sem.Minor, sem.Patch)
}
// New creates Version_Info from a json byte array
func New(data []byte) (v Info, err error) {
err = json.Unmarshal(data, &v)
return v, err
}
// Marshal converts the existing Version Info to any json byte array
func (v Info) Marshal() (data []byte, err error) {
data, err = json.Marshal(v)
return
}
// Proto converts an Info struct to a pb.NodeVersion
// TODO: shouldn't we just use pb.NodeVersion everywhere? gogoproto will let
// us make it match Info.
func (v Info) Proto() (*pb.NodeVersion, error) {
pbts, err := ptypes.TimestampProto(v.Timestamp)
if err != nil {
return nil, err
}
return &pb.NodeVersion{
Version: v.Version.String(),
CommitHash: v.CommitHash,
Timestamp: pbts,
Release: v.Release,
}, nil
}
// containsVersion compares the allowed version array against the passed version
func containsVersion(all []SemVer, x SemVer) bool {
for _, n := range all {
if x == n {
return true
}
}
return false
}
// StrToSemVerList converts a list of versions to a list of SemVer
func StrToSemVerList(serviceVersions []string) (versions []SemVer, err error) {
for _, subversion := range serviceVersions {
sVer, err := NewSemVer(subversion)
if err != nil {
return nil, err
}
versions = append(versions, *sVer)
}
return versions, err
}
func init() {
if buildVersion == "" && buildTimestamp == "" && buildCommitHash == "" && buildRelease == "" {
return
}
timestamp, err := strconv.ParseInt(buildTimestamp, 10, 64)
if err != nil {
panic(fmt.Sprintf("invalid timestamp: %v", err))
}
Build = Info{
Timestamp: time.Unix(timestamp, 0),
CommitHash: buildCommitHash,
Release: strings.ToLower(buildRelease) == "true",
}
sv, err := NewSemVer(buildVersion)
if err != nil {
panic(err)
}
Build.Version = *sv
if Build.Timestamp.Unix() == 0 || Build.CommitHash == "" {
Build.Release = false
}
}