Skip to content

Commit 0a97c52

Browse files
committed
✨ Add ability to generate markdown docs of commands
1 parent 9a604dc commit 0a97c52

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

cmd/tool.go

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"github.com/julien040/anyquery/controller"
55
"github.com/spf13/cobra"
6+
"github.com/spf13/cobra/doc"
67
)
78

89
var toolCmd = &cobra.Command{
@@ -53,11 +54,22 @@ var toolDevNewTableCmd = &cobra.Command{
5354
RunE: controller.DevNewTable,
5455
}
5556

57+
var toolGenerateDocCmd = &cobra.Command{
58+
Use: "generate-doc [dir]",
59+
Short: "Generate the markdown documentation of the CLI",
60+
Args: cobra.ExactArgs(1),
61+
RunE: func(cmd *cobra.Command, args []string) error {
62+
return doc.GenMarkdownTree(rootCmd, args[0])
63+
},
64+
}
65+
66+
5667
func init() {
5768
rootCmd.AddCommand(toolCmd)
5869
toolCmd.AddCommand(toolHashDirCmd)
5970
toolCmd.AddCommand(toolMySQLPasswordCmd)
6071
toolCmd.AddCommand(toolDevCmd)
6172
toolDevCmd.AddCommand(toolDevInitCmd)
6273
toolDevCmd.AddCommand(toolDevNewTableCmd)
74+
toolCmd.AddCommand(toolGenerateDocCmd)
6375
}

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ require (
3333
github.com/pganalyze/pg_query_go/v5 v5.1.0
3434
github.com/runreveal/pql v0.1.0
3535
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
36-
github.com/spf13/cobra v1.8.0
36+
github.com/spf13/cobra v1.8.1
3737
github.com/spf13/pflag v1.0.5
3838
github.com/stretchr/testify v1.9.0
3939
golang.org/x/crypto v0.24.0
@@ -66,6 +66,7 @@ require (
6666
github.com/charmbracelet/x/input v0.1.1 // indirect
6767
github.com/charmbracelet/x/term v0.1.1 // indirect
6868
github.com/charmbracelet/x/windows v0.1.2 // indirect
69+
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
6970
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
7071
github.com/dustin/go-humanize v1.0.1 // indirect
7172
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
@@ -105,6 +106,7 @@ require (
105106
github.com/pkg/term v1.2.0-beta.2 // indirect
106107
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
107108
github.com/rivo/uniseg v0.4.7 // indirect
109+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
108110
github.com/segmentio/encoding v0.3.6 // indirect
109111
github.com/ulikunitz/xz v0.5.10 // indirect
110112
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWH
260260
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
261261
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
262262
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
263+
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
264+
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
263265
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
264266
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
265267
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -521,6 +523,7 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
521523
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
522524
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
523525
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
526+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
524527
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
525528
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
526529
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
@@ -530,6 +533,8 @@ github.com/segmentio/encoding v0.3.6/go.mod h1:n0JeuIqEQrQoPDGsjo8UNd1iA0U8d8+oH
530533
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
531534
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
532535
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
536+
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
537+
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
533538
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
534539
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
535540
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#! /usr/bin/env bun
2+
3+
import { $ } from "bun";
4+
import { readdir } from "node:fs/promises";
5+
6+
// Export the commands
7+
try {
8+
await $`anyquery tool generate-doc Commands`;
9+
} catch (error) {
10+
// In case it's a build machine, anyquery might not be installed
11+
await $`../../../../../../main.out tool generate-doc Commands`;
12+
}
13+
14+
// For each file, take the lop level h2 header, and add the frontmatter
15+
// with the title equaling to the header text
16+
17+
const files = await readdir("Commands");
18+
19+
for (const file of files) {
20+
const fileContent = Bun.file(`Commands/${file}`);
21+
let text = await fileContent.text();
22+
23+
const title = text.match(/## (.*)/)[1];
24+
// Strip the title from the markdown
25+
text = text.replace(`## ${title}\n\n`, "");
26+
const frontmatter = `---
27+
title: ${title}
28+
description: Learn how to use the ${title} command in AnyQuery.
29+
---
30+
31+
`;
32+
console.log(`Writing ${title} to ${file}`);
33+
await Bun.write(`Commands/${file}`, frontmatter + text);
34+
}

0 commit comments

Comments
 (0)