From 97206b3170fb7e2e5c28333eab815dcd6d47d369 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 21 Jun 2016 12:25:26 -0400 Subject: [PATCH] Use the correct man page section for the filename Also make header mutation cleaner. Signed-off-by: Daniel Nephin --- doc/man_docs.go | 24 ++++++++++-------------- doc/man_docs_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/doc/man_docs.go b/doc/man_docs.go index e990b3f..ce4404d 100644 --- a/doc/man_docs.go +++ b/doc/man_docs.go @@ -45,9 +45,12 @@ func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error { return err } } - needToResetTitle := header.Title == "" + section := "1" + if header.Section != "" { + section = header.Section + } - basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".1" + basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + "." + section filename := filepath.Join(dir, basename) f, err := os.Create(filename) if err != nil { @@ -55,14 +58,8 @@ func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error { } defer f.Close() - if err := GenMan(cmd, header, f); err != nil { - return err - } - - if needToResetTitle { - header.Title = "" - } - return nil + headerCopy := *header + return GenMan(cmd, &headerCopy, f) } // GenManHeader is a lot like the .TH header at the start of man pages. These @@ -84,9 +81,10 @@ func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error { if header == nil { header = &GenManHeader{} } + fillHeader(header, cmd.CommandPath()) + b := genMan(cmd, header) - final := mangen.Render(b) - _, err := w.Write(final) + _, err := w.Write(mangen.Render(b)) return err } @@ -174,8 +172,6 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte { // something like `rootcmd-subcmd1-subcmd2` dashCommandName := strings.Replace(commandName, " ", "-", -1) - fillHeader(header, commandName) - buf := new(bytes.Buffer) manPreamble(buf, header, cmd, dashCommandName) diff --git a/doc/man_docs_test.go b/doc/man_docs_test.go index 3fd195f..26b8fcc 100644 --- a/doc/man_docs_test.go +++ b/doc/man_docs_test.go @@ -4,7 +4,9 @@ import ( "bufio" "bytes" "fmt" + "io/ioutil" "os" + "path/filepath" "strings" "testing" @@ -144,6 +146,30 @@ func TestManPrintFlagsHidesShortDeperecated(t *testing.T) { } } +func TestGenManTree(t *testing.T) { + cmd := &cobra.Command{ + Use: "do [OPTIONS] arg1 arg2", + } + header := &GenManHeader{Section: "2"} + tmpdir, err := ioutil.TempDir("", "test-gen-man-tree") + if err != nil { + t.Fatalf("Failed to create tempdir: %s", err.Error()) + } + defer os.RemoveAll(tmpdir) + + if err := GenManTree(cmd, header, tmpdir); err != nil { + t.Fatalf("GenManTree failed: %s", err.Error()) + } + + if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil { + t.Fatalf("Expected file 'do.2' to exist") + } + + if header.Title != "" { + t.Fatalf("Expected header.Title to be unmodified") + } +} + func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error { for scanner.Scan() { line := scanner.Text()