Skip to content

Commit

Permalink
ci: generate zetacored CLI docs in a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Aug 7, 2024
1 parent 27303ee commit cf810bc
Show file tree
Hide file tree
Showing 283 changed files with 12,271 additions and 12,228 deletions.
54 changes: 50 additions & 4 deletions cmd/zetacored/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package main

import (
"os"

"bytes"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)

func docsCmd(cmd *cobra.Command, args []string) error {
var path string

// If path is provided as an argument, use it. Else, get from flag.
// Determine the output path
if len(args) > 0 {
path = args[0]
} else {
Expand All @@ -21,25 +22,70 @@ func docsCmd(cmd *cobra.Command, args []string) error {
}
}

// Create the output directory if it doesn't exist
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, 0750)
if err != nil {
return err
}
}

err := doc.GenMarkdownTree(cmd.Root(), path)
// Set the output file
outputFile := path + "/cli.md"
file, err := os.Create(outputFile)
if err != nil {
return err
}
defer file.Close()

// Generate the documentation
err = GenMarkdownToSingleFile(cmd.Root(), file)
if err != nil {
return err
}
return nil
}

// GenMarkdownToSingleFile generates markdown documentation for all commands into a single file
func GenMarkdownToSingleFile(cmd *cobra.Command, w *os.File) error {
buf := new(bytes.Buffer)
linkHandler := func(s string) string {
anchor := strings.ReplaceAll(strings.ToLower(s), " ", "-")
anchor = strings.ReplaceAll(anchor, "_", "-")
return "#" + anchor
}

cmd.DisableAutoGenTag = true

err := doc.GenMarkdownCustom(cmd, buf, linkHandler)
if err != nil {
return err
}

_, err = buf.WriteTo(w)
if err != nil {
return err
}

for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
continue
}
err := GenMarkdownToSingleFile(c, w)
if err != nil {
return err
}
}

return nil
}


func docsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "docs [path]",
Short: "Generate markdown documentation for zetacored",
RunE: docsCmd,
RunE: docsCmd,
Args: cobra.MaximumNArgs(1),
}

Expand Down
Loading

0 comments on commit cf810bc

Please sign in to comment.