Skip to content

Commit 0bc987f

Browse files
committed
stembuild: extract messenger from main()
1 parent 85a0cd9 commit 0bc987f

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

stembuild/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"context"
55
"flag"
6-
"fmt"
76
"os"
87
"path"
98
"strings"
@@ -20,18 +19,20 @@ import (
2019
)
2120

2221
func main() {
22+
mainMessenger := messenger.NewMainMessenger(os.Stdout, os.Stderr)
23+
2324
envs := os.Environ()
2425
for _, env := range envs {
2526
envName := strings.Split(env, "=")[0]
2627
if strings.HasPrefix(envName, "GOVC_") || strings.HasPrefix(envName, "GOVMOMI_") {
27-
fmt.Fprintf(os.Stderr, "Warning: The following environment variable is set and might override flags provided to stembuild: %s\n", envName) //nolint:errcheck
28+
mainMessenger.EnvironmentVariableWarning(envName)
2829
}
2930
}
3031

3132
s := "./StemcellAutomation.zip"
3233
err := os.WriteFile(s, assets.StemcellAutomation, 0644)
3334
if err != nil {
34-
fmt.Fprintln(os.Stderr, "Unable to write StemcellAutomation.zip") //nolint:errcheck
35+
mainMessenger.StemcellAutomationWarning()
3536
os.Exit(1)
3637
}
3738

@@ -66,8 +67,8 @@ func main() {
6667

6768
fs.Parse(os.Args[1:]) //nolint:errcheck
6869
if gf.ShowVersion {
69-
fmt.Fprintf(os.Stdout, "%s version %s, Windows Stemcell Building Tool\n\n", path.Base(os.Args[0]), version.Current) //nolint:errcheck
70-
os.Remove(s) //nolint:errcheck
70+
mainMessenger.VersionInfo(path.Base(os.Args[0]), version.Current)
71+
os.Remove(s) //nolint:errcheck
7172
os.Exit(0)
7273
}
7374

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package messenger
2+
3+
import (
4+
"fmt"
5+
"io"
6+
)
7+
8+
func NewMainMessenger(stdout io.Writer, stderr io.Writer) *MainMessenger {
9+
return &MainMessenger{stdout: stdout, stderr: stderr}
10+
}
11+
12+
type MainMessenger struct {
13+
stdout io.Writer
14+
stderr io.Writer
15+
}
16+
17+
func (m *MainMessenger) VersionInfo(executable string, version string) {
18+
write(m.stdout, fmt.Sprintf("%s version %s, Windows Stemcell Building Tool\n\n", executable, version))
19+
}
20+
21+
func (m *MainMessenger) EnvironmentVariableWarning(envVarName string) {
22+
write(m.stderr, fmt.Sprintf("Warning: The following environment variable is set and might override flags provided to stembuild: %s\n", envVarName))
23+
}
24+
25+
func (m *MainMessenger) StemcellAutomationWarning() {
26+
write(m.stderr, "Unable to write StemcellAutomation.zip")
27+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package messenger_test
2+
3+
import (
4+
"fmt"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
. "github.com/onsi/gomega/gbytes"
9+
10+
"github.com/cloudfoundry/bosh-windows-stemcell-builder/stembuild/messenger"
11+
)
12+
13+
var _ = Describe("MainMessenger", func() {
14+
var (
15+
outBuf *Buffer
16+
errBuf *Buffer
17+
m *messenger.MainMessenger
18+
)
19+
20+
BeforeEach(func() {
21+
outBuf = NewBuffer()
22+
errBuf = NewBuffer()
23+
m = messenger.NewMainMessenger(outBuf, errBuf)
24+
})
25+
26+
Describe("EnvironmentVariableWarning", func() {
27+
It("outputs the expected warning to stderr", func() {
28+
envVar := "SOME_VAR"
29+
m.EnvironmentVariableWarning(envVar)
30+
31+
Eventually(errBuf).Should(Say(fmt.Sprintf("Warning: The following environment variable is set and might override flags provided to stembuild: %s\n", envVar)))
32+
})
33+
})
34+
35+
Describe("StemcellAutomationWarning", func() {
36+
It("outputs the expected warning to stderr", func() {
37+
m.StemcellAutomationWarning()
38+
39+
Eventually(errBuf).Should(Say("Unable to write StemcellAutomation.zip"))
40+
})
41+
})
42+
43+
Describe("StemcellAutomationWarning", func() {
44+
It("outputs the expected warning to stderr", func() {
45+
m.StemcellAutomationWarning()
46+
47+
Eventually(errBuf).Should(Say("Unable to write StemcellAutomation.zip"))
48+
})
49+
})
50+
51+
Describe("PrintVersionInfo", func() {
52+
It("outputs the expected value to stdout", func() {
53+
executable := "custom-stembuild"
54+
version := "my.custom.version"
55+
m.VersionInfo(executable, version)
56+
57+
Eventually(outBuf).Should(Say(fmt.Sprintf("%s version %s, Windows Stemcell Building Tool\n\n", executable, version)))
58+
})
59+
})
60+
})

0 commit comments

Comments
 (0)