Skip to content
Myr

md_docs.go

This file

Path

doc/md_docs.go

155 lines · 4.5 KiB · Go

Last changed by

Revert "Add the ability to specify a filePostpender in GenMarkdownTreeCustom (#1270)" (#1317)John McBridebefore adoptionff416ad

History of this file

Take it

The walk stopped after 200 commits: lines older than 0ea93dd point at it.

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 1–61why, one step back
1//Copyright 2015 Red Hat Inc. All rights reserved.
2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314package doc1516import (17 "bytes"18 "fmt"19 "io"20 "os"21 "path/filepath"22 "sort"23 "strings"24 "time"2526 "github.com/spf13/cobra"27)2829func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {30 flags := cmd.NonInheritedFlags()31 flags.SetOutput(buf)32 if flags.HasAvailableFlags() {33 buf.WriteString("### Options\n\n```\n")34 flags.PrintDefaults()35 buf.WriteString("```\n\n")36 }3738 parentFlags := cmd.InheritedFlags()39 parentFlags.SetOutput(buf)40 if parentFlags.HasAvailableFlags() {41 buf.WriteString("### Options inherited from parent commands\n\n```\n")42 parentFlags.PrintDefaults()43 buf.WriteString("```\n\n")44 }45 return nil46}4748// GenMarkdown creates markdown output.49func GenMarkdown(cmd *cobra.Command, w io.Writer) error {50 return GenMarkdownCustom(cmd, w, func(s string) string { return s })51}5253// GenMarkdownCustom creates custom markdown output.54func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {55 cmd.InitDefaultHelpCmd()56 cmd.InitDefaultHelpFlag()5758 buf := new(bytes.Buffer)59 name := cmd.CommandPath()6061 buf.WriteString("## " + name + "\n\n")

doc: GenMarkdown skip Synopsis on empty long cmd (#1207)Marc Lopez Rubiobefore adoption02a0d2f

lines 62–66why, one step back
62 buf.WriteString(cmd.Short + "\n\n")
63 if len(cmd.Long) > 0 {64 buf.WriteString("### Synopsis\n\n")65 buf.WriteString(cmd.Long + "\n\n")66 }

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 67–124why, one step back
67
68 if cmd.Runnable() {69 buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.UseLine()))70 }7172 if len(cmd.Example) > 0 {73 buf.WriteString("### Examples\n\n")74 buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.Example))75 }7677 if err := printOptions(buf, cmd, name); err != nil {78 return err79 }80 if hasSeeAlso(cmd) {81 buf.WriteString("### SEE ALSO\n\n")82 if cmd.HasParent() {83 parent := cmd.Parent()84 pname := parent.CommandPath()85 link := pname + ".md"86 link = strings.Replace(link, " ", "_", -1)87 buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short))88 cmd.VisitParents(func(c *cobra.Command) {89 if c.DisableAutoGenTag {90 cmd.DisableAutoGenTag = c.DisableAutoGenTag91 }92 })93 }9495 children := cmd.Commands()96 sort.Sort(byName(children))9798 for _, child := range children {99 if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {100 continue101 }102 cname := name + " " + child.Name()103 link := cname + ".md"104 link = strings.Replace(link, " ", "_", -1)105 buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short))106 }107 buf.WriteString("\n")108 }109 if !cmd.DisableAutoGenTag {110 buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n")111 }112 _, err := buf.WriteTo(w)113 return err114}115116// GenMarkdownTree will generate a markdown page for this command and all117// descendants in the directory given. The header may be nil.118// This function may not work correctly if your command names have `-` in them.119// If you have `cmd` with two subcmds, `sub` and `sub-third`,120// and `sub` has a subcommand called `third`, it is undefined which121// help output will be in the file `cmd-sub-third.1`.122func GenMarkdownTree(cmd *cobra.Command, dir string) error {123 identity := func(s string) string { return s }124 emptyStr := func(s string) string { return "" }

Revert "Add the ability to specify a filePostpender in GenMarkdownTreeCustom (#1270)" (#1317)John McBridebefore adoptionff416ad

line 125why, one step back
125 return GenMarkdownTreeCustom(cmd, dir, emptyStr, identity)

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 126–128why, one step back
126}
127128// GenMarkdownTreeCustom is the the same as GenMarkdownTree, but

Revert "Add the ability to specify a filePostpender in GenMarkdownTreeCustom (#1270)" (#1317)John McBridebefore adoptionff416ad

lines 129–130why, one step back
129// with custom filePrepender and linkHandler.
130func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 131–134why, one step back
131 for _, c := range cmd.Commands() {
132 if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {133 continue134 }

Revert "Add the ability to specify a filePostpender in GenMarkdownTreeCustom (#1270)" (#1317)John McBridebefore adoptionff416ad

line 135why, one step back
135 if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 136–155why, one step back
136 return err
137 }138 }139140 basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"141 filename := filepath.Join(dir, basename)142 f, err := os.Create(filename)143 if err != nil {144 return err145 }146 defer f.Close()147148 if _, err := io.WriteString(f, filePrepender(filename)); err != nil {149 return err150 }151 if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {152 return err153 }154 return nil155}