2016-10-31 23:50:02 +08:00
|
|
|
//Copyright 2015 Red Hat Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package doc
|
|
|
|
|
|
|
|
import (
|
2017-05-11 22:39:54 +08:00
|
|
|
"bytes"
|
2016-10-31 23:50:02 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2017-05-11 22:39:54 +08:00
|
|
|
func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
|
2016-10-31 23:50:02 +08:00
|
|
|
flags := cmd.NonInheritedFlags()
|
2017-05-11 22:39:54 +08:00
|
|
|
flags.SetOutput(buf)
|
2016-10-31 23:50:02 +08:00
|
|
|
if flags.HasFlags() {
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString("### Options\n\n```\n")
|
2016-10-31 23:50:02 +08:00
|
|
|
flags.PrintDefaults()
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString("```\n\n")
|
2016-10-31 23:50:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
parentFlags := cmd.InheritedFlags()
|
2017-05-11 22:39:54 +08:00
|
|
|
parentFlags.SetOutput(buf)
|
2016-10-31 23:50:02 +08:00
|
|
|
if parentFlags.HasFlags() {
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString("### Options inherited from parent commands\n\n```\n")
|
2016-10-31 23:50:02 +08:00
|
|
|
parentFlags.PrintDefaults()
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString("```\n\n")
|
2016-10-31 23:50:02 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-11 22:39:54 +08:00
|
|
|
// GenMarkdown creates markdown output.
|
2016-10-31 23:50:02 +08:00
|
|
|
func GenMarkdown(cmd *cobra.Command, w io.Writer) error {
|
|
|
|
return GenMarkdownCustom(cmd, w, func(s string) string { return s })
|
|
|
|
}
|
|
|
|
|
2017-05-11 22:39:54 +08:00
|
|
|
// GenMarkdownCustom creates custom markdown output.
|
2016-10-31 23:50:02 +08:00
|
|
|
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
|
2017-07-23 15:51:42 +08:00
|
|
|
cmd.InitDefaultHelpCmd()
|
|
|
|
cmd.InitDefaultHelpFlag()
|
|
|
|
|
2017-05-11 22:39:54 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2016-10-31 23:50:02 +08:00
|
|
|
name := cmd.CommandPath()
|
|
|
|
|
|
|
|
short := cmd.Short
|
|
|
|
long := cmd.Long
|
|
|
|
if len(long) == 0 {
|
|
|
|
long = short
|
|
|
|
}
|
|
|
|
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString("## " + name + "\n\n")
|
|
|
|
buf.WriteString(short + "\n\n")
|
|
|
|
buf.WriteString("### Synopsis\n\n")
|
|
|
|
buf.WriteString("\n" + long + "\n\n")
|
2016-10-31 23:50:02 +08:00
|
|
|
|
|
|
|
if cmd.Runnable() {
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.UseLine()))
|
2016-10-31 23:50:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(cmd.Example) > 0 {
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString("### Examples\n\n")
|
|
|
|
buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.Example))
|
2016-10-31 23:50:02 +08:00
|
|
|
}
|
|
|
|
|
2017-05-11 22:39:54 +08:00
|
|
|
if err := printOptions(buf, cmd, name); err != nil {
|
2016-10-31 23:50:02 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hasSeeAlso(cmd) {
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString("### SEE ALSO\n")
|
2016-10-31 23:50:02 +08:00
|
|
|
if cmd.HasParent() {
|
|
|
|
parent := cmd.Parent()
|
|
|
|
pname := parent.CommandPath()
|
|
|
|
link := pname + ".md"
|
|
|
|
link = strings.Replace(link, " ", "_", -1)
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short))
|
2016-10-31 23:50:02 +08:00
|
|
|
cmd.VisitParents(func(c *cobra.Command) {
|
|
|
|
if c.DisableAutoGenTag {
|
|
|
|
cmd.DisableAutoGenTag = c.DisableAutoGenTag
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
children := cmd.Commands()
|
|
|
|
sort.Sort(byName(children))
|
|
|
|
|
|
|
|
for _, child := range children {
|
2017-05-11 22:39:54 +08:00
|
|
|
if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
|
2016-10-31 23:50:02 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
cname := name + " " + child.Name()
|
|
|
|
link := cname + ".md"
|
|
|
|
link = strings.Replace(link, " ", "_", -1)
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short))
|
2016-10-31 23:50:02 +08:00
|
|
|
}
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString("\n")
|
2016-10-31 23:50:02 +08:00
|
|
|
}
|
|
|
|
if !cmd.DisableAutoGenTag {
|
2017-05-11 22:39:54 +08:00
|
|
|
buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n")
|
2016-10-31 23:50:02 +08:00
|
|
|
}
|
2017-05-11 22:39:54 +08:00
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
2016-10-31 23:50:02 +08:00
|
|
|
}
|
|
|
|
|
2017-05-11 22:39:54 +08:00
|
|
|
// GenMarkdownTree will generate a markdown page for this command and all
|
|
|
|
// descendants in the directory given. The header may be nil.
|
|
|
|
// This function may not work 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`, it is undefined which
|
|
|
|
// help output will be in the file `cmd-sub-third.1`.
|
2016-10-31 23:50:02 +08:00
|
|
|
func GenMarkdownTree(cmd *cobra.Command, dir string) error {
|
|
|
|
identity := func(s string) string { return s }
|
|
|
|
emptyStr := func(s string) string { return "" }
|
|
|
|
return GenMarkdownTreeCustom(cmd, dir, emptyStr, identity)
|
|
|
|
}
|
|
|
|
|
2017-05-11 22:39:54 +08:00
|
|
|
// GenMarkdownTreeCustom is the the same as GenMarkdownTree, but
|
|
|
|
// with custom filePrepender and linkHandler.
|
2016-10-31 23:50:02 +08:00
|
|
|
func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {
|
|
|
|
for _, c := range cmd.Commands() {
|
2017-05-11 22:39:54 +08:00
|
|
|
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
2016-10-31 23:50:02 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
|
|
|
|
filename := filepath.Join(dir, basename)
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := io.WriteString(f, filePrepender(filename)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|