Skip to content
Myr

Use the correct man page section for the filename

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Also make header mutation cleaner.

authored by Daniel Nephin · committed by Daniel Nephin · parent 112c7dc · Browse files · 2 files+36 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Daniel Nephin <dnephin@gmail.com>

Diff stats

2 files+36 added−14 removed

  • other:+36 added−14 removed

2 files+36 added−14 removed

modified:doc/man_docs_test.go+26 added
2 unmodified lines
3import (4 "bufio"5 "bytes"6 "fmt"7 "io/ioutil"8 "os"9 "path/filepath"10 "strings"11 "testing"1213 "github.com/spf13/cobra"
131 unmodified lines
145 t.Fatalf("Expected %s, but got %s", expected, out.String())146 }147}148149func TestGenManTree(t *testing.T) {150 cmd := &cobra.Command{151 Use: "do [OPTIONS] arg1 arg2",152 }153 header := &GenManHeader{Section: "2"}154 tmpdir, err := ioutil.TempDir("", "test-gen-man-tree")155 if err != nil {156 t.Fatalf("Failed to create tempdir: %s", err.Error())157 }158 defer os.RemoveAll(tmpdir)159160 if err := GenManTree(cmd, header, tmpdir); err != nil {161 t.Fatalf("GenManTree failed: %s", err.Error())162 }163164 if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil {165 t.Fatalf("Expected file 'do.2' to exist")166 }167168 if header.Title != "" {169 t.Fatalf("Expected header.Title to be unmodified")170 }171}172173func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {174 for scanner.Scan() {175 line := scanner.Text()176 if line == expectedLine {
26 unmodified lines
modified:doc/man_docs.go+10 added-14 removed
43 unmodified lines
44 if err := GenManTree(c, header, dir); err != nil {45 return err46 }47 }48 needToResetTitle := header.Title == ""48 section := "1"49 if header.Section != "" {50 section = header.Section51 }5250 basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".1"53 basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + "." + section54 filename := filepath.Join(dir, basename)55 f, err := os.Create(filename)56 if err != nil {57 return err58 }59 defer f.Close()6058 if err := GenMan(cmd, header, f); err != nil {59 return err60 }6162 if needToResetTitle {63 header.Title = ""64 }65 return nil61 headerCopy := *header62 return GenMan(cmd, &headerCopy, f)63}6465// GenManHeader is a lot like the .TH header at the start of man pages. These66// include the title, section, date, source, and manual. We will use the
13 unmodified lines
80func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {81 if header == nil {82 header = &GenManHeader{}83 }84 fillHeader(header, cmd.CommandPath())8586 b := genMan(cmd, header)88 final := mangen.Render(b)89 _, err := w.Write(final)87 _, err := w.Write(mangen.Render(b))88 return err89}9091func fillHeader(header *GenManHeader, name string) {
79 unmodified lines
171 commandName := cmd.CommandPath()172 // something like `rootcmd-subcmd1-subcmd2`173 dashCommandName := strings.Replace(commandName, " ", "-", -1)174177 fillHeader(header, commandName)178175 buf := new(bytes.Buffer)176177 manPreamble(buf, header, cmd, dashCommandName)178 manPrintOptions(buf, cmd)
34 unmodified lines