-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
57 lines (49 loc) · 1.24 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
package types
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"time"
)
// Version creates a formatted struct for output
type Version struct {
Version string `json:"version,omitempty"`
Commit string `json:"commit,omitempty"`
Date string `json:"date,omitempty"`
Started time.Time `json:"started,omitempty"`
}
// NewVersion will create a pointer to a new version object
func NewVersion(version string, commit string, date string) *Version {
return &Version{
Version: version,
Commit: commit,
Date: date,
Started: time.Now(),
}
}
func (v *Version) String() string {
if v.Version == "" || v.Version == v.Commit {
return v.Commit + "_" + v.Date
}
return v.Version + "_" + v.Commit + "_" + v.Date
}
// Output will add the versioning code
func (v *Version) Output(shortened bool) string {
var response string
if shortened {
response = v.ToShortened()
} else {
response = v.ToJSON()
}
return fmt.Sprintf("%s", response)
}
// ToJSON converts the Version into a JSON String
func (v *Version) ToJSON() string {
bytes, _ := json.Marshal(v)
return string(bytes) + "\n"
}
// ToShortened converts the Version into a JSON String
func (v *Version) ToShortened() string {
bytes, _ := yaml.Marshal(v)
return string(bytes) + "\n"
}