Skip to content

Commit fee8dd1

Browse files
author
Christopher Hein
authored
Merge pull request #6 from christopherhein/beature/5-without-values
Adding Support for Empty Values
2 parents 74881aa + 40cb085 commit fee8dd1

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

version.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package version
22

33
import (
44
"encoding/json"
5-
"fmt"
5+
"strings"
66
)
77

88
// Info creates a formattable struct for output
99
type Info struct {
10-
Version string
11-
Commit string
12-
Date string
10+
Version string `json:"Version,omitempty"`
11+
Commit string `json:"Commit,omitempty"`
12+
Date string `json:"Date,omitempty"`
1313
}
1414

1515
// New will create a pointer to a new version object
@@ -28,7 +28,29 @@ func (v *Info) ToJSON() string {
2828
}
2929

3030
// ToShortened converts the Info into a JSON String
31-
func (v *Info) ToShortened() string {
32-
str := fmt.Sprintf("Version: %v\nCommit: %v\nDate: %v\n", v.Version, v.Commit, v.Date)
33-
return str
31+
func (v *Info) ToShortened() (str string) {
32+
var version, commit, date string
33+
if v.Version != "" {
34+
version = "Version: " + v.Version
35+
}
36+
if v.Commit != "" {
37+
commit = "Commit: " + v.Commit
38+
}
39+
if v.Date != "" {
40+
date = "Date: " + v.Date
41+
}
42+
values := []string{version, commit, date}
43+
values = deleteEmpty(values)
44+
str = strings.Join(values, "\n")
45+
return str + "\n"
46+
}
47+
48+
func deleteEmpty(s []string) []string {
49+
var r []string
50+
for _, str := range s {
51+
if str != "" {
52+
r = append(r, str)
53+
}
54+
}
55+
return r
3456
}

0 commit comments

Comments
 (0)