Skip to content

Commit

Permalink
Merge pull request #104 from SimonRichardson/render-all-documentation
Browse files Browse the repository at this point in the history
#104

The following changes the documentation command to use io.Writer instead of the bufio.Writer. This means that we can 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.

### Q&A

The following should render the documentation in its entirety.

```sh
$ juju wait-for documenation
```

```sh
$ juju wait-for documentation --out ~/tmp/docs
```
  • Loading branch information
jujubot authored Aug 14, 2023
2 parents 5229d4b + 92195c4 commit ea2e6a1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -251,39 +252,39 @@ 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) {
continue
}
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
}
Expand Down

0 comments on commit ea2e6a1

Please sign in to comment.