-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91c9220
commit 1997788
Showing
6 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package build | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"text/template" | ||
) | ||
|
||
var ( | ||
// Version holds the tag of the build | ||
Version = "" | ||
// Commit holds the git commit sha | ||
Commit = "" | ||
// Date is the build date | ||
Date = "" | ||
) | ||
|
||
const versionTpl = ` | ||
Version: v{{ .Version }} | ||
SHA: {{ .Commit }} | ||
Built On: {{ .Date }}` | ||
|
||
// PrintVersion outputs the short version info | ||
func PrintVersion() { | ||
if Version != "" { | ||
fmt.Printf("v%s\n", Version) | ||
} | ||
} | ||
|
||
// PrintLongVersion outputs the full version info | ||
func PrintLongVersion() error { | ||
if Version == "" { | ||
return nil | ||
} | ||
|
||
data := struct { | ||
Version string | ||
Commit string | ||
Date string | ||
}{ | ||
Version: Version, | ||
Commit: Commit, | ||
Date: Date, | ||
} | ||
|
||
var tpl bytes.Buffer | ||
|
||
t, err := template.New("build").Parse(versionTpl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := t.Execute(&tpl, data); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(tpl.String()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package build | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestKubeconfig(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Build Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package build | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Build/PrintVersion", func() { | ||
It("Should print nothing if a version is not set", func() { | ||
// redirect stdout | ||
oldStdout := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
// all this function does is print to stdout | ||
Version = "" | ||
PrintVersion() | ||
|
||
// read captured stdout | ||
w.Close() | ||
out, _ := ioutil.ReadAll(r) | ||
|
||
// restore stdout | ||
os.Stdout = oldStdout | ||
|
||
Expect(out).To(BeEmpty()) | ||
}) | ||
|
||
It("Should print a version if it was set", func() { | ||
// redirect stdout | ||
oldStdout := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
// all this function does is print to stdout | ||
Version = "1.2.3" | ||
PrintVersion() | ||
|
||
// read captured stdout | ||
w.Close() | ||
out, _ := ioutil.ReadAll(r) | ||
|
||
// restore stdout | ||
os.Stdout = oldStdout | ||
|
||
Expect(string(out)).To(Equal("v1.2.3\n")) | ||
}) | ||
}) | ||
|
||
var _ = Describe("Build/PrintLongVersion", func() { | ||
It("Should print nothing if a version is not set", func() { | ||
// redirect stdout | ||
oldStdout := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
// all this function does is print to stdout | ||
Version = "" | ||
PrintLongVersion() | ||
|
||
// read captured stdout | ||
w.Close() | ||
out, _ := ioutil.ReadAll(r) | ||
|
||
// restore stdout | ||
os.Stdout = oldStdout | ||
|
||
Expect(out).To(BeEmpty()) | ||
}) | ||
|
||
It("Should print a version if it was set", func() { | ||
// redirect stdout | ||
oldStdout := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
// all this function does is print to stdout | ||
Version = "1.2.3" | ||
Commit = "abcdef1234" | ||
Date = "20200101" | ||
PrintLongVersion() | ||
|
||
// read captured stdout | ||
w.Close() | ||
out, _ := ioutil.ReadAll(r) | ||
|
||
// restore stdout | ||
os.Stdout = oldStdout | ||
|
||
Expect(string(out)).To(ContainSubstring("Version:")) | ||
Expect(string(out)).To(ContainSubstring("v1.2.3")) | ||
Expect(string(out)).To(ContainSubstring("SHA:")) | ||
Expect(string(out)).To(ContainSubstring("abcdef1234")) | ||
Expect(string(out)).To(ContainSubstring("Built On:")) | ||
Expect(string(out)).To(ContainSubstring("20200101")) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters