Skip to content

Commit ee5008f

Browse files
committed
stembuild: messenger for HelpCommand
1 parent 72198d5 commit ee5008f

File tree

3 files changed

+169
-19
lines changed

3 files changed

+169
-19
lines changed

stembuild/commandparser/help.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package commandparser
33
import (
44
"context"
55
"flag"
6-
"fmt"
76
"io"
87
"os"
98
"path"
109

1110
"github.com/google/subcommands"
1211

12+
"github.com/cloudfoundry/bosh-windows-stemcell-builder/stembuild/messenger"
1313
"github.com/cloudfoundry/bosh-windows-stemcell-builder/stembuild/version"
1414
)
1515

@@ -50,37 +50,29 @@ func (h *stembuildHelp) Usage() string {
5050
return h.commander.HelpCommand().Usage()
5151
}
5252

53-
func (h *stembuildHelp) Execute(c context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
54-
switch f.NArg() {
53+
func (h *stembuildHelp) Execute(ctx context.Context, flagSet *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
54+
switch flagSet.NArg() {
5555
case 0:
5656
h.Explain(h.commander.Output)
5757
return subcommands.ExitSuccess
5858

5959
default:
60-
return h.commander.HelpCommand().Execute(c, f, args)
60+
return h.commander.HelpCommand().Execute(ctx, flagSet, args)
6161
}
6262
}
6363

64-
func (h *stembuildHelp) Explain(w io.Writer) {
64+
func (h *stembuildHelp) Explain(output io.Writer) {
65+
helpMessenger := messenger.NewHelpMessenger(output)
6566

66-
fmt.Fprintf(w, "%s version %s, Windows Stemcell Building Tool\n\n", path.Base(os.Args[0]), version.Current) //nolint:errcheck
67-
fmt.Fprintf(w, "Usage: %s <global options> <command> <command flags>\n\n", path.Base(os.Args[0])) //nolint:errcheck
67+
helpMessenger.UsagePreamble(path.Base(os.Args[0]), version.Current)
6868

69-
fmt.Fprint(w, "Commands:\n") //nolint:errcheck
69+
helpMessenger.PrintCommandPreamble()
7070
for _, command := range *h.commands {
71-
if len(command.Name()) < 5 { // This help align the synopses when the commands are of different lengths
72-
fmt.Fprintf(w, " %s\t\t%s\n", command.Name(), command.Synopsis()) //nolint:errcheck
73-
} else {
74-
fmt.Fprintf(w, " %s\t%s\n", command.Name(), command.Synopsis()) //nolint:errcheck
75-
}
71+
helpMessenger.PrintCommand(command.Name(), command.Synopsis())
7672
}
7773

78-
fmt.Fprint(w, "\nGlobal Options:\n") //nolint:errcheck
74+
helpMessenger.PrintGlobalFlagPreamble()
7975
h.topLevelFlags.VisitAll(func(f *flag.Flag) {
80-
if len(f.Name) > 1 {
81-
fmt.Fprintf(w, " -%s\t%s\n", f.Name, f.Usage) //nolint:errcheck
82-
} else {
83-
fmt.Fprintf(w, " -%s\t\t%s\n", f.Name, f.Usage) //nolint:errcheck
84-
}
76+
helpMessenger.PrintGlobalFlag(f.Name, f.Usage)
8577
})
8678
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package messenger
2+
3+
import (
4+
"fmt"
5+
"io"
6+
)
7+
8+
func NewHelpMessenger(output io.Writer) *HelpMessenger {
9+
return &HelpMessenger{output: output}
10+
}
11+
12+
type HelpMessenger struct {
13+
output io.Writer
14+
}
15+
16+
func (m *HelpMessenger) UsagePreamble(executable string, version string) {
17+
write(m.output, fmt.Sprintf("%s version %s, Windows Stemcell Building Tool\n\n", executable, version))
18+
write(m.output, fmt.Sprintf("Usage: %s <global options> <command> <command flags>\n\n", executable))
19+
}
20+
21+
func (m *HelpMessenger) PrintCommandPreamble() {
22+
write(m.output, "Commands:\n")
23+
}
24+
25+
func (m *HelpMessenger) PrintCommand(command string, description string) {
26+
if len(command) < 5 { // This help align the synopses when the commands are of different lengths
27+
write(m.output, fmt.Sprintf(" %s\t\t%s\n", command, description))
28+
} else {
29+
write(m.output, fmt.Sprintf(" %s\t%s\n", command, description))
30+
}
31+
}
32+
33+
func (m *HelpMessenger) PrintGlobalFlagPreamble() {
34+
write(m.output, "\nGlobal Options:\n")
35+
}
36+
37+
func (m *HelpMessenger) PrintGlobalFlag(flag string, usage string) {
38+
if len(flag) > 1 {
39+
write(m.output, fmt.Sprintf(" -%s\t%s\n", flag, usage))
40+
} else {
41+
write(m.output, fmt.Sprintf(" -%s\t\t%s\n", flag, usage))
42+
}
43+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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("HelpMessenger", func() {
14+
var (
15+
buf *Buffer
16+
m *messenger.HelpMessenger
17+
)
18+
19+
BeforeEach(func() {
20+
buf = NewBuffer()
21+
m = messenger.NewHelpMessenger(buf)
22+
})
23+
24+
Describe("UsagePreamble", func() {
25+
It("outputs the expected text", func() {
26+
executable := "STEMBUILD_EXECUTABLE"
27+
version := "STEMBUILD_VERSION"
28+
m.UsagePreamble(executable, version)
29+
30+
expectedText :=
31+
fmt.Sprintf("%s version %s, Windows Stemcell Building Tool\n\n", executable, version) +
32+
fmt.Sprintf("Usage: %s <global options> <command> <command flags>\n\n", executable)
33+
34+
Eventually(buf).Should(Say(expectedText))
35+
})
36+
})
37+
38+
Describe("PrintCommandPreamble", func() {
39+
It("outputs the expected text", func() {
40+
m.PrintCommandPreamble()
41+
42+
Eventually(buf).Should(Say("Commands:\n"))
43+
})
44+
})
45+
46+
Describe("PrintCommand", func() {
47+
var (
48+
command string
49+
description = "COMMAND DESCRIPTION"
50+
)
51+
52+
Context("when the command name is shorter than 5 characters", func() {
53+
BeforeEach(func() {
54+
command = "CMD"
55+
})
56+
57+
It("outputs the expected text", func() {
58+
m.PrintCommand(command, description)
59+
60+
Eventually(buf).Should(Say(fmt.Sprintf(" %s\t\t%s\n", command, description)))
61+
})
62+
})
63+
64+
Context("when the command name is longer than 5 characters", func() {
65+
BeforeEach(func() {
66+
command = "LONG_CMD"
67+
})
68+
69+
It("outputs the expected text", func() {
70+
m.PrintCommand(command, description)
71+
72+
Eventually(buf).Should(Say(fmt.Sprintf(" %s\t%s\n", command, description)))
73+
})
74+
})
75+
})
76+
77+
Describe("PrintGlobalFlagPreamble", func() {
78+
It("outputs the expected text", func() {
79+
m.PrintGlobalFlagPreamble()
80+
81+
Eventually(buf).Should(Say("\nGlobal Options:\n"))
82+
})
83+
})
84+
85+
Describe("PrintGlobalFlag", func() {
86+
var (
87+
flag string
88+
usage = "FLAG USAGE"
89+
)
90+
91+
Context("when the flag is longer than 1 character", func() {
92+
BeforeEach(func() {
93+
flag = "long-flag"
94+
})
95+
96+
It("outputs the expected text", func() {
97+
m.PrintGlobalFlag(flag, usage)
98+
99+
Eventually(buf).Should(Say(fmt.Sprintf(" -%s\t%s\n", flag, usage)))
100+
})
101+
})
102+
103+
Context("when the flag is shorter than 1 character", func() {
104+
BeforeEach(func() {
105+
flag = "f"
106+
})
107+
108+
It("outputs the expected text", func() {
109+
m.PrintGlobalFlag(flag, usage)
110+
111+
Eventually(buf).Should(Say(fmt.Sprintf(" -%s\t\t%s\n", flag, usage)))
112+
})
113+
})
114+
})
115+
})

0 commit comments

Comments
 (0)