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.
This commit is contained in:
parent
eb5040e69e
commit
5df1341f93
@ -32,7 +32,7 @@ import (
|
|||||||
// correctly if your command names have - in them. If you have `cmd` with two
|
// 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`
|
// 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`.
|
// 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 {
|
if header == nil {
|
||||||
header = &GenManHeader{}
|
header = &GenManHeader{}
|
||||||
}
|
}
|
||||||
@ -40,31 +40,28 @@ func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) {
|
|||||||
if !c.IsAvailableCommand() || c.IsHelpCommand() {
|
if !c.IsAvailableCommand() || c.IsHelpCommand() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
GenManTree(c, header, dir)
|
if err := GenManTree(c, header, dir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
out := new(bytes.Buffer)
|
|
||||||
|
|
||||||
needToResetTitle := header.Title == ""
|
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 {
|
if needToResetTitle {
|
||||||
header.Title = ""
|
header.Title = ""
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenManHeader is a lot like the .TH header at the start of man pages. These
|
// GenManHeader is a lot like the .TH header at the start of man pages. These
|
||||||
@ -80,15 +77,16 @@ type GenManHeader struct {
|
|||||||
Manual string
|
Manual string
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenMan will generate a man page for the given command in the out buffer.
|
// GenMan will generate a man page for the given command and write it to
|
||||||
// The header argument may be nil, however obviously out may not.
|
// w. The header argument may be nil, however obviously w may not.
|
||||||
func GenMan(cmd *cobra.Command, header *GenManHeader, out io.Writer) {
|
func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {
|
||||||
if header == nil {
|
if header == nil {
|
||||||
header = &GenManHeader{}
|
header = &GenManHeader{}
|
||||||
}
|
}
|
||||||
buf := genMan(cmd, header)
|
b := genMan(cmd, header)
|
||||||
final := mangen.Render(buf)
|
final := mangen.Render(b)
|
||||||
out.Write(final)
|
_, err := w.Write(final)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func fillHeader(header *GenManHeader, name string) {
|
func fillHeader(header *GenManHeader, name string) {
|
||||||
|
@ -29,7 +29,9 @@ func TestGenManDoc(t *testing.T) {
|
|||||||
Section: "2",
|
Section: "2",
|
||||||
}
|
}
|
||||||
// We generate on a subcommand so we have both subcommands and parents
|
// 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()
|
found := out.String()
|
||||||
|
|
||||||
// Make sure parent has - in CommandPath() in SEE ALSO:
|
// Make sure parent has - in CommandPath() in SEE ALSO:
|
||||||
@ -85,7 +87,9 @@ func TestGenManNoGenTag(t *testing.T) {
|
|||||||
Section: "2",
|
Section: "2",
|
||||||
}
|
}
|
||||||
// We generate on a subcommand so we have both subcommands and parents
|
// 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()
|
found := out.String()
|
||||||
|
|
||||||
unexpected := translate("#HISTORY")
|
unexpected := translate("#HISTORY")
|
||||||
|
Loading…
Reference in New Issue
Block a user