From 92195c4337cba191c4454854c15aa7087af536f7 Mon Sep 17 00:00:00 2001 From: Simon Richardson Date: Mon, 14 Aug 2023 10:04:40 +0100 Subject: [PATCH] Render all documentation The following changes the documentation command to use io.Writer instead of the bufio.Writer. This means that we can then just use the stdout for writing the documentation output. As the bufio buffer is actually rather small, only 4096 bytes long. We clearly don't blow past that during the individual files, but we do when dumping out the documentation. --- documentation.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/documentation.go b/documentation.go index caab9cb7..a9cf5ddb 100644 --- a/documentation.go +++ b/documentation.go @@ -7,6 +7,7 @@ import ( "bufio" "errors" "fmt" + "io" "os" "path/filepath" "sort" @@ -102,7 +103,7 @@ func (c *documentationCommand) Run(ctx *Context) error { // dumpOneFile is invoked when the output is contained in a single output func (c *documentationCommand) dumpOneFile(ctx *Context) error { - var writer *bufio.Writer + var writer io.Writer if c.out != "" { _, err := os.Stat(c.out) if err != nil { @@ -116,9 +117,9 @@ func (c *documentationCommand) dumpOneFile(ctx *Context) error { return err } defer f.Close() - writer = bufio.NewWriter(f) + writer = f } else { - writer = bufio.NewWriter(ctx.Stdout) + writer = ctx.Stdout } return c.dumpEntries(writer) @@ -251,24 +252,24 @@ func (c *documentationCommand) readFileIds(path string) (map[string]string, erro return ids, nil } -func (c *documentationCommand) dumpEntries(writer *bufio.Writer) error { +func (c *documentationCommand) dumpEntries(w io.Writer) error { if len(c.super.subcmds) == 0 { fmt.Printf("No commands found for %s", c.super.Name) return nil } if !c.noIndex { - _, err := writer.WriteString(c.commandsIndex()) + _, err := fmt.Fprintf(w, "%s", c.commandsIndex()) if err != nil { return err } } - return c.writeSections(writer, []string{c.super.Name}, true) + return c.writeSections(w, []string{c.super.Name}, true) } // writeSections (recursively) writes sections for all commands to the given file. -func (c *documentationCommand) writeSections(writer *bufio.Writer, superCommands []string, printDefaultCommands bool) error { +func (c *documentationCommand) writeSections(w io.Writer, superCommands []string, printDefaultCommands bool) error { sorted := c.getSortedListCommands() for _, name := range sorted { if !printDefaultCommands && isDefaultCommand(name) { @@ -276,14 +277,14 @@ func (c *documentationCommand) writeSections(writer *bufio.Writer, superCommands } ref := c.super.subcmds[name] commandSeq := append(superCommands, name) - _, err := writer.WriteString(c.formatCommand(ref, true, commandSeq)) + _, err := fmt.Fprintf(w, "%s", c.formatCommand(ref, true, commandSeq)) if err != nil { return err } // Handle subcommands if sc, ok := ref.command.(*SuperCommand); ok { - err = sc.documentation.writeSections(writer, commandSeq, false) + err = sc.documentation.writeSections(w, commandSeq, false) if err != nil { return err }