diff --git a/cli/generate_docs.go b/cli/generate_docs.go index 04c7430..4450247 100644 --- a/cli/generate_docs.go +++ b/cli/generate_docs.go @@ -3,6 +3,7 @@ package cli import ( + "bytes" "os" "path" "strings" @@ -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()) } } } diff --git a/cli/generate_docs_default.go b/cli/generate_docs_default.go index 851f719..d8b0722 100644 --- a/cli/generate_docs_default.go +++ b/cli/generate_docs_default.go @@ -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 } diff --git a/cli/main.go b/cli/main.go index ccb4d66..2d1e7ef 100644 --- a/cli/main.go +++ b/cli/main.go @@ -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 }