/
version.go
87 lines (76 loc) · 2.14 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
package objects
import (
"bytes"
"encoding/json"
"strings"
"github.com/maahsome/gron"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Version struct {
SemVer string `json:"SemVer"`
GitCommit string `json:"GitCommit"`
BuildDate string `json:"BuildDate"`
GitRef string `json:"GitRef"`
}
// ToJSON - Write the output as JSON
func (v *Version) ToJSON() string {
versionJSON, err := json.MarshalIndent(v, "", " ")
if err != nil {
logrus.WithError(err).Error("Error extracting JSON")
return ""
}
return string(versionJSON[:])
}
func (v *Version) ToGRON() string {
versionJSON, err := json.MarshalIndent(v, "", " ")
if err != nil {
logrus.WithError(err).Error("Error extracting JSON for GRON")
}
subReader := strings.NewReader(string(versionJSON[:]))
subValues := &bytes.Buffer{}
ges := gron.NewGron(subReader, subValues)
ges.SetMonochrome(false)
if serr := ges.ToGron(); serr != nil {
logrus.WithError(serr).Error("Problem generating GRON syntax")
return ""
}
return string(subValues.Bytes())
}
func (v *Version) ToYAML() string {
versionYAML, err := yaml.Marshal(v)
if err != nil {
logrus.WithError(err).Error("Error extracting YAML")
return ""
}
return string(versionYAML[:])
}
func (v *Version) ToTEXT(noHeaders bool) string {
buf, row := new(bytes.Buffer), make([]string, 0)
// ************************** TableWriter ******************************
table := tablewriter.NewWriter(buf)
if !noHeaders {
table.SetHeader([]string{"COMPONENT", "VERSION"})
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
}
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
row = []string{"Version", v.SemVer}
table.Append(row)
row = []string{"BuildDate", v.BuildDate}
table.Append(row)
row = []string{"GitCommit", v.GitCommit}
table.Append(row)
row = []string{"GitRef", v.GitRef}
table.Append(row)
return buf.String()
}