Skip to content

Commit d2be210

Browse files
authored
Merge pull request #84 from dispatchrun/gen_docs
Create CLI docs
2 parents fb0e7a4 + 1cb3578 commit d2be210

9 files changed

+134
-14
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ goreleaser/
3434
.netrc
3535

3636
.dispatch
37+
38+
docs/

.goreleaser.yml

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
project_name: dispatch
22
dist: ./goreleaser/dist
3+
version: 2
34

45
before:
56
hooks:
@@ -9,7 +10,8 @@ gomod:
910
proxy: true
1011

1112
builds:
12-
- main: .
13+
- id: dispatch
14+
main: .
1315
binary: dispatch
1416
mod_timestamp: "{{ .CommitTimestamp }}"
1517

@@ -22,6 +24,29 @@ builds:
2224
- linux
2325
- windows
2426

27+
- id: dispatch-docs
28+
main: .
29+
binary: dispatch-docs
30+
mod_timestamp: "{{ .CommitTimestamp }}"
31+
tags: docs
32+
33+
goarch:
34+
- amd64
35+
36+
goos:
37+
- linux
38+
39+
archives:
40+
- id: dispatch
41+
builds: [dispatch]
42+
format_overrides:
43+
- goos: windows
44+
format: zip
45+
46+
- id: dispatch-docs
47+
builds: [dispatch-docs]
48+
name_template: "{{ .ProjectName }}_docs_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
49+
2550
release:
2651
github:
2752
owner: dispatchrun
@@ -34,6 +59,8 @@ changelog:
3459

3560
brews:
3661
- name: dispatch
62+
ids:
63+
- dispatch
3764
url_template: "https://github.com/dispatchrun/dispatch/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
3865

3966
commit_author:

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ push: image
3939
update:
4040
for ref in $$(yq -r '.deps[] | .remote + "/gen/go/" + .owner + "/" + .repository + "/protocolbuffers/go@" + .commit' proto/buf.lock); do go get $$ref; done
4141
go mod tidy
42+
43+
dispatch-docs:
44+
${GO} build -tags docs -o ${DISPATCH} .
45+
${DISPATCH}

cli/generate_docs.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//go:build docs
2+
3+
package cli
4+
5+
import (
6+
"bytes"
7+
"os"
8+
"path"
9+
"strings"
10+
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/cobra/doc"
13+
)
14+
15+
const DispatchCmdLong = "This is the main command for Dispatch CLI. Add a subcommand to make it useful."
16+
17+
const RunExampleText = "```\ndispatch run [options] -- <command>\n```"
18+
19+
func generateDocs(cmd *cobra.Command, title string) {
20+
cmd.DisableAutoGenTag = true
21+
22+
// create docs directory
23+
_ = os.Mkdir("./docs", 0755)
24+
25+
out := new(bytes.Buffer)
26+
27+
err := doc.GenMarkdownCustom(cmd, out, func(name string) string {
28+
// err := doc.GenMarkdownCustom(cmd, out, func(name string) string {
29+
base := strings.TrimSuffix(name, path.Ext(name))
30+
return "/cli/" + strings.ToLower(base) + "/"
31+
})
32+
if err != nil {
33+
panic(err)
34+
}
35+
36+
// Define the text to be replaced and the replacement text
37+
oldText := []byte("## " + title)
38+
newText := []byte("---\ntitle: " + title + "\n---")
39+
40+
// Perform the replacement on the buffer's content
41+
updatedContent := bytes.Replace(out.Bytes(), oldText, newText, 1)
42+
43+
// Reset the buffer and write the updated content back to it
44+
out.Reset()
45+
out.Write(updatedContent)
46+
47+
// write markdown to file
48+
file, err := os.Create("./docs/" + strings.ReplaceAll(title, " ", "_") + ".md")
49+
if err != nil {
50+
panic(err)
51+
}
52+
53+
_, err = file.Write(out.Bytes())
54+
if err != nil {
55+
panic(err)
56+
}
57+
58+
defer file.Close()
59+
60+
// if command has subcommands, generate markdown for each subcommand
61+
if cmd.HasSubCommands() {
62+
for _, c := range cmd.Commands() {
63+
// if c.Use starts with "help", skip it
64+
if c.Name() == "help" {
65+
continue
66+
}
67+
generateDocs(c, title+" "+c.Name())
68+
}
69+
}
70+
}

cli/generate_docs_default.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build !docs
2+
3+
package cli
4+
5+
import "github.com/spf13/cobra"
6+
7+
const DispatchCmdLong = `Welcome to Dispatch!
8+
9+
To get started, use the login command to authenticate with Dispatch or create an account.
10+
11+
Documentation: https://docs.dispatch.run
12+
Discord: https://dispatch.run/discord
13+
14+
`
15+
16+
const RunExampleText = " dispatch run [options] -- <command>"
17+
18+
func generateDocs(_ *cobra.Command, _ string) {
19+
// do nothing if the build tag "docs" is not set
20+
}

cli/main.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
var (
10-
DispatchCmdLong = `Welcome to Dispatch!
11-
12-
To get started, use the login command to authenticate with Dispatch or create an account.
13-
14-
Documentation: https://docs.dispatch.run
15-
Discord: https://dispatch.run/discord
16-
17-
`
18-
)
19-
209
func createMainCommand() *cobra.Command {
2110
cmd := &cobra.Command{
2211
Version: version(),
2312
Use: "dispatch",
2413
Long: DispatchCmdLong,
14+
Short: "Main command for Dispatch CLI",
2515
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
2616
return loadEnvFromFile(DotEnvFilePath)
2717
},
@@ -50,6 +40,9 @@ func createMainCommand() *cobra.Command {
5040
cmd.AddCommand(runCommand())
5141
cmd.AddCommand(versionCommand())
5242

43+
// Generate markdown documentation
44+
generateDocs(cmd, "dispatch")
45+
5346
return cmd
5447
}
5548

cli/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func runCommand() *cobra.Command {
6565
The command to start the local application endpoint should be
6666
specified after the run command and its options:
6767
68-
dispatch run [options] -- <command>
68+
`+RunExampleText+`
6969
7070
Dispatch spawns the local application endpoint and then dispatches
7171
function calls to it continuously.

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/charmbracelet/lipgloss v0.9.1
1010
github.com/joho/godotenv v1.5.1
1111
github.com/muesli/reflow v0.3.0
12+
github.com/muesli/termenv v0.15.2
1213
github.com/nlpodyssey/gopickle v0.3.0
1314
github.com/pelletier/go-toml/v2 v2.2.0
1415
github.com/spf13/cobra v1.8.0
@@ -22,6 +23,7 @@ require (
2223
github.com/atotto/clipboard v0.1.4 // indirect
2324
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
2425
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
26+
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
2527
github.com/davecgh/go-spew v1.1.1 // indirect
2628
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2729
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
@@ -30,9 +32,9 @@ require (
3032
github.com/mattn/go-runewidth v0.0.15 // indirect
3133
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
3234
github.com/muesli/cancelreader v0.2.2 // indirect
33-
github.com/muesli/termenv v0.15.2 // indirect
3435
github.com/pmezard/go-difflib v1.0.0 // indirect
3536
github.com/rivo/uniseg v0.4.6 // indirect
37+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
3638
github.com/spf13/pflag v1.0.5 // indirect
3739
golang.org/x/sync v0.1.0 // indirect
3840
golang.org/x/sys v0.19.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1
1414
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
1515
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
1616
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
17+
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
1718
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1819
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1920
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -51,6 +52,7 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
5152
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
5253
github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=
5354
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
55+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
5456
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5557
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
5658
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=

0 commit comments

Comments
 (0)