Treat write errors in markdown doc generation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This code was already using io.Writer, but was completely ignoring write errors. The most worrying part is how GenMarkdownTreeCustom used an unnecessary buffer to then dump all of its contents on a file, and instead of returning an error on file creation/writing, it would just exit the entire program.
authored by Daniel Martí · committed by Daniel Martí · parent ea06b29 · Browse files · 3 files·+82 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+82 added−47 removed
- other:+82 added−47 removed
Provenance
- Commit
eb5040e- Tree
d545731- Author
Daniel Martí <mvdan@mvdan.cc>- Committer
Daniel Martí <mvdan@mvdan.cc>- Parent
ea06b29
3 files changed+82 added−47 removed
modified: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, 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, 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