mirror of
https://github.com/sbrow/envr.git
synced 2025-12-29 23:47:39 -05:00
refactor: Rewrote in golang.
This commit is contained in:
58
internal/tools/docgen/main.go
Normal file
58
internal/tools/docgen/main.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/sbrow/envr/cmd" // update to your module path
|
||||
"github.com/spf13/cobra/doc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
out := flag.String("out", "./docs/cli", "output directory")
|
||||
format := flag.String("format", "markdown", "markdown|man|rest")
|
||||
front := flag.Bool("frontmatter", false, "prepend simple YAML front matter to markdown")
|
||||
flag.Parse()
|
||||
|
||||
if err := os.MkdirAll(*out, 0o755); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
root := cmd.Root()
|
||||
root.DisableAutoGenTag = true // stable, reproducible files (no timestamp footer)
|
||||
|
||||
switch *format {
|
||||
case "markdown":
|
||||
if *front {
|
||||
prep := func(filename string) string {
|
||||
base := filepath.Base(filename)
|
||||
name := strings.TrimSuffix(base, filepath.Ext(base))
|
||||
title := strings.ReplaceAll(name, "_", " ")
|
||||
return fmt.Sprintf("---\ntitle: %q\nslug: %q\ndescription: \"CLI reference for %s\"\n---\n\n", title, name, title)
|
||||
}
|
||||
link := func(name string) string { return strings.ToLower(name) }
|
||||
if err := doc.GenMarkdownTreeCustom(root, *out, prep, link); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
if err := doc.GenMarkdownTree(root, *out); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
case "man":
|
||||
hdr := &doc.GenManHeader{Title: strings.ToUpper(root.Name()), Section: "1"}
|
||||
if err := doc.GenManTree(root, hdr, *out); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
case "rest":
|
||||
if err := doc.GenReSTTree(root, *out); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
default:
|
||||
log.Fatalf("unknown format: %s", *format)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user