Skip to content
Myr

md_docs.go

This file

Path

md_docs.go

124 lines · 3.1 KiB · Go

Last changed by

Deprecated subcommandsEric Parisbefore adoptionc3e48f9

History of this file

Take it

Merge pull request #87 from eparis/gen-md-docEric Parisbefore adoptionc0da825

lines 1–36why, 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 cobra1516import (17 "bytes"18 "fmt"19 "os"20 "sort"21 "strings"22 "time"23)2425func printOptions(out *bytes.Buffer, cmd *Command, name string) {26 flags := cmd.NonInheritedFlags()27 flags.SetOutput(out)28 if flags.HasFlags() {29 fmt.Fprintf(out, "### Options\n\n```\n")30 flags.PrintDefaults()31 fmt.Fprintf(out, "```\n\n")32 }3334 parentFlags := cmd.InheritedFlags()35 parentFlags.SetOutput(out)36 if parentFlags.HasFlags() {

Merge pull request #96 from commonlisp/masterBjørn Erik Pedersenbefore adoptiond697cc2

line 37why, one step back
37 fmt.Fprintf(out, "### Options inherited from parent commands\n\n```\n")

Merge pull request #87 from eparis/gen-md-docEric Parisbefore adoptionc0da825

lines 38–87why, one step back
38 parentFlags.PrintDefaults()
39 fmt.Fprintf(out, "```\n\n")40 }41}4243type byName []*Command4445func (s byName) Len() int { return len(s) }46func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }47func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }4849func GenMarkdown(cmd *Command, out *bytes.Buffer) {50 name := cmd.CommandPath()5152 short := cmd.Short53 long := cmd.Long54 if len(long) == 0 {55 long = short56 }5758 fmt.Fprintf(out, "## %s\n\n", name)59 fmt.Fprintf(out, "%s\n\n", short)60 fmt.Fprintf(out, "### Synopsis\n\n")61 fmt.Fprintf(out, "\n%s\n\n", long)6263 if cmd.Runnable() {64 fmt.Fprintf(out, "```\n%s\n```\n\n", cmd.UseLine())65 }6667 if len(cmd.Example) > 0 {68 fmt.Fprintf(out, "### Examples\n\n")69 fmt.Fprintf(out, "```\n%s\n```\n\n", cmd.Example)70 }7172 printOptions(out, cmd, name)7374 if len(cmd.Commands()) > 0 || cmd.HasParent() {75 fmt.Fprintf(out, "### SEE ALSO\n")76 if cmd.HasParent() {77 parent := cmd.Parent()78 pname := parent.CommandPath()79 link := pname + ".md"80 link = strings.Replace(link, " ", "_", -1)81 fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, link, parent.Short)82 }8384 children := cmd.Commands()85 sort.Sort(byName(children))8687 for _, child := range children {

Deprecated subcommandsEric Parisbefore adoptionc3e48f9

lines 88–90why, one step back
88 if len(child.Deprecated) > 0 {
89 continue90 }

Merge pull request #87 from eparis/gen-md-docEric Parisbefore adoptionc0da825

lines 91–124why, one step back
91 cname := name + " " + child.Name()
92 link := cname + ".md"93 link = strings.Replace(link, " ", "_", -1)94 fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, link, child.Short)95 }96 fmt.Fprintf(out, "\n")97 }9899 fmt.Fprintf(out, "###### Auto generated by spf13/cobra at %s\n", time.Now().UTC())100}101102func GenMarkdownTree(cmd *Command, dir string) {103 for _, c := range cmd.Commands() {104 GenMarkdownTree(c, dir)105 }106107 out := new(bytes.Buffer)108109 GenMarkdown(cmd, out)110111 filename := cmd.CommandPath()112 filename = dir + strings.Replace(filename, " ", "_", -1) + ".md"113 outFile, err := os.Create(filename)114 if err != nil {115 fmt.Println(err)116 os.Exit(1)117 }118 defer outFile.Close()119 _, err = outFile.Write(out.Bytes())120 if err != nil {121 fmt.Println(err)122 os.Exit(1)123 }124}