-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
docs.go
138 lines (123 loc) · 3.78 KB
/
docs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Copyright 2020 The Kubernetes Authors 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 generate
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"k8s.io/minikube/pkg/minikube/out"
)
// Docs generates docs for minikube command
func Docs(root *cobra.Command, path string) error {
cmds := root.Commands()
for _, c := range cmds {
if c.Hidden {
glog.Infof("Skipping generating doc for %s as it's a hidden command", c.Name())
continue
}
contents, err := DocForCommand(c)
if err != nil {
return errors.Wrapf(err, "generating doc for %s", c.Name())
}
if err := saveDocForCommand(c, []byte(contents), path); err != nil {
return errors.Wrapf(err, "saving doc for %s", c.Name())
}
}
return nil
}
// DocForCommand returns the specific doc for that command
func DocForCommand(command *cobra.Command) (string, error) {
buf := bytes.NewBuffer([]byte{})
if err := generateTitle(command, buf); err != nil {
return "", errors.Wrap(err, "generating title")
}
if err := rewriteFlags(command); err != nil {
return "", errors.Wrap(err, "rewriting flags")
}
if err := writeSubcommands(command, buf); err != nil {
return "", errors.Wrap(err, "writing subcommands")
}
return removeHelpText(buf), nil
}
// after every command, cobra automatically appends
// ### SEE ALSO
// * [minikube addons](minikube_addons.md) - Modify minikube's Kubernetes addons
// ###### Auto generated by spf13/cobra on 1-Apr-2020
// help text which is unnecessary info after every subcommand
// This function removes that text.
func removeHelpText(buffer *bytes.Buffer) string {
beginningHelpText := "### SEE ALSO"
endHelpText := "###### Auto generated by spf13/cobra"
scanner := bufio.NewScanner(buffer)
includeLine := true
final := bytes.NewBuffer([]byte{})
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, beginningHelpText) {
includeLine = false
continue
}
if strings.Contains(line, endHelpText) {
includeLine = true
continue
}
if !includeLine {
continue
}
// scanner strips the ending newline
if _, err := final.WriteString(line + "\n"); err != nil {
glog.Warningf("error removing help text: %v", err)
break
}
}
return final.String()
}
// writeSubcommands recursively appends all subcommands to the doc
func writeSubcommands(command *cobra.Command, w io.Writer) error {
if err := doc.GenMarkdown(command, w); err != nil {
return errors.Wrapf(err, "getting markdown custom")
}
if !command.HasSubCommands() {
return nil
}
subCommands := command.Commands()
for _, sc := range subCommands {
if err := writeSubcommands(sc, w); err != nil {
return err
}
}
return nil
}
func generateTitle(command *cobra.Command, w io.Writer) error {
date := time.Now().Format("2006-01-02")
title := out.Fmt(title, out.V{"Command": command.Name(), "Description": command.Short, "Date": date})
_, err := w.Write([]byte(title))
return err
}
func saveDocForCommand(command *cobra.Command, contents []byte, path string) error {
fp := filepath.Join(path, fmt.Sprintf("%s.md", command.Name()))
if err := os.Remove(fp); err != nil {
glog.Warningf("error removing %s", fp)
}
return ioutil.WriteFile(fp, contents, 0o644)
}