Skip to content
Myr

Treat write errors in man doc generation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by Daniel Martí · committed by Daniel Martí · parent eb5040e · Browse files · 2 files+28 added−26 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+28 added−26 removed

  • other:+28 added−26 removed

2 files+28 added−26 removed

modified:doc/man_docs_test.go+6 added-2 removed
27 unmodified lines
28 Title: "Project",29 Section: "2",30 }31 // We generate on a subcommand so we have both subcommands and parents32 GenMan(cmdEcho, header, out)32 if err := GenMan(cmdEcho, header, out); err != nil {33 t.Fatal(err)34 }35 found := out.String()3637 // Make sure parent has - in CommandPath() in SEE ALSO:38 parentPath := cmdEcho.Parent().CommandPath()
47 unmodified lines
86 Title: "Project",87 Section: "2",88 }89 // We generate on a subcommand so we have both subcommands and parents88 GenMan(cmdEcho, header, out)90 if err := GenMan(cmdEcho, header, out); err != nil {91 t.Fatal(err)92 }93 found := out.String()9495 unexpected := translate("#HISTORY")96 checkStringOmits(t, found, unexpected)
1 unmodified line
modified:doc/man_docs.go+22 added-24 removed
30 unmodified lines
31// in the directory given. The header may be nil. This function may not work32// correctly if your command names have - in them. If you have `cmd` with two33// subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third`34// it is undefined which help output will be in the file `cmd-sub-third.1`.35func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) {35func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error {36 if header == nil {37 header = &GenManHeader{}38 }39 for _, c := range cmd.Commands() {40 if !c.IsAvailableCommand() || c.IsHelpCommand() {41 continue42 }43 GenManTree(c, header, dir)43 if err := GenManTree(c, header, dir); err != nil {44 return err45 }46 }45 out := new(bytes.Buffer)4647 needToResetTitle := header.Title == ""4849 GenMan(cmd, header, out)5051 if needToResetTitle {52 header.Title = ""53 }5449 filename := cmd.CommandPath()50 filename = dir + strings.Replace(filename, " ", "-", -1) + ".1"57 outFile, err := os.Create(filename)51 f, err := os.Create(filename)52 if err != nil {59 fmt.Println(err)60 os.Exit(1)53 return err54 }62 defer outFile.Close()63 _, err = outFile.Write(out.Bytes())64 if err != nil {65 fmt.Println(err)66 os.Exit(1)55 defer f.Close()5657 if err := GenMan(cmd, header, f); err != nil {58 return err59 }6061 if needToResetTitle {62 header.Title = ""63 }64 return nil65}6667// GenManHeader is a lot like the .TH header at the start of man pages. These68// include the title, section, date, source, and manual. We will use the
7 unmodified lines
76 Source string77 Manual string78}7983// GenMan will generate a man page for the given command in the out buffer.84// The header argument may be nil, however obviously out may not.85func GenMan(cmd *cobra.Command, header *GenManHeader, out io.Writer) {80// GenMan will generate a man page for the given command and write it to81// w. The header argument may be nil, however obviously w may not.82func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {83 if header == nil {84 header = &GenManHeader{}85 }89 buf := genMan(cmd, header)90 final := mangen.Render(buf)91 out.Write(final)86 b := genMan(cmd, header)87 final := mangen.Render(b)88 _, err := w.Write(final)89 return err90}9192func fillHeader(header *GenManHeader, name string) {93 if header.Title == "" {
124 unmodified lines