Skip to content

Commit

Permalink
Merge pull request #116 from barrettj12/flush-index
Browse files Browse the repository at this point in the history
fix: correctly write index in documentation command
  • Loading branch information
barrettj12 authored Aug 7, 2024
2 parents cc2574f + 65c6e80 commit 687aaf5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
26 changes: 16 additions & 10 deletions documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ func (c *documentationCommand) dumpSeveralFiles() error {
return err
}

writer := bufio.NewWriter(f)
_, err = writer.WriteString(c.commandsIndex())
err = c.writeIndex(f)
if err != nil {
return err
return fmt.Errorf("writing index: %w", err)
}
f.Close()
}
Expand Down Expand Up @@ -271,9 +270,9 @@ func (c *documentationCommand) dumpEntries(w io.Writer) error {
}

if !c.noIndex {
_, err := fmt.Fprintf(w, "%s", c.commandsIndex())
err := c.writeIndex(w)
if err != nil {
return err
return fmt.Errorf("writing index: %w", err)
}
}

Expand Down Expand Up @@ -310,19 +309,26 @@ func (c *documentationCommand) writeSections(w io.Writer, superCommands []string
return nil
}

func (c *documentationCommand) commandsIndex() string {
index := "# Index\n"
// writeIndex writes the command index to the specified writer.
func (c *documentationCommand) writeIndex(w io.Writer) error {
_, err := fmt.Fprintf(w, "# Index\n")
if err != nil {
return err
}

listCommands := c.getSortedListCommands()
for id, name := range listCommands {
if isDefaultCommand(name) {
continue
}
index += fmt.Sprintf("%d. [%s](%s)\n", id, name, c.linkForCommand(name))
_, err = fmt.Fprintf(w, "%d. [%s](%s)\n", id, name, c.linkForCommand(name))
if err != nil {
return err
}
// TODO: handle subcommands ??
}
index += "---\n\n"
return index
_, err = fmt.Fprintf(w, "---\n\n")
return err
}

// Return the URL/location for the given command
Expand Down
23 changes: 23 additions & 0 deletions documentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd_test

import (
"fmt"
"os"
"path/filepath"

"github.com/juju/gnuflag"
gc "gopkg.in/check.v1"
Expand Down Expand Up @@ -209,3 +211,24 @@ formatted YAML-formatted file.
c.Check(cmd.EscapeMarkdown(t.input), gc.Equals, t.output)
}
}

// TestWriteIndex checks that the index file is successfully written.
func (*documentationSuite) TestWriteIndex(c *gc.C) {
// Make temp dir to hold docs
docsDir := c.MkDir()

// Create a supercommand and run the documentation command
superCmd := cmd.NewSuperCommand(cmd.SuperCommandParams{})
superCmd.SetFlags(&gnuflag.FlagSet{})
err := superCmd.Init([]string{"documentation", "--split", "--out", docsDir})
c.Assert(err, gc.IsNil)
err = superCmd.Run(&cmd.Context{})
c.Assert(err, gc.IsNil)

// Check the index file
indexPath := filepath.Join(docsDir, "index.md")
indexContents, err := os.ReadFile(indexPath)
c.Assert(err, gc.IsNil)
// Index should be non-empty
c.Assert(string(indexContents), gc.Matches, "(?m).*Index.*")
}

0 comments on commit 687aaf5

Please sign in to comment.