-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversioninfo.go
72 lines (59 loc) · 1.75 KB
/
versioninfo.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
package buildinfo
import (
"fmt"
)
const (
// default value for VersionInfo.Version
DefaultVersion = "0.0.0"
// default value for VersionInfo.Revision
DefaultRevision = "HEAD"
// default value for VersionInfo.Branch
DefaultBranch = "trunk"
)
const (
versionConcat = "-"
)
// VersionInfo contains data about the project state in a version control system
type VersionInfo struct {
Version string `json:"version,omitempty"`
Revision string `json:"revision,omitempty"`
Branch string `json:"branch,omitempty"`
}
// NewVersionInfo returns a VersionInfo instance with default values
func NewVersionInfo() *VersionInfo {
return &VersionInfo{
Version: DefaultVersion,
Revision: DefaultRevision,
Branch: DefaultBranch,
}
}
// String returns version, branch and revision information.
func (i *VersionInfo) String() string {
return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", i.Version, i.Branch, i.Revision)
}
// Clone creates an independant copy of itself.
func (i *VersionInfo) Clone() *VersionInfo {
i2 := *i
return &i2
}
// Equal compares the fields of this instance to the given one
func (i *VersionInfo) Equal(o *VersionInfo) bool {
if i == nil || o == nil {
return i == nil && o == nil
}
return i.Version == o.Version && i.Revision == o.Revision && i.Branch == o.Branch
}
// ShortRevision returns the truncated Revision.
// If that value is less than 8 characters long, the result
// is the Revision value itself
func (i *VersionInfo) ShortRevision() string {
if len(i.Revision) < 8 {
return i.Revision
}
return i.Revision[0:8]
}
// VersionRevision returns the Version and (short) Revision value
// concatenated by an hyphen character
func (i *VersionInfo) VersionRevision() string {
return i.Version + versionConcat + i.ShortRevision()
}