Skip to content

Commit 8572871

Browse files
committed
Cleanup RPC doc generating script
1 parent 7625ce8 commit 8572871

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

contrib/doc-gen/generate.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@
44
// (1) install golang
55
// (2) install bitcoin core, set it up to use regtest
66
// (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
7+
// (4) run this script with `go run generate.go` while being in contrib/doc-gen, and with bitcoin-cli in PATH
8+
// (5) add the generated files to git
109
package main
1110

1211
import (
12+
"encoding/json"
1313
"fmt"
14-
"text/template"
1514
"io"
1615
"log"
1716
"os"
1817
"os/exec"
1918
"strings"
19+
"text/template"
2020
)
2121

22-
const BITCOIN_PATH = "/home/g/dev/bitcoind/bitcoin-0.16.0/bin/bitcoin-cli"
23-
const VERSION = "0.16.0"
22+
const BITCOIN_COMMAND = "bitcoin-cli"
2423

2524
type Command struct {
2625
Name string
@@ -41,7 +40,23 @@ type CommandData struct {
4140
Permalink string
4241
}
4342

43+
func getVersion() string {
44+
allInfo := run("getnetworkinfo")
45+
var f interface{}
46+
err := json.Unmarshal([]byte(allInfo), &f)
47+
if err != nil {
48+
panic("Cannot read network info as JSON")
49+
}
50+
m := f.(map[string]interface{})
51+
52+
numv := int(m["version"].(float64))
53+
v := fmt.Sprintf("%d.%d.%d", numv/1000000, (numv/10000)%100, (numv/100)%100)
54+
return v
55+
}
56+
4457
func main() {
58+
version := getVersion()
59+
4560
first := run("help")
4661
split := strings.Split(first, "\n")
4762

@@ -85,17 +100,17 @@ func main() {
85100

86101
for _, group := range groups {
87102
groupname := group.Name
88-
dirname := fmt.Sprintf("../../_doc/en/%s/rpc/%s/", VERSION, groupname)
103+
dirname := fmt.Sprintf("../../_doc/en/%s/rpc/%s/", version, groupname)
89104
err := os.MkdirAll(dirname, 0777)
90105
if err != nil {
91106
log.Fatalf("Cannot make directory %s: %s", dirname, err.Error())
92107
}
93108
for _, command := range group.Commands {
94109
name := command.Name
95110
address := fmt.Sprintf("%s%s.html", dirname, name)
96-
permalink := fmt.Sprintf("en/doc/%s/rpc/%s/%s/", VERSION, groupname, name)
111+
permalink := fmt.Sprintf("en/doc/%s/rpc/%s/%s/", version, groupname, name)
97112
err = tmpl.Execute(open(address), CommandData{
98-
Version: VERSION,
113+
Version: version,
99114
Name: name,
100115
Description: command.Description,
101116
Group: groupname,
@@ -105,10 +120,10 @@ func main() {
105120
log.Fatalf("Cannot make command file %s: %s", name, err.Error())
106121
}
107122
}
108-
address := fmt.Sprintf("../../_doc/en/%s/rpc/index.html", VERSION)
109-
permalink := fmt.Sprintf("en/doc/%s/rpc/", VERSION)
123+
address := fmt.Sprintf("../../_doc/en/%s/rpc/index.html", version)
124+
permalink := fmt.Sprintf("en/doc/%s/rpc/", version)
110125
err = tmpl.Execute(open(address), CommandData{
111-
Version: VERSION,
126+
Version: version,
112127
Name: "rpcindex",
113128
Description: "",
114129
Group: "index",
@@ -118,10 +133,10 @@ func main() {
118133
log.Fatalf("Cannot make index file: %s", err.Error())
119134
}
120135

121-
address = fmt.Sprintf("../../_doc/en/%s/index.html", VERSION)
122-
permalink = fmt.Sprintf("en/doc/%s/", VERSION)
136+
address = fmt.Sprintf("../../_doc/en/%s/index.html", version)
137+
permalink = fmt.Sprintf("en/doc/%s/", version)
123138
err = tmpl.Execute(open(address), CommandData{
124-
Version: VERSION,
139+
Version: version,
125140
Name: "index",
126141
Description: "",
127142
Group: "index",
@@ -143,7 +158,7 @@ func open(path string) io.Writer {
143158
}
144159

145160
func run(args ...string) string {
146-
out, err := exec.Command(BITCOIN_PATH, args...).CombinedOutput()
161+
out, err := exec.Command(BITCOIN_COMMAND, args...).CombinedOutput()
147162
if err != nil {
148163
log.Fatalf("Cannot run bitcoin-cli: %s, is bitcoind running?", err.Error())
149164
}

0 commit comments

Comments
 (0)