Skip to content

Commit 37c1494

Browse files
committed
Golang script for generating RPC docs
1 parent ef3cf45 commit 37c1494

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

contrib/doc-gen/command-template.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
name: {{.Name}}
3+
btcversion: {{.Version}}
4+
btcgroup: {{.Group}}
5+
permalink: {{.Permalink}}
6+
---
7+
8+
{{.Description}}

contrib/doc-gen/generate.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// This is a golang script, needed for generating the RPC bitcoin documentation
2+
//
3+
// What is necessary to run this:
4+
// (1) install golang
5+
// (2) install bitcoin core, set it up to use regtest
6+
// (3) run bitcoind
7+
// (4) change BITCOIN_PATH to actual bitcoin-cli directory path, change VERSION
8+
// (5) run this script with `go run generate.go` while being in contrib/doc-gen
9+
// (6) add the generated files to git
10+
package main
11+
12+
import (
13+
"fmt"
14+
"text/template"
15+
"io"
16+
"log"
17+
"os"
18+
"os/exec"
19+
"strings"
20+
)
21+
22+
const BITCOIN_PATH = "/home/g/dev/bitcoind/bitcoin-0.16.0/bin/bitcoin-cli"
23+
const VERSION = "0.16.0"
24+
25+
type Command struct {
26+
Name string
27+
Description string
28+
}
29+
30+
type Group struct {
31+
Index int
32+
Name string
33+
Commands []Command
34+
}
35+
36+
type CommandData struct {
37+
Version string
38+
Name string
39+
Description string
40+
Group string
41+
Permalink string
42+
}
43+
44+
func main() {
45+
first := run("help")
46+
split := strings.Split(first, "\n")
47+
48+
groups := make([]Group, 0)
49+
commands := make([]Command, 0)
50+
lastGroupName := ""
51+
52+
for _, line := range split {
53+
if len(line) > 0 {
54+
if strings.HasPrefix(line, "== ") {
55+
if len(commands) != 0 {
56+
g := Group{
57+
Name: lastGroupName,
58+
Commands: commands,
59+
Index: len(groups),
60+
}
61+
groups = append(groups, g)
62+
commands = make([]Command, 0)
63+
}
64+
lastGroupName = strings.ToLower(line[3 : len(line)-3])
65+
} else {
66+
name := strings.Split(line, " ")[0]
67+
desc := run("help", name)
68+
comm := Command{
69+
Name: name,
70+
Description: desc,
71+
}
72+
commands = append(commands, comm)
73+
}
74+
}
75+
}
76+
77+
g := Group{
78+
Name: lastGroupName,
79+
Commands: commands,
80+
Index: len(groups),
81+
}
82+
groups = append(groups, g)
83+
84+
tmpl := template.Must(template.ParseFiles("command-template.html"))
85+
86+
for _, group := range groups {
87+
groupname := group.Name
88+
dirname := fmt.Sprintf("../../_doc/en/%s/rpc/%s/", VERSION, groupname)
89+
err := os.MkdirAll(dirname, 0777)
90+
if err != nil {
91+
log.Fatalf("Cannot make directory %s: %s", dirname, err.Error())
92+
}
93+
for _, command := range group.Commands {
94+
name := command.Name
95+
address := fmt.Sprintf("%s%s.html", dirname, name)
96+
permalink := fmt.Sprintf("en/doc/%s/rpc/%s/%s/", VERSION, groupname, name)
97+
err = tmpl.Execute(open(address), CommandData{
98+
Version: VERSION,
99+
Name: name,
100+
Description: command.Description,
101+
Group: groupname,
102+
Permalink: permalink,
103+
})
104+
if err != nil {
105+
log.Fatalf("Cannot make command file %s: %s", name, err.Error())
106+
}
107+
}
108+
}
109+
}
110+
111+
func open(path string) io.Writer {
112+
f, err := os.Create(path)
113+
// not closing, program will close sooner
114+
if err != nil {
115+
log.Fatalf("Cannot open file %s: %s", path, err.Error())
116+
}
117+
return f
118+
}
119+
120+
func run(args ...string) string {
121+
out, err := exec.Command(BITCOIN_PATH, args...).CombinedOutput()
122+
if err != nil {
123+
log.Fatalf("Cannot run bitcoin-cli: %s, is bitcoind running?", err.Error())
124+
}
125+
126+
return string(out)
127+
}

0 commit comments

Comments
 (0)