Skip to content

Commit 2757503

Browse files
authored
chore: Add Options to GenTreeDocs and move buf to io.Writer (#68)
1 parent c6158d5 commit 2757503

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

cobra2snooty.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ const (
3535
)
3636

3737
// GenTreeDocs generates the docs for the full tree of commands.
38-
func GenTreeDocs(cmd *cobra.Command, dir string) error {
38+
func GenTreeDocs(cmd *cobra.Command, dir string, genDocOptions ...GenDocsOption) error {
3939
for _, c := range cmd.Commands() {
4040
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
4141
continue
4242
}
43-
if err := GenTreeDocs(c, dir); err != nil {
43+
if err := GenTreeDocs(c, dir, genDocOptions...); err != nil {
4444
return err
4545
}
4646
}
@@ -53,7 +53,7 @@ func GenTreeDocs(cmd *cobra.Command, dir string) error {
5353
}
5454
defer f.Close()
5555

56-
return GenDocs(cmd, f)
56+
return GenDocs(cmd, f, genDocOptions...)
5757
}
5858

5959
const toc = `
@@ -198,7 +198,7 @@ func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
198198
func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
199199

200200
type GenDocsOptions struct {
201-
exampleFormatter func(buf *bytes.Buffer, cmd *cobra.Command)
201+
exampleFormatter ExampleFormatter
202202
timeGetter func() time.Time
203203
}
204204

cobra2snooty_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cobra2snooty
1717
import (
1818
"bytes"
1919
"fmt"
20+
"io"
2021
"os"
2122
"path/filepath"
2223
"strings"
@@ -317,9 +318,9 @@ func TestGenDocsSnapshots(t *testing.T) {
317318
WithCustomTimeGetter(func() time.Time {
318319
return time.Date(2025, 3, 5, 17, 0, 0, 0, time.UTC)
319320
}),
320-
WithCustomExampleFormatter(func(buf *bytes.Buffer, cmd *cobra.Command) {
321-
_, _ = fmt.Fprintf(buf, "custom example for %s\n", cmd.Use)
322-
_, _ = buf.WriteString(cmd.Example)
321+
WithCustomExampleFormatter(func(w io.Writer, cmd *cobra.Command) {
322+
_, _ = fmt.Fprintf(w, "custom example for %s\n", cmd.Use)
323+
_, _ = w.Write([]byte(cmd.Example))
323324
}),
324325
},
325326
},
@@ -334,10 +335,10 @@ func TestGenDocsSnapshots(t *testing.T) {
334335
WithCustomTimeGetter(func() time.Time {
335336
return time.Date(2025, 3, 5, 17, 0, 0, 0, time.UTC)
336337
}),
337-
WithCustomExampleFormatter(func(buf *bytes.Buffer, cmd *cobra.Command) {
338-
_, _ = buf.WriteString("-- before example --\n")
339-
DefaultExampleFormatter(buf, cmd)
340-
_, _ = buf.WriteString("-- after example --\n")
338+
WithCustomExampleFormatter(func(w io.Writer, cmd *cobra.Command) {
339+
_, _ = w.Write([]byte("-- before example --\n"))
340+
DefaultExampleFormatter(w, cmd)
341+
_, _ = w.Write([]byte("-- after example --\n"))
341342
}),
342343
},
343344
},

examples.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package cobra2snooty
1616

1717
import (
18-
"bytes"
1918
"fmt"
19+
"io"
2020
"strings"
2121

2222
"github.com/spf13/cobra"
@@ -30,32 +30,34 @@ const (
3030
identChar = " "
3131
)
3232

33-
func DefaultExampleFormatter(buf *bytes.Buffer, cmd *cobra.Command) {
33+
type ExampleFormatter func(w io.Writer, cmd *cobra.Command)
34+
35+
func DefaultExampleFormatter(w io.Writer, cmd *cobra.Command) {
3436
if cmd.Example != "" {
35-
printExamples(buf, cmd)
37+
printExamples(w, cmd)
3638
}
3739
}
3840

39-
func WithCustomExampleFormatter(customFormatter func(buf *bytes.Buffer, cmd *cobra.Command)) func(options *GenDocsOptions) {
41+
func WithCustomExampleFormatter(customFormatter ExampleFormatter) func(options *GenDocsOptions) {
4042
return func(options *GenDocsOptions) {
4143
options.exampleFormatter = customFormatter
4244
}
4345
}
4446

45-
func printExamples(buf *bytes.Buffer, cmd *cobra.Command) {
47+
func printExamples(w io.Writer, cmd *cobra.Command) {
4648
// Create example substrings
4749
examplestrimmed := strings.TrimLeft(cmd.Example, " #")
4850
examples := strings.Split(examplestrimmed, "# ")
49-
buf.WriteString(examplesHeader)
51+
_, _ = w.Write([]byte(examplesHeader))
5052
// If it has an example, print the header, then print each in a code block.
5153
for _, example := range examples[0:] {
5254
comment := ""
5355
if strings.Contains(cmd.Example, "#") {
5456
comment = " #"
5557
}
56-
buf.WriteString(`.. code-block::
58+
_, _ = w.Write([]byte(`.. code-block::
5759
:copyable: false
58-
`)
59-
_, _ = fmt.Fprintf(buf, "\n %s%s\n", comment, indentString(example, identChar))
60+
`))
61+
_, _ = fmt.Fprintf(w, "\n %s%s\n", comment, indentString(example, identChar))
6062
}
6163
}

0 commit comments

Comments
 (0)