Merge pull request #218 from mvdan/error-checking
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Error checking for doc generation
authored by Eric Paris · committed by Eric Paris · parents c82a0ce, 5df1341 · Browse files · 6 files·+111 added−74 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+111 added−74 removed
- other:+111 added−74 removed
Provenance
- Commit
a3a9f34- Tree
3565694- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
c82a0ce- Parent 2
5df1341
6 files changed+111 added−74 removed
modified:doc/cmd_test.go+1 added-1 removed
9 unmodified lines10 "github.com/spf13/cobra"11 )12 13 var flagb1, flagb2, flagb3, flagbr, flagbp bool14-var flags1, flags2a, flags2b, flags3, outs string14+var flags1, flags2a, flags2b, flags3 string15 var flagi1, flagi2, flagi3, flagir int16 17 const strtwoParentHelp = "help message for parent flag strtwo"18 const strtwoChildHelp = "help message for child flag strtwo"127 unmodified linesmodified:doc/man_docs_test.go+6 added-2 removed
27 unmodified lines28 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()36 37 // Make sure parent has - in CommandPath() in SEE ALSO:38 parentPath := cmdEcho.Parent().CommandPath()47 unmodified lines86 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()94 95 unexpected := translate("#HISTORY")96 checkStringOmits(t, found, unexpected)1 unmodified linemodified:doc/man_docs.go+22 added-24 removed
30 unmodified lines31 // 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`.35-func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) {35+func 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)46-47 needToResetTitle := header.Title == ""48 49- GenMan(cmd, header, out)50-51- if needToResetTitle {52- header.Title = ""53- }54-49 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()56+57+ if err := GenMan(cmd, header, f); err != nil {58+ return err59 }60+61+ if needToResetTitle {62+ header.Title = ""63+ }64+ return nil65 }66 67 // 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 the7 unmodified lines76 Source string77 Manual string78 }79 83-// 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.85-func 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.82+func 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 }91 92 func fillHeader(header *GenManHeader, name string) {93 if header.Title == "" {124 unmodified linesmodified:doc/md_docs_test.go+6 added-2 removed
19 unmodified lines20 21 out := new(bytes.Buffer)22 23 // We generate on s subcommand so we have both subcommands and parents24- GenMarkdown(cmdEcho, out)24+ if err := GenMarkdown(cmdEcho, out); err != nil {25+ t.Fatal(err)26+ }27 found := out.String()28 29 // Our description30 expected := cmdEcho.Long45 unmodified lines76 c.DisableAutoGenTag = true77 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)78 out := new(bytes.Buffer)79 78- GenMarkdown(c, out)80+ if err := GenMarkdown(c, out); err != nil {81+ t.Fatal(err)82+ }83 found := out.String()84 85 unexpected := "Auto generated"86 checkStringOmits(t, found, unexpected)2 unmodified linesmodified:doc/md_docs.go+74 added-43 removed
12 unmodified lines13 14 package doc15 16 import (17- "bytes"17 "fmt"18 "io"19 "os"20 "sort"2 unmodified lines23 24 "github.com/spf13/cobra"25 )26 28-func printOptions(out io.Writer, cmd *cobra.Command, name string) {27+func printOptions(w io.Writer, cmd *cobra.Command, name string) error {28 flags := cmd.NonInheritedFlags()30- flags.SetOutput(out)29+ flags.SetOutput(w)30 if flags.HasFlags() {32- fmt.Fprintf(out, "### Options\n\n```\n")31+ if _, err := fmt.Fprintf(w, "### Options\n\n```\n"); err != nil {32+ return err33+ }34 flags.PrintDefaults()34- fmt.Fprintf(out, "```\n\n")35+ if _, err := fmt.Fprintf(w, "```\n\n"); err != nil {36+ return err37+ }38 }39 40 parentFlags := cmd.InheritedFlags()38- parentFlags.SetOutput(out)41+ parentFlags.SetOutput(w)42 if parentFlags.HasFlags() {40- fmt.Fprintf(out, "### Options inherited from parent commands\n\n```\n")43+ if _, err := fmt.Fprintf(w, "### Options inherited from parent commands\n\n```\n"); err != nil {44+ return err45+ }46 parentFlags.PrintDefaults()42- fmt.Fprintf(out, "```\n\n")47+ if _, err := fmt.Fprintf(w, "```\n\n"); err != nil {48+ return err49+ }50 }51+ return nil52 }53 46-func GenMarkdown(cmd *cobra.Command, out io.Writer) {47- GenMarkdownCustom(cmd, out, func(s string) string { return s })54+func GenMarkdown(cmd *cobra.Command, w io.Writer) error {55+ return GenMarkdownCustom(cmd, w, func(s string) string { return s })56 }57 50-func GenMarkdownCustom(cmd *cobra.Command, out io.Writer, linkHandler func(string) string) {58+func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {59 name := cmd.CommandPath()60 61 short := cmd.Short62 long := cmd.Long63 if len(long) == 0 {64 long = short65 }66 59- fmt.Fprintf(out, "## %s\n\n", name)60- fmt.Fprintf(out, "%s\n\n", short)61- fmt.Fprintf(out, "### Synopsis\n\n")62- fmt.Fprintf(out, "\n%s\n\n", long)67+ if _, err := fmt.Fprintf(w, "## %s\n\n", name); err != nil {68+ return err69+ }70+ if _, err := fmt.Fprintf(w, "%s\n\n", short); err != nil {71+ return err72+ }73+ if _, err := fmt.Fprintf(w, "### Synopsis\n\n"); err != nil {74+ return err75+ }76+ if _, err := fmt.Fprintf(w, "\n%s\n\n", long); err != nil {77+ return err78+ }79 80 if cmd.Runnable() {65- fmt.Fprintf(out, "```\n%s\n```\n\n", cmd.UseLine())81+ if _, err := fmt.Fprintf(w, "```\n%s\n```\n\n", cmd.UseLine()); err != nil {82+ return err83+ }84 }85 86 if len(cmd.Example) > 0 {69- fmt.Fprintf(out, "### Examples\n\n")70- fmt.Fprintf(out, "```\n%s\n```\n\n", cmd.Example)87+ if _, err := fmt.Fprintf(w, "### Examples\n\n"); err != nil {88+ return err89+ }90+ if _, err := fmt.Fprintf(w, "```\n%s\n```\n\n", cmd.Example); err != nil {91+ return err92+ }93 }94 73- printOptions(out, cmd, name)95+ if err := printOptions(w, cmd, name); err != nil {96+ return err97+ }98 if hasSeeAlso(cmd) {75- fmt.Fprintf(out, "### SEE ALSO\n")99+ if _, err := fmt.Fprintf(w, "### SEE ALSO\n"); err != nil {100+ return err101+ }102 if cmd.HasParent() {103 parent := cmd.Parent()104 pname := parent.CommandPath()105 link := pname + ".md"106 link = strings.Replace(link, " ", "_", -1)81- fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)107+ if _, err := fmt.Fprintf(w, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short); err != nil {108+ return err109+ }110 cmd.VisitParents(func(c *cobra.Command) {111 if c.DisableAutoGenTag {112 cmd.DisableAutoGenTag = c.DisableAutoGenTag113 }9 unmodified lines123 }124 cname := name + " " + child.Name()125 link := cname + ".md"126 link = strings.Replace(link, " ", "_", -1)99- fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)127+ if _, err := fmt.Fprintf(w, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short); err != nil {128+ return err129+ }130 }101- fmt.Fprintf(out, "\n")131+ if _, err := fmt.Fprintf(w, "\n"); err != nil {132+ return err133+ }134 }135 if !cmd.DisableAutoGenTag {104- fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006"))136+ if _, err := fmt.Fprintf(w, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006")); err != nil {137+ return err138+ }139 }140+ return nil141 }142 108-func GenMarkdownTree(cmd *cobra.Command, dir string) {143+func GenMarkdownTree(cmd *cobra.Command, dir string) error {144 identity := func(s string) string { return s }145 emptyStr := func(s string) string { return "" }111- GenMarkdownTreeCustom(cmd, dir, emptyStr, identity)146+ return GenMarkdownTreeCustom(cmd, dir, emptyStr, identity)147 }148 114-func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender func(string) string, linkHandler func(string) string) {149+func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {150 for _, c := range cmd.Commands() {151 if !c.IsAvailableCommand() || c.IsHelpCommand() {152 continue153 }119- GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler)154+ if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {155+ return err156+ }157 }121- out := new(bytes.Buffer)158 123- GenMarkdownCustom(cmd, out, linkHandler)124-159 filename := cmd.CommandPath()160 filename = dir + strings.Replace(filename, " ", "_", -1) + ".md"127- outFile, err := os.Create(filename)161+ f, err := os.Create(filename)162 if err != nil {129- fmt.Println(err)130- os.Exit(1)163+ return err164 }132- defer outFile.Close()133- _, err = outFile.WriteString(filePrepender(filename))134- if err != nil {135- fmt.Println(err)136- os.Exit(1)165+ defer f.Close()166+167+ if _, err := io.WriteString(f, filePrepender(filename)); err != nil {168+ return err169 }138- _, err = outFile.Write(out.Bytes())139- if err != nil {140- fmt.Println(err)141- os.Exit(1)170+ if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {171+ return err172 }173+ return nil174 }modified:doc/md_docs.md+2 added-2 removed
37 unmodified lines383940414242434445464748484950515229 unmodified lines37 unmodified lines
Both `GenMarkdown` and `GenMarkdownTree` have alternate versions with callbacks to get some control of the output:
```gofunc GenMarkdownTreeCustom(cmd *Command, dir string, filePrepender func(string) string, linkHandler func(string) string) {func GenMarkdownTreeCustom(cmd *Command, dir string, filePrepender, linkHandler func(string) string) error { //...}```
```gofunc GenMarkdownCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) {func GenMarkdownCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) error { //...}```
29 unmodified lines