Skip to content

Commit 5afd1d8

Browse files
committed
stembuild: simplify main()
- parse args early - print error if arg parsing fails - print version info early if requested - simplify Messenger struct - use one context.Background() - use defer to remove stemcell automation artifact
1 parent c79b5d5 commit 5afd1d8

File tree

3 files changed

+25
-65
lines changed

3 files changed

+25
-65
lines changed

stembuild/main.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"flag"
6+
"fmt"
67
"os"
78
"path"
89
"strings"
@@ -26,9 +27,25 @@ func main() {
2627
fs.BoolVar(&gf.Color, "color", false, "Colorize debug output")
2728
fs.BoolVar(&gf.ShowVersion, "version", false, "Show Stembuild version")
2829
fs.BoolVar(&gf.ShowVersion, "v", false, "Stembuild version (shorthand)")
30+
2931
commander := subcommands.NewCommander(fs, path.Base(os.Args[0]))
3032

3133
stembuildMessenger := messenger.NewStembuildMessenger(commander.Output, commander.Error)
34+
35+
stembuildExecutable := path.Base(os.Args[0])
36+
stembuildArgs := os.Args[1:]
37+
38+
err := fs.Parse(stembuildArgs)
39+
if err != nil {
40+
stembuildMessenger.PrintErr(fmt.Sprintf("Unable to parse args: %v", stembuildArgs))
41+
os.Exit(1)
42+
}
43+
44+
if gf.ShowVersion {
45+
stembuildMessenger.PrintOut(fmt.Sprintf("%s version %s, Windows Stemcell Building Tool\n\n", stembuildExecutable, version.Current))
46+
os.Exit(0)
47+
}
48+
3249
logLevel := colorlogger.NONE
3350
if gf.Debug {
3451
logLevel = colorlogger.DEBUG
@@ -39,16 +56,19 @@ func main() {
3956
for _, env := range envs {
4057
envName := strings.Split(env, "=")[0]
4158
if strings.HasPrefix(envName, "GOVC_") || strings.HasPrefix(envName, "GOVMOMI_") {
42-
stembuildMessenger.EnvironmentVariableWarning(envName)
59+
stembuildMessenger.PrintErr(fmt.Sprintf("Warning: The following environment variable is set and might override flags provided to stembuild: %s\n", envName))
4360
}
4461
}
4562

4663
s := "./StemcellAutomation.zip"
47-
err := os.WriteFile(s, assets.StemcellAutomation, 0644)
64+
err = os.WriteFile(s, assets.StemcellAutomation, 0644)
4865
if err != nil {
49-
stembuildMessenger.StemcellAutomationWarning()
66+
stembuildMessenger.PrintErr("Unable to write StemcellAutomation.zip")
5067
os.Exit(1)
5168
}
69+
defer os.Remove(s) //nolint:errcheck
70+
71+
ctx := context.Background()
5272

5373
var commands []subcommands.Command
5474
helpCmd := commandparser.NewStembuildHelp(commander, fs, &commands)
@@ -60,23 +80,13 @@ func main() {
6080
commander.Register(packageCmd, "")
6181
commands = append(commands, packageCmd)
6282

63-
constructCmd := commandparser.NewConstructCmd(context.Background(), &construct.Factory{}, &vcenter_manager.ManagerFactory{}, &commandparser.ConstructValidator{}, stembuildMessenger)
83+
constructCmd := commandparser.NewConstructCmd(ctx, &construct.Factory{}, &vcenter_manager.ManagerFactory{}, &commandparser.ConstructValidator{}, stembuildMessenger)
6484
constructCmd.GlobalFlags = &gf
6585
commander.Register(constructCmd, "")
6686
commands = append(commands, constructCmd)
6787

6888
// Override the default usage text of Google's Subcommand with our own
6989
fs.Usage = func() { helpCmd.Explain(commander.Error) }
7090

71-
fs.Parse(os.Args[1:]) //nolint:errcheck
72-
if gf.ShowVersion {
73-
stembuildMessenger.VersionInfo(path.Base(os.Args[0]), version.Current)
74-
os.Remove(s) //nolint:errcheck
75-
os.Exit(0)
76-
}
77-
78-
ctx := context.Background()
79-
i := int(commander.Execute(ctx))
80-
os.Remove(s) //nolint:errcheck
81-
os.Exit(i)
91+
os.Exit(int(commander.Execute(ctx)))
8292
}
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package messenger
22

33
import (
4-
"fmt"
54
"io"
65
)
76

@@ -21,15 +20,3 @@ func (m *StembuildMessenger) PrintOut(message string) {
2120
func (m *StembuildMessenger) PrintErr(message string) {
2221
write(m.stderr, message)
2322
}
24-
25-
func (m *StembuildMessenger) VersionInfo(executable string, version string) {
26-
write(m.stdout, fmt.Sprintf("%s version %s, Windows Stemcell Building Tool\n\n", executable, version))
27-
}
28-
29-
func (m *StembuildMessenger) EnvironmentVariableWarning(envVarName string) {
30-
write(m.stderr, fmt.Sprintf("Warning: The following environment variable is set and might override flags provided to stembuild: %s\n", envVarName))
31-
}
32-
33-
func (m *StembuildMessenger) StemcellAutomationWarning() {
34-
write(m.stderr, "Unable to write StemcellAutomation.zip")
35-
}
Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package messenger_test
22

33
import (
4-
"fmt"
5-
64
. "github.com/onsi/ginkgo/v2"
75
. "github.com/onsi/gomega"
86
. "github.com/onsi/gomega/gbytes"
@@ -40,39 +38,4 @@ var _ = Describe("StembuildMessenger", func() {
4038
Eventually(errBuf).Should(Say(message))
4139
})
4240
})
43-
44-
Describe("EnvironmentVariableWarning", func() {
45-
It("outputs the expected warning to stderr", func() {
46-
envVar := "SOME_VAR"
47-
m.EnvironmentVariableWarning(envVar)
48-
49-
Eventually(errBuf).Should(Say(fmt.Sprintf("Warning: The following environment variable is set and might override flags provided to stembuild: %s\n", envVar)))
50-
})
51-
})
52-
53-
Describe("StemcellAutomationWarning", func() {
54-
It("outputs the expected warning to stderr", func() {
55-
m.StemcellAutomationWarning()
56-
57-
Eventually(errBuf).Should(Say("Unable to write StemcellAutomation.zip"))
58-
})
59-
})
60-
61-
Describe("StemcellAutomationWarning", func() {
62-
It("outputs the expected warning to stderr", func() {
63-
m.StemcellAutomationWarning()
64-
65-
Eventually(errBuf).Should(Say("Unable to write StemcellAutomation.zip"))
66-
})
67-
})
68-
69-
Describe("PrintVersionInfo", func() {
70-
It("outputs the expected value to stdout", func() {
71-
executable := "custom-stembuild"
72-
version := "my.custom.version"
73-
m.VersionInfo(executable, version)
74-
75-
Eventually(outBuf).Should(Say(fmt.Sprintf("%s version %s, Windows Stemcell Building Tool\n\n", executable, version)))
76-
})
77-
})
7841
})

0 commit comments

Comments
 (0)