139 lines
3.8 KiB
Go
139 lines
3.8 KiB
Go
/*
|
|
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.ApplyTemplateFormatting(9999, false, 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, 0644)
|
|
}
|