Skip to content
Myr

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

6 files+111 added−74 removed

modified:doc/cmd_test.go+1 added-1 removed
9 unmodified lines
10 "github.com/spf13/cobra"11)1213var flagb1, flagb2, flagb3, flagbr, flagbp bool14var flags1, flags2a, flags2b, flags3, outs string14var flags1, flags2a, flags2b, flags3 string15var flagi1, flagi2, flagi3, flagir int1617const strtwoParentHelp = "help message for parent flag strtwo"18const strtwoChildHelp = "help message for child flag strtwo"
127 unmodified lines
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
modified:doc/md_docs_test.go+6 added-2 removed
19 unmodified lines
2021 out := new(bytes.Buffer)2223 // 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()2829 // Our description30 expected := cmdEcho.Long
45 unmodified lines
76 c.DisableAutoGenTag = true77 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)78 out := new(bytes.Buffer)7978 GenMarkdown(c, out)80 if err := GenMarkdown(c, out); err != nil {81 t.Fatal(err)82 }83 found := out.String()8485 unexpected := "Auto generated"86 checkStringOmits(t, found, unexpected)
2 unmodified lines
modified:doc/md_docs.go+74 added-43 removed
12 unmodified lines
1314package doc1516import (17 "bytes"17 "fmt"18 "io"19 "os"20 "sort"
2 unmodified lines
2324 "github.com/spf13/cobra"25)2628func printOptions(out io.Writer, cmd *cobra.Command, name string) {27func 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 }3940 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}5346func GenMarkdown(cmd *cobra.Command, out io.Writer) {47 GenMarkdownCustom(cmd, out, func(s string) string { return s })54func GenMarkdown(cmd *cobra.Command, w io.Writer) error {55 return GenMarkdownCustom(cmd, w, func(s string) string { return s })56}5750func GenMarkdownCustom(cmd *cobra.Command, out io.Writer, linkHandler func(string) string) {58func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {59 name := cmd.CommandPath()6061 short := cmd.Short62 long := cmd.Long63 if len(long) == 0 {64 long = short65 }6659 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 }7980 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 }8586 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 }9473 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 lines
123 }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}142108func GenMarkdownTree(cmd *cobra.Command, dir string) {143func 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}148114func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender func(string) string, linkHandler func(string) string) {149func 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)158123 GenMarkdownCustom(cmd, out, linkHandler)124159 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()166167 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