11package cli
22
33import (
4- "fmt"
5- "os"
6- "path/filepath"
7- "strings"
8-
9- "github.com/fatih/color"
10- "github.com/spf13/cobra"
11- "github.com/spf13/cobra/doc"
4+ "fmt"
5+ "os"
6+ "path/filepath"
7+ "strings"
8+
9+ "github.com/fatih/color"
10+ "github.com/spf13/cobra"
11+ "github.com/spf13/cobra/doc"
1212)
1313
1414var docsCmd = & cobra.Command {
15- Use : "docs" ,
16- Short : "Generate CLI documentation" ,
17- Long : `Generate comprehensive markdown documentation for all CLI commands.` ,
18- Example : ` # Generate documentation
15+ Use : "docs" ,
16+ Short : "Generate CLI documentation" ,
17+ Long : `Generate comprehensive markdown documentation for all CLI commands.` ,
18+ Example : ` # Generate documentation
1919 otc-cli docs
2020
2121 # Generates:
2222 # - otc-cli.md (main docs in root)
2323 # - docs/*.md (subcommand docs)` ,
24- RunE : runDocs ,
24+ RunE : runDocs ,
2525}
2626
2727func runDocs (cmd * cobra.Command , args []string ) error {
28- // Create docs subdirectory if it doesn't exist
29- docsSubDir := "./docs"
30- if err := os .MkdirAll (docsSubDir , 0755 ); err != nil {
31- return fmt .Errorf ("failed to create docs directory: %w" , err )
32- }
33-
34- // Custom link handler to fix relative paths
35- linkHandler := func (name string ) string {
36- base := strings .TrimSuffix (name , filepath .Ext (name ))
37- // Main doc stays in root, subcommands go to docs/
38- if base == "otc-cli" {
39- return name
40- }
41- return "docs/" + name
42- }
43-
44- // Generate documentation with custom link handler
45- if err := doc .GenMarkdownTreeCustom (rootCmd , "." , filePrepender , linkHandler ); err != nil {
46- return fmt .Errorf ("failed to generate docs: %w" , err )
47- }
48-
49- // Move subcommand docs to docs/ directory, keep root doc in root
50- entries , err := os .ReadDir ("." )
51- if err != nil {
52- return fmt .Errorf ("failed to read directory: %w" , err )
53- }
54-
55- var mainDocCount , subDocCount int
56- for _ , entry := range entries {
57- // Skip directories and non-markdown files
58- if entry .IsDir () || filepath .Ext (entry .Name ()) != ".md" {
59- continue
60- }
61-
62- // If it's the main otc-cli.md, keep it in root
63- if entry .Name () == "otc-cli.md" {
64- mainDocCount ++
65- continue
66- }
67-
68- // If it's a subcommand doc (starts with otc-cli_), move to docs/
69- if strings .HasPrefix (entry .Name (), "otc-cli_" ) {
70- oldPath := entry .Name ()
71- newPath := filepath .Join (docsSubDir , entry .Name ())
72-
73- if err := os .Rename (oldPath , newPath ); err != nil {
74- color .Yellow ("Warning: could not move %s: %v" , entry .Name (), err )
75- } else {
76- subDocCount ++
77- }
78- }
79- }
80-
81- color .Green ("\n ✓ Documentation generated successfully!\n " )
82- color .Cyan ("Main documentation:" )
83- fmt .Printf (" - %s (root directory)\n \n " , "otc-cli.md" )
84-
85- color .Cyan ("Subcommand documentation (%d files in docs/):" , subDocCount )
86-
87- // List files in docs/
88- docEntries , _ := os .ReadDir (docsSubDir )
89- for _ , entry := range docEntries {
90- if ! entry .IsDir () && filepath .Ext (entry .Name ()) == ".md" {
91- fmt .Printf (" - docs/%s\n " , entry .Name ())
92- }
93- }
94-
95- fmt .Println ()
96- return nil
28+ // Create docs subdirectory if it doesn't exist
29+ docsSubDir := "./docs"
30+ if err := os .MkdirAll (docsSubDir , 0755 ); err != nil {
31+ return fmt .Errorf ("failed to create docs directory: %w" , err )
32+ }
33+
34+ // Custom link handler to fix relative paths
35+ linkHandler := func (name string ) string {
36+ base := strings .TrimSuffix (name , filepath .Ext (name ))
37+ // Main doc stays in root, subcommands go to docs/
38+ if base == "otc-cli" {
39+ return name
40+ }
41+ return "docs/" + name
42+ }
43+
44+ // Generate documentation with custom link handler
45+ if err := doc .GenMarkdownTreeCustom (rootCmd , "." , filePrepender , linkHandler ); err != nil {
46+ return fmt .Errorf ("failed to generate docs: %w" , err )
47+ }
48+
49+ // Move subcommand docs to docs/ directory, keep root doc in root
50+ entries , err := os .ReadDir ("." )
51+ if err != nil {
52+ return fmt .Errorf ("failed to read directory: %w" , err )
53+ }
54+
55+ var mainDocCount , subDocCount int
56+ for _ , entry := range entries {
57+ // Skip directories and non-markdown files
58+ if entry .IsDir () || filepath .Ext (entry .Name ()) != ".md" {
59+ continue
60+ }
61+
62+ // If it's the main otc-cli.md, keep it in root
63+ if entry .Name () == "otc-cli.md" {
64+ mainDocCount ++
65+ continue
66+ }
67+
68+ // If it's a subcommand doc (starts with otc-cli_), move to docs/
69+ if strings .HasPrefix (entry .Name (), "otc-cli_" ) {
70+ oldPath := entry .Name ()
71+ newPath := filepath .Join (docsSubDir , entry .Name ())
72+
73+ if err := os .Rename (oldPath , newPath ); err != nil {
74+ color .Yellow ("Warning: could not move %s: %v" , entry .Name (), err )
75+ } else {
76+ subDocCount ++
77+ }
78+ }
79+ }
80+
81+ color .Green ("\n ✓ Documentation generated successfully!\n " )
82+ color .Cyan ("Main documentation:" )
83+ fmt .Printf (" - %s (root directory)\n \n " , "otc-cli.md" )
84+
85+ color .Cyan ("Subcommand documentation (%d files in docs/):" , subDocCount )
86+
87+ // List files in docs/
88+ docEntries , _ := os .ReadDir (docsSubDir )
89+ for _ , entry := range docEntries {
90+ if ! entry .IsDir () && filepath .Ext (entry .Name ()) == ".md" {
91+ fmt .Printf (" - docs/%s\n " , entry .Name ())
92+ }
93+ }
94+
95+ fmt .Println ()
96+ return nil
9797}
9898
9999// filePrepender adds a header to generated files
100100func filePrepender (filename string ) string {
101- return ""
102- }
101+ return ""
102+ }
0 commit comments