Skip to content

Commit 0d2336e

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

2 files changed

Lines changed: 251 additions & 110 deletions

File tree

cmd/src/doc.go

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"io"
88
"os"
99
"path"
10+
"sort"
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"
17+
"github.com/urfave/cli/v3"
1518

1619
"github.com/sourcegraph/src-cli/internal/cmderrors"
1720
)
@@ -29,7 +32,7 @@ documentation used within Sourcegraph.
2932
Usage:
3033
3134
src doc -o DIR
32-
35+
3336
Examples:
3437
3538
$ src doc -o ~/sourcegraph/doc/integration/cli/reference
@@ -47,6 +50,8 @@ Examples:
4750
return cmderrors.ExitCode(1, nil)
4851
}
4952

53+
dstDir := *outputFlag
54+
5055
dr, err := newDocRenderer()
5156
if err != nil {
5257
return err
@@ -66,6 +71,7 @@ Examples:
6671

6772
pending := out.Pending(output.Line("", output.StylePending, "Rendering Markdown..."))
6873
count := 0
74+
rootSubcommands := map[string]string{}
6975
defer func() {
7076
pending.Complete(output.Linef(output.EmojiSuccess, output.StyleSuccess, "%d files rendered under %s", count, *outputFlag))
7177
}()
@@ -94,7 +100,7 @@ Examples:
94100
return err
95101
}
96102

97-
file, err := openDocFile(*outputFlag, fqcn)
103+
file, err := openDocFile(dstDir, fqcn)
98104
if err != nil {
99105
return err
100106
}
@@ -110,12 +116,19 @@ Examples:
110116
}
111117
}
112118

119+
if groupName == "" {
120+
for name, link := range subcommands {
121+
rootSubcommands[name] = link
122+
}
123+
continue
124+
}
125+
113126
content, err := dr.RenderGroup(groupName, subcommands)
114127
if err != nil {
115128
return err
116129
}
117130

118-
file, err := openDocFile(*outputFlag, groupName+" index")
131+
file, err := openDocFile(dstDir, groupName+" index")
119132
if err != nil {
120133
return err
121134
}
@@ -127,6 +140,44 @@ Examples:
127140
count++
128141
}
129142

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

@@ -151,6 +202,29 @@ func openDocFile(base, fqcn string) (interface {
151202
return os.Create(cmdPath + ".md")
152203
}
153204

205+
func migratedVisibleCommands(commands []*cli.Command) []*cli.Command {
206+
visible := make([]*cli.Command, 0, len(commands))
207+
for _, cmd := range commands {
208+
if cmd.Hidden {
209+
continue
210+
}
211+
visible = append(visible, cmd)
212+
}
213+
214+
sort.Slice(visible, func(i, j int) bool {
215+
return visible[i].Name < visible[j].Name
216+
})
217+
218+
return visible
219+
}
220+
221+
func migratedDocLink(cmd *cli.Command) string {
222+
if len(migratedVisibleCommands(cmd.Commands)) > 0 {
223+
return path.Join(cmd.Name, "index.md")
224+
}
225+
return cmd.Name + ".md"
226+
}
227+
154228
type docRenderer struct {
155229
commandTemplate *template.Template
156230
groupTemplate *template.Template

0 commit comments

Comments
 (0)