From 5df1341f9327a2aa23f979ab4640898687c13850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 6 Jan 2016 12:21:23 +0100 Subject: [PATCH] Treat write errors in man doc generation Just like the last commit, but now for manpages. genMan still works with a buffer and returns []byte instead of working directly with an io.Writer. This is because, in turn, md2man takes byte slices instead of readers and writers. Wrapping genMan around a writer is unnecessary especially since it's not an exported function, and also because we'd still need a buffer to get the output bytes. --- doc/man_docs.go | 48 +++++++++++++++++++++----------------------- doc/man_docs_test.go | 8 ++++++-- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/doc/man_docs.go b/doc/man_docs.go index 71e020d5..942b0aad 100644 --- a/doc/man_docs.go +++ b/doc/man_docs.go @@ -32,7 +32,7 @@ import ( // correctly if your command names have - in them. If you have `cmd` with two // subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third` // it is undefined which help output will be in the file `cmd-sub-third.1`. -func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) { +func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error { if header == nil { header = &GenManHeader{} } @@ -40,31 +40,28 @@ func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) { if !c.IsAvailableCommand() || c.IsHelpCommand() { continue } - GenManTree(c, header, dir) + if err := GenManTree(c, header, dir); err != nil { + return err + } } - out := new(bytes.Buffer) - needToResetTitle := header.Title == "" - GenMan(cmd, header, out) + filename := cmd.CommandPath() + filename = dir + strings.Replace(filename, " ", "-", -1) + ".1" + f, err := os.Create(filename) + if err != nil { + return err + } + defer f.Close() + + if err := GenMan(cmd, header, f); err != nil { + return err + } if needToResetTitle { header.Title = "" } - - filename := cmd.CommandPath() - filename = dir + strings.Replace(filename, " ", "-", -1) + ".1" - outFile, err := os.Create(filename) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - defer outFile.Close() - _, err = outFile.Write(out.Bytes()) - if err != nil { - fmt.Println(err) - os.Exit(1) - } + return nil } // GenManHeader is a lot like the .TH header at the start of man pages. These @@ -80,15 +77,16 @@ type GenManHeader struct { Manual string } -// GenMan will generate a man page for the given command in the out buffer. -// The header argument may be nil, however obviously out may not. -func GenMan(cmd *cobra.Command, header *GenManHeader, out io.Writer) { +// GenMan will generate a man page for the given command and write it to +// w. The header argument may be nil, however obviously w may not. +func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error { if header == nil { header = &GenManHeader{} } - buf := genMan(cmd, header) - final := mangen.Render(buf) - out.Write(final) + b := genMan(cmd, header) + final := mangen.Render(b) + _, err := w.Write(final) + return err } func fillHeader(header *GenManHeader, name string) { diff --git a/doc/man_docs_test.go b/doc/man_docs_test.go index 4e8f706b..3083125a 100644 --- a/doc/man_docs_test.go +++ b/doc/man_docs_test.go @@ -29,7 +29,9 @@ func TestGenManDoc(t *testing.T) { Section: "2", } // We generate on a subcommand so we have both subcommands and parents - GenMan(cmdEcho, header, out) + if err := GenMan(cmdEcho, header, out); err != nil { + t.Fatal(err) + } found := out.String() // Make sure parent has - in CommandPath() in SEE ALSO: @@ -85,7 +87,9 @@ func TestGenManNoGenTag(t *testing.T) { Section: "2", } // We generate on a subcommand so we have both subcommands and parents - GenMan(cmdEcho, header, out) + if err := GenMan(cmdEcho, header, out); err != nil { + t.Fatal(err) + } found := out.String() unexpected := translate("#HISTORY")