mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Use the correct man page section for the filename
Also make header mutation cleaner. Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
parent
112c7dca3a
commit
97206b3170
2 changed files with 36 additions and 14 deletions
|
@ -45,9 +45,12 @@ func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
needToResetTitle := header.Title == ""
|
section := "1"
|
||||||
|
if header.Section != "" {
|
||||||
|
section = header.Section
|
||||||
|
}
|
||||||
|
|
||||||
basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".1"
|
basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + "." + section
|
||||||
filename := filepath.Join(dir, basename)
|
filename := filepath.Join(dir, basename)
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -55,14 +58,8 @@ func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error {
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
if err := GenMan(cmd, header, f); err != nil {
|
headerCopy := *header
|
||||||
return err
|
return GenMan(cmd, &headerCopy, f)
|
||||||
}
|
|
||||||
|
|
||||||
if needToResetTitle {
|
|
||||||
header.Title = ""
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -84,9 +81,10 @@ func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {
|
||||||
if header == nil {
|
if header == nil {
|
||||||
header = &GenManHeader{}
|
header = &GenManHeader{}
|
||||||
}
|
}
|
||||||
|
fillHeader(header, cmd.CommandPath())
|
||||||
|
|
||||||
b := genMan(cmd, header)
|
b := genMan(cmd, header)
|
||||||
final := mangen.Render(b)
|
_, err := w.Write(mangen.Render(b))
|
||||||
_, err := w.Write(final)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,8 +172,6 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
|
||||||
// something like `rootcmd-subcmd1-subcmd2`
|
// something like `rootcmd-subcmd1-subcmd2`
|
||||||
dashCommandName := strings.Replace(commandName, " ", "-", -1)
|
dashCommandName := strings.Replace(commandName, " ", "-", -1)
|
||||||
|
|
||||||
fillHeader(header, commandName)
|
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
manPreamble(buf, header, cmd, dashCommandName)
|
manPreamble(buf, header, cmd, dashCommandName)
|
||||||
|
|
|
@ -4,7 +4,9 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -144,6 +146,30 @@ func TestManPrintFlagsHidesShortDeperecated(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenManTree(t *testing.T) {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "do [OPTIONS] arg1 arg2",
|
||||||
|
}
|
||||||
|
header := &GenManHeader{Section: "2"}
|
||||||
|
tmpdir, err := ioutil.TempDir("", "test-gen-man-tree")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create tempdir: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
|
if err := GenManTree(cmd, header, tmpdir); err != nil {
|
||||||
|
t.Fatalf("GenManTree failed: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil {
|
||||||
|
t.Fatalf("Expected file 'do.2' to exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
if header.Title != "" {
|
||||||
|
t.Fatalf("Expected header.Title to be unmodified")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {
|
func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
|
Loading…
Reference in a new issue