2023-08-30 13:26:26 +02:00
|
|
|
package docs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-09-06 16:44:50 +02:00
|
|
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/command"
|
2023-08-30 13:26:26 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra/doc"
|
|
|
|
)
|
|
|
|
|
|
|
|
const fmTemplate = `---
|
|
|
|
date: %s
|
|
|
|
title: "%s"
|
|
|
|
weight: 35
|
|
|
|
---
|
|
|
|
`
|
|
|
|
|
|
|
|
func websitePrepender(filename string) string {
|
|
|
|
now := time.Now().Format(time.RFC3339)
|
|
|
|
name := filepath.Base(filename)
|
|
|
|
base := strings.TrimSuffix(name, path.Ext(name))
|
|
|
|
return fmt.Sprintf(fmTemplate, now, strings.Replace(base, "_", " ", -1))
|
|
|
|
}
|
|
|
|
|
2023-08-30 17:17:50 +02:00
|
|
|
func websiteLinkHandler(filename string) string {
|
|
|
|
return "../" + strings.TrimSuffix(filename, filepath.Ext(filename))
|
|
|
|
}
|
|
|
|
|
2023-08-30 13:26:26 +02:00
|
|
|
func identity(s string) string {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func empty(s string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func Command(root *cobra.Command) *cobra.Command {
|
|
|
|
var path string
|
|
|
|
var website bool
|
2023-08-31 17:17:26 +02:00
|
|
|
var autogenTag bool
|
2023-08-30 13:26:26 +02:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "docs",
|
2023-09-06 16:44:50 +02:00
|
|
|
Short: command.FormatDescription(true, websiteUrl, false, description...),
|
|
|
|
Long: command.FormatDescription(false, websiteUrl, false, description...),
|
|
|
|
Example: command.FormatExamples(examples...),
|
2023-08-30 13:26:26 +02:00
|
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
|
|
|
prepender := empty
|
|
|
|
linkHandler := identity
|
|
|
|
if website {
|
|
|
|
prepender = websitePrepender
|
2023-08-30 17:17:50 +02:00
|
|
|
linkHandler = websiteLinkHandler
|
2023-08-30 13:26:26 +02:00
|
|
|
}
|
|
|
|
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
|
|
if err := os.MkdirAll(path, os.ModeDir|os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 17:17:26 +02:00
|
|
|
root.DisableAutoGenTag = !autogenTag
|
2023-08-30 13:26:26 +02:00
|
|
|
return doc.GenMarkdownTreeCustom(root, path, prepender, linkHandler)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&path, "output", "o", ".", "Output path")
|
|
|
|
cmd.Flags().BoolVar(&website, "website", false, "Website version")
|
2023-08-31 17:17:26 +02:00
|
|
|
cmd.Flags().BoolVar(&autogenTag, "autogenTag", true, "Determines if the generated docs should contain a timestamp")
|
2023-08-30 13:26:26 +02:00
|
|
|
if err := cmd.MarkFlagDirname("output"); err != nil {
|
|
|
|
log.Println("WARNING", err)
|
|
|
|
}
|
|
|
|
if err := cmd.MarkFlagRequired("output"); err != nil {
|
|
|
|
log.Println("WARNING", err)
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|