2022-09-16 14:55:56 +03:00
|
|
|
// Copyright 2013-2022 The Cobra Authors
|
2015-08-19 01:33:41 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2022-09-16 14:55:56 +03:00
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-08-19 01:33:41 +03:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-11-24 02:19:16 +03:00
|
|
|
package doc
|
2015-08-19 01:33:41 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-11-24 02:19:16 +03:00
|
|
|
"io"
|
2015-08-19 01:33:41 +03:00
|
|
|
"os"
|
2016-01-17 03:24:54 +03:00
|
|
|
"path/filepath"
|
2015-08-19 01:33:41 +03:00
|
|
|
"sort"
|
2018-10-21 17:11:14 +03:00
|
|
|
"strconv"
|
2015-08-19 01:33:41 +03:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-10-20 01:17:41 +03:00
|
|
|
"github.com/cpuguy83/go-md2man/v2/md2man"
|
2015-11-24 02:19:16 +03:00
|
|
|
"github.com/spf13/cobra"
|
2015-08-19 01:33:41 +03:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2016-06-07 13:50:48 +03:00
|
|
|
// GenManTree will generate a man page for this command and all descendants
|
2015-09-08 23:02:02 +03:00
|
|
|
// in the directory given. The header may be nil. This function may not work
|
2017-04-19 15:39:58 +03:00
|
|
|
// 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`
|
2015-09-08 23:02:02 +03:00
|
|
|
// it is undefined which help output will be in the file `cmd-sub-third.1`.
|
2016-01-06 14:21:23 +03:00
|
|
|
func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error {
|
2016-06-21 19:34:20 +03:00
|
|
|
return GenManTreeFromOpts(cmd, GenManTreeOptions{
|
|
|
|
Header: header,
|
|
|
|
Path: dir,
|
2016-09-04 14:42:13 +03:00
|
|
|
CommandSeparator: "-",
|
2016-06-21 19:34:20 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenManTreeFromOpts generates a man page for the command and all descendants.
|
|
|
|
// The pages are written to the opts.Path directory.
|
|
|
|
func GenManTreeFromOpts(cmd *cobra.Command, opts GenManTreeOptions) error {
|
|
|
|
header := opts.Header
|
2015-09-08 23:02:02 +03:00
|
|
|
if header == nil {
|
|
|
|
header = &GenManHeader{}
|
|
|
|
}
|
2015-08-19 01:33:41 +03:00
|
|
|
for _, c := range cmd.Commands() {
|
2017-03-09 18:37:15 +03:00
|
|
|
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
2015-08-19 01:33:41 +03:00
|
|
|
continue
|
|
|
|
}
|
2016-06-21 19:34:20 +03:00
|
|
|
if err := GenManTreeFromOpts(c, opts); err != nil {
|
2016-01-06 14:21:23 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
2016-06-21 19:25:26 +03:00
|
|
|
section := "1"
|
|
|
|
if header.Section != "" {
|
|
|
|
section = header.Section
|
|
|
|
}
|
2015-11-21 16:18:37 +03:00
|
|
|
|
2016-06-21 19:34:20 +03:00
|
|
|
separator := "_"
|
|
|
|
if opts.CommandSeparator != "" {
|
|
|
|
separator = opts.CommandSeparator
|
|
|
|
}
|
2022-04-23 22:27:28 +03:00
|
|
|
|
|
|
|
replacer := strings.NewReplacer(
|
|
|
|
" ", separator,
|
|
|
|
"/", separator,
|
|
|
|
)
|
|
|
|
|
|
|
|
basename := replacer.Replace(cmd.CommandPath())
|
2016-08-20 10:09:46 +03:00
|
|
|
filename := filepath.Join(opts.Path, basename+"."+section)
|
2016-01-06 14:21:23 +03:00
|
|
|
f, err := os.Create(filename)
|
2015-08-19 01:33:41 +03:00
|
|
|
if err != nil {
|
2016-01-06 14:21:23 +03:00
|
|
|
return err
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
2016-01-06 14:21:23 +03:00
|
|
|
defer f.Close()
|
|
|
|
|
2016-06-21 19:25:26 +03:00
|
|
|
headerCopy := *header
|
|
|
|
return GenMan(cmd, &headerCopy, f)
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
|
2017-05-09 12:14:48 +03:00
|
|
|
// GenManTreeOptions is the options for generating the man pages.
|
|
|
|
// Used only in GenManTreeFromOpts.
|
2016-06-21 19:34:20 +03:00
|
|
|
type GenManTreeOptions struct {
|
|
|
|
Header *GenManHeader
|
|
|
|
Path string
|
|
|
|
CommandSeparator string
|
|
|
|
}
|
|
|
|
|
2015-09-08 23:02:02 +03:00
|
|
|
// GenManHeader is a lot like the .TH header at the start of man pages. These
|
|
|
|
// include the title, section, date, source, and manual. We will use the
|
2018-08-20 20:45:24 +03:00
|
|
|
// current time if Date is unset and will use "Auto generated by spf13/cobra"
|
2015-09-08 23:02:02 +03:00
|
|
|
// if the Source is unset.
|
|
|
|
type GenManHeader struct {
|
|
|
|
Title string
|
|
|
|
Section string
|
|
|
|
Date *time.Time
|
|
|
|
date string
|
|
|
|
Source string
|
|
|
|
Manual string
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
|
2016-01-06 14:21:23 +03:00
|
|
|
// GenMan will generate a man page for the given command and write it to
|
|
|
|
// w. The header argument may be nil, however obviously w may not.
|
|
|
|
func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {
|
2015-09-08 23:02:02 +03:00
|
|
|
if header == nil {
|
|
|
|
header = &GenManHeader{}
|
|
|
|
}
|
2020-04-29 20:15:55 +03:00
|
|
|
if err := fillHeader(header, cmd.CommandPath(), cmd.DisableAutoGenTag); err != nil {
|
2018-10-21 17:11:14 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-06-21 19:25:26 +03:00
|
|
|
|
2016-01-06 14:21:23 +03:00
|
|
|
b := genMan(cmd, header)
|
2017-04-24 21:40:15 +03:00
|
|
|
_, err := w.Write(md2man.Render(b))
|
2016-01-06 14:21:23 +03:00
|
|
|
return err
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
|
2020-04-29 20:15:55 +03:00
|
|
|
func fillHeader(header *GenManHeader, name string, disableAutoGen bool) error {
|
2015-09-08 23:02:02 +03:00
|
|
|
if header.Title == "" {
|
2022-05-14 23:10:36 +03:00
|
|
|
header.Title = strings.ToUpper(strings.ReplaceAll(name, " ", "\\-"))
|
2015-09-08 23:02:02 +03:00
|
|
|
}
|
|
|
|
if header.Section == "" {
|
|
|
|
header.Section = "1"
|
|
|
|
}
|
|
|
|
if header.Date == nil {
|
|
|
|
now := time.Now()
|
2018-10-21 17:11:14 +03:00
|
|
|
if epoch := os.Getenv("SOURCE_DATE_EPOCH"); epoch != "" {
|
|
|
|
unixEpoch, err := strconv.ParseInt(epoch, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid SOURCE_DATE_EPOCH: %v", err)
|
|
|
|
}
|
|
|
|
now = time.Unix(unixEpoch, 0)
|
|
|
|
}
|
2015-09-08 23:02:02 +03:00
|
|
|
header.Date = &now
|
|
|
|
}
|
|
|
|
header.date = (*header.Date).Format("Jan 2006")
|
2020-04-29 20:15:55 +03:00
|
|
|
if header.Source == "" && !disableAutoGen {
|
2015-09-08 23:02:02 +03:00
|
|
|
header.Source = "Auto generated by spf13/cobra"
|
|
|
|
}
|
2018-10-21 17:11:14 +03:00
|
|
|
return nil
|
2015-09-08 23:02:02 +03:00
|
|
|
}
|
|
|
|
|
2021-02-08 03:08:50 +03:00
|
|
|
func manPreamble(buf io.StringWriter, header *GenManHeader, cmd *cobra.Command, dashedName string) {
|
2016-06-21 00:27:41 +03:00
|
|
|
description := cmd.Long
|
|
|
|
if len(description) == 0 {
|
|
|
|
description = cmd.Short
|
|
|
|
}
|
|
|
|
|
2021-02-08 03:08:50 +03:00
|
|
|
cobra.WriteStringAndCheck(buf, fmt.Sprintf(`%% "%s" "%s" "%s" "%s" "%s"
|
2015-08-19 01:33:41 +03:00
|
|
|
# NAME
|
2017-04-24 21:40:15 +03:00
|
|
|
`, header.Title, header.Section, header.date, header.Source, header.Manual))
|
2021-02-08 03:08:50 +03:00
|
|
|
cobra.WriteStringAndCheck(buf, fmt.Sprintf("%s \\- %s\n\n", dashedName, cmd.Short))
|
|
|
|
cobra.WriteStringAndCheck(buf, "# SYNOPSIS\n")
|
|
|
|
cobra.WriteStringAndCheck(buf, fmt.Sprintf("**%s**\n\n", cmd.UseLine()))
|
|
|
|
cobra.WriteStringAndCheck(buf, "# DESCRIPTION\n")
|
|
|
|
cobra.WriteStringAndCheck(buf, description+"\n\n")
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
|
2021-02-08 03:08:50 +03:00
|
|
|
func manPrintFlags(buf io.StringWriter, flags *pflag.FlagSet) {
|
2015-08-19 01:33:41 +03:00
|
|
|
flags.VisitAll(func(flag *pflag.Flag) {
|
2015-09-09 05:43:49 +03:00
|
|
|
if len(flag.Deprecated) > 0 || flag.Hidden {
|
2015-08-19 01:33:41 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
format := ""
|
2016-06-21 00:29:54 +03:00
|
|
|
if len(flag.Shorthand) > 0 && len(flag.ShorthandDeprecated) == 0 {
|
|
|
|
format = fmt.Sprintf("**-%s**, **--%s**", flag.Shorthand, flag.Name)
|
2015-08-19 01:33:41 +03:00
|
|
|
} else {
|
2016-06-21 00:29:54 +03:00
|
|
|
format = fmt.Sprintf("**--%s**", flag.Name)
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
if len(flag.NoOptDefVal) > 0 {
|
2017-04-24 21:40:15 +03:00
|
|
|
format += "["
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
if flag.Value.Type() == "string" {
|
|
|
|
// put quotes on the value
|
2017-04-24 21:40:15 +03:00
|
|
|
format += "=%q"
|
2015-08-19 01:33:41 +03:00
|
|
|
} else {
|
2017-04-24 21:40:15 +03:00
|
|
|
format += "=%s"
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
if len(flag.NoOptDefVal) > 0 {
|
2017-04-24 21:40:15 +03:00
|
|
|
format += "]"
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
2017-04-24 21:40:15 +03:00
|
|
|
format += "\n\t%s\n\n"
|
2021-02-08 03:08:50 +03:00
|
|
|
cobra.WriteStringAndCheck(buf, fmt.Sprintf(format, flag.DefValue, flag.Usage))
|
2015-08-19 01:33:41 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-08 03:08:50 +03:00
|
|
|
func manPrintOptions(buf io.StringWriter, command *cobra.Command) {
|
2015-08-19 01:33:41 +03:00
|
|
|
flags := command.NonInheritedFlags()
|
2018-04-24 19:15:12 +03:00
|
|
|
if flags.HasAvailableFlags() {
|
2021-02-08 03:08:50 +03:00
|
|
|
cobra.WriteStringAndCheck(buf, "# OPTIONS\n")
|
2017-04-24 21:40:15 +03:00
|
|
|
manPrintFlags(buf, flags)
|
2021-02-08 03:08:50 +03:00
|
|
|
cobra.WriteStringAndCheck(buf, "\n")
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
flags = command.InheritedFlags()
|
2018-04-24 19:15:12 +03:00
|
|
|
if flags.HasAvailableFlags() {
|
2021-02-08 03:08:50 +03:00
|
|
|
cobra.WriteStringAndCheck(buf, "# OPTIONS INHERITED FROM PARENT COMMANDS\n")
|
2017-04-24 21:40:15 +03:00
|
|
|
manPrintFlags(buf, flags)
|
2021-02-08 03:08:50 +03:00
|
|
|
cobra.WriteStringAndCheck(buf, "\n")
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 02:19:16 +03:00
|
|
|
func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
|
2017-05-20 20:28:06 +03:00
|
|
|
cmd.InitDefaultHelpCmd()
|
|
|
|
cmd.InitDefaultHelpFlag()
|
|
|
|
|
2022-04-23 22:27:28 +03:00
|
|
|
// note that genMan doesn't accept custom separator defined in GenManTreeOptions
|
|
|
|
replacer := strings.NewReplacer(
|
|
|
|
" ", "-",
|
|
|
|
"/", "-",
|
|
|
|
)
|
|
|
|
|
2015-08-19 01:33:41 +03:00
|
|
|
// something like `rootcmd-subcmd1-subcmd2`
|
2022-04-23 22:27:28 +03:00
|
|
|
dashCommandName := replacer.Replace(cmd.CommandPath())
|
2015-08-19 01:33:41 +03:00
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
2016-06-21 00:27:41 +03:00
|
|
|
manPreamble(buf, header, cmd, dashCommandName)
|
2015-08-19 01:33:41 +03:00
|
|
|
manPrintOptions(buf, cmd)
|
|
|
|
if len(cmd.Example) > 0 {
|
2017-04-24 21:40:15 +03:00
|
|
|
buf.WriteString("# EXAMPLE\n")
|
|
|
|
buf.WriteString(fmt.Sprintf("```\n%s\n```\n", cmd.Example))
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
2015-11-24 02:19:16 +03:00
|
|
|
if hasSeeAlso(cmd) {
|
2017-04-24 21:40:15 +03:00
|
|
|
buf.WriteString("# SEE ALSO\n")
|
2016-01-17 07:18:47 +03:00
|
|
|
seealsos := make([]string, 0)
|
2015-08-19 01:33:41 +03:00
|
|
|
if cmd.HasParent() {
|
2015-09-08 19:32:15 +03:00
|
|
|
parentPath := cmd.Parent().CommandPath()
|
2022-04-23 22:27:28 +03:00
|
|
|
|
|
|
|
dashParentPath := replacer.Replace(parentPath)
|
2016-01-17 07:18:47 +03:00
|
|
|
seealso := fmt.Sprintf("**%s(%s)**", dashParentPath, header.Section)
|
|
|
|
seealsos = append(seealsos, seealso)
|
2015-11-24 02:19:16 +03:00
|
|
|
cmd.VisitParents(func(c *cobra.Command) {
|
2015-11-08 04:21:25 +03:00
|
|
|
if c.DisableAutoGenTag {
|
|
|
|
cmd.DisableAutoGenTag = c.DisableAutoGenTag
|
|
|
|
}
|
|
|
|
})
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
|
|
|
children := cmd.Commands()
|
|
|
|
sort.Sort(byName(children))
|
2016-01-17 07:18:47 +03:00
|
|
|
for _, c := range children {
|
2017-03-09 18:37:15 +03:00
|
|
|
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
2015-08-19 01:33:41 +03:00
|
|
|
continue
|
|
|
|
}
|
2022-04-23 22:27:28 +03:00
|
|
|
seealso := fmt.Sprintf("**%s-%s(%s)**", dashCommandName, replacer.Replace(c.Name()), header.Section)
|
2016-01-17 07:18:47 +03:00
|
|
|
seealsos = append(seealsos, seealso)
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
2017-04-24 21:40:15 +03:00
|
|
|
buf.WriteString(strings.Join(seealsos, ", ") + "\n")
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
2015-11-08 04:21:25 +03:00
|
|
|
if !cmd.DisableAutoGenTag {
|
2017-04-24 21:40:15 +03:00
|
|
|
buf.WriteString(fmt.Sprintf("# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006")))
|
2015-11-08 04:21:25 +03:00
|
|
|
}
|
2015-08-19 01:33:41 +03:00
|
|
|
return buf.Bytes()
|
|
|
|
}
|