Skip to content

Commit

Permalink
fix command names
Browse files Browse the repository at this point in the history
  • Loading branch information
chicoxyzzy committed Jul 11, 2024
1 parent ea91775 commit 88fdefd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
51 changes: 35 additions & 16 deletions cli/generate_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cli

import (
"bytes"
"os"
"path"
"strings"
Expand All @@ -13,37 +14,55 @@ import (

var isDocsBuild = true

func generateDocs(cmd *cobra.Command, filename string, title string) {
func generateDocs(cmd *cobra.Command, title string) {
cmd.DisableAutoGenTag = true

// create docs directory
_ = os.Mkdir("./docs", 0755)

err := doc.GenMarkdownTreeCustom(cmd, "./docs",
func(_ string) string {
return `---
title: ` + title + `
---
`
},
func(name string) string {
// err := doc.GenMarkdownCustom(cmd, out, func(name string) string {
base := strings.TrimSuffix(name, path.Ext(name))
return "/cli/" + strings.ToLower(base) + "/"
})
out := new(bytes.Buffer)

err := doc.GenMarkdownCustom(cmd, out, func(name string) string {
// err := doc.GenMarkdownCustom(cmd, out, func(name string) string {
base := strings.TrimSuffix(name, path.Ext(name))
return "/cli/" + strings.ToLower(base) + "/"
})
if err != nil {
panic(err)
}

// Define the text to be replaced and the replacement text
oldText := []byte("## " + title)
newText := []byte("---\ntitle: " + title + "\n---")

// Perform the replacement on the buffer's content
updatedContent := bytes.Replace(out.Bytes(), oldText, newText, 1)

// Reset the buffer and write the updated content back to it
out.Reset()
out.Write(updatedContent)

// write markdown to file
file, err := os.Create("./docs/" + strings.ReplaceAll(title, " ", "_") + ".md")
if err != nil {
panic(err)
}

_, err = file.Write(out.Bytes())
if err != nil {
panic(err)
}

defer file.Close()

// if command has subcommands, generate markdown for each subcommand
if cmd.HasSubCommands() {
for _, c := range cmd.Commands() {
// if c.Use starts with "help", skip it
if strings.HasPrefix(c.Use, "help") {
if c.Name() == "help" {
continue
}
generateDocs(c, filename, title+" "+c.Use)
generateDocs(c, title+" "+c.Name())
}
}
}
2 changes: 1 addition & 1 deletion cli/generate_docs_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import "github.com/spf13/cobra"

var isDocsBuild = false

func generateDocs(_ *cobra.Command, _ string, _ string) {
func generateDocs(_ *cobra.Command, _ string) {
// do nothing if the build tag "docs" is not set
}
7 changes: 1 addition & 6 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ func createMainCommand() *cobra.Command {
cmd.AddCommand(versionCommand())

// Generate markdown documentation
generateDocs(loginCommand(), "dispatch_login.md", "dispatch login")
generateDocs(initCommand(), "dispatch_init.md", "dispatch init")
generateDocs(switchCommand(DispatchConfigPath), "dispatch_switch.md", "dispatch switch")
generateDocs(verificationCommand(), "dispatch_verification.md", "dispatch verification")
generateDocs(runCommand(), "dispatch_run.md", "dispatch run")
generateDocs(versionCommand(), "dispatch_version.md", "dispatch version")
generateDocs(cmd, "dispatch")

return cmd
}
Expand Down

0 comments on commit 88fdefd

Please sign in to comment.