Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: generate zetacored CLI docs in a single file #2651

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
67 changes: 63 additions & 4 deletions cmd/zetacored/docs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package main

import (
"bytes"
"errors"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
Expand All @@ -10,7 +14,7 @@ import (
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,20 +25,75 @@ func docsCmd(cmd *cobra.Command, args []string) error {
}
}

if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, 0750)
// Sanitize and validate the path
absPath, err := filepath.Abs(path)
if err != nil {
return err
}

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

err := doc.GenMarkdownTree(cmd.Root(), path)
// Set the output file
outputFile := filepath.Join(absPath, "cli.md")

// Inline validation of the output file path
cleanPath := filepath.Clean(outputFile)
if strings.Contains(cleanPath, "..") {
return errors.New("invalid output file path: potential security risk")
}

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 {
return "#" + strings.NewReplacer(" ", "-", "_", "-").Replace(strings.ToLower(s))
}

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]",
Expand Down
Loading
Loading