Skip to content

Commit 9d95ea0

Browse files
committed
feat/docgen: generate docs for legacy and urfave commands
1 parent 04f81b4 commit 9d95ea0

2 files changed

Lines changed: 234 additions & 109 deletions

File tree

cmd/src/doc.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"flag"
66
"fmt"
77
"io"
8+
"maps"
89
"os"
910
"path"
1011
"strings"
1112
"text/template"
1213

14+
"github.com/sourcegraph/sourcegraph/lib/docgen"
1315
"github.com/sourcegraph/sourcegraph/lib/errors"
1416
"github.com/sourcegraph/sourcegraph/lib/output"
1517

@@ -29,7 +31,7 @@ documentation used within Sourcegraph.
2931
Usage:
3032
3133
src doc -o DIR
32-
34+
3335
Examples:
3436
3537
$ src doc -o ~/sourcegraph/doc/integration/cli/reference
@@ -47,6 +49,8 @@ Examples:
4749
return cmderrors.ExitCode(1, nil)
4850
}
4951

52+
dstDir := *outputFlag
53+
5054
dr, err := newDocRenderer()
5155
if err != nil {
5256
return err
@@ -66,6 +70,7 @@ Examples:
6670

6771
pending := out.Pending(output.Line("", output.StylePending, "Rendering Markdown..."))
6872
count := 0
73+
rootSubcommands := map[string]string{}
6974
defer func() {
7075
pending.Complete(output.Linef(output.EmojiSuccess, output.StyleSuccess, "%d files rendered under %s", count, *outputFlag))
7176
}()
@@ -94,7 +99,7 @@ Examples:
9499
return err
95100
}
96101

97-
file, err := openDocFile(*outputFlag, fqcn)
102+
file, err := openDocFile(dstDir, fqcn)
98103
if err != nil {
99104
return err
100105
}
@@ -110,12 +115,17 @@ Examples:
110115
}
111116
}
112117

118+
if groupName == "" {
119+
maps.Copy(subcommands, rootSubcommands)
120+
continue
121+
}
122+
113123
content, err := dr.RenderGroup(groupName, subcommands)
114124
if err != nil {
115125
return err
116126
}
117127

118-
file, err := openDocFile(*outputFlag, groupName+" index")
128+
file, err := openDocFile(dstDir, groupName+" index")
119129
if err != nil {
120130
return err
121131
}
@@ -127,6 +137,44 @@ Examples:
127137
count++
128138
}
129139

140+
root := migratedRootCommand()
141+
mdFiles, err := docgen.Markdown(root)
142+
if err != nil {
143+
return err
144+
}
145+
146+
for _, cmd := range docgen.VisibleCommands(root.Commands) {
147+
rootSubcommands[cmd.Name] = docgen.SubcommandDocPath(cmd)
148+
}
149+
150+
for _, md := range mdFiles {
151+
pending.Update(md.Name)
152+
docPath := path.Join(dstDir, md.Name)
153+
if err := os.MkdirAll(path.Dir(docPath), 0755); err != nil {
154+
return err
155+
}
156+
if err := os.WriteFile(docPath, []byte(md.Content), 0644); err != nil {
157+
return err
158+
}
159+
count++
160+
}
161+
162+
content, err := dr.RenderGroup("", rootSubcommands)
163+
if err != nil {
164+
return err
165+
}
166+
167+
file, err := openDocFile(dstDir, " index")
168+
if err != nil {
169+
return err
170+
}
171+
defer file.Close()
172+
173+
if _, err := file.WriteString(content); err != nil {
174+
return err
175+
}
176+
count++
177+
130178
return nil
131179
}
132180

0 commit comments

Comments
 (0)