github.com/crowdsecurity/crowdsec@v1.6.1/cmd/crowdsec-cli/doc.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/spf13/cobra"
     9  	"github.com/spf13/cobra/doc"
    10  )
    11  
    12  type cliDoc struct{}
    13  
    14  func NewCLIDoc() *cliDoc {
    15  	return &cliDoc{}
    16  }
    17  
    18  func (cli cliDoc) NewCommand(rootCmd *cobra.Command) *cobra.Command {
    19  	cmd := &cobra.Command{
    20  		Use:               "doc",
    21  		Short:             "Generate the documentation in `./doc/`. Directory must exist.",
    22  		Args:              cobra.ExactArgs(0),
    23  		Hidden:            true,
    24  		DisableAutoGenTag: true,
    25  		RunE: func(_ *cobra.Command, _ []string) error {
    26  			if err := doc.GenMarkdownTreeCustom(rootCmd, "./doc/", cli.filePrepender, cli.linkHandler); err != nil {
    27  				return fmt.Errorf("failed to generate cobra doc: %s", err)
    28  			}
    29  			return nil
    30  		},
    31  	}
    32  
    33  	return cmd
    34  }
    35  
    36  func (cli cliDoc) filePrepender(filename string) string {
    37  	const header = `---
    38  id: %s
    39  title: %s
    40  ---
    41  `
    42  	name := filepath.Base(filename)
    43  	base := strings.TrimSuffix(name, filepath.Ext(name))
    44  	return fmt.Sprintf(header, base, strings.ReplaceAll(base, "_", " "))
    45  }
    46  
    47  func (cli cliDoc) linkHandler(name string) string {
    48  	return fmt.Sprintf("/cscli/%s", name)
    49  }