Newer
Older
"os"
"strings"
"text/template"
"github.com/eris-ltd/common/go/docs"
commands "github.com/eris-ltd/eris-db/cmd"
clientCommands "github.com/eris-ltd/eris-db/client/cmd"
"github.com/eris-ltd/eris-db/version"
"github.com/spf13/cobra"
)
// Repository maintainers should customize the next two lines.
var Description = "Blockchain Client" // should match the docs site name
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var RenderDir = fmt.Sprintf("./docs/documentation/db/%s/", version.VERSION) // should be the "shortversion..."
// The below variables should be updated only if necessary.
var Specs = []*docs.Entry{}
var Examples = []*docs.Entry{}
var SpecsDir = "./docs/specs"
var ExamplesDir = "./docs/examples"
type Cmd struct {
Command *cobra.Command
Entry *docs.Entry
Description string
}
func RenderFiles(cmdRaw *cobra.Command, tmpl *template.Template) error {
this_entry := &docs.Entry{
Title: cmdRaw.CommandPath(),
Specifications: Specs,
Examples: Examples,
BaseURL: strings.Replace(RenderDir, ".", "", 1),
Template: tmpl,
FileName: docs.GenerateFileName(RenderDir, cmdRaw.CommandPath()),
}
cmd := &Cmd{
Command: cmdRaw,
Entry: this_entry,
Description: Description,
}
for _, command := range cmd.Command.Commands() {
RenderFiles(command, tmpl)
}
if !cmd.Command.HasParent() {
entries := append(cmd.Entry.Specifications, cmd.Entry.Examples...)
for _, entry := range entries {
entry.Specifications = cmd.Entry.Specifications
entry.Examples = cmd.Entry.Examples
entry.CmdEntryPoint = cmd.Entry.Title
entry.BaseURL = cmd.Entry.BaseURL
if err := docs.RenderEntry(entry); err != nil {
return err
}
}
}
outFile, err := os.Create(cmd.Entry.FileName)
if err != nil {
return err
}
defer outFile.Close()
err = cmd.Entry.Template.Execute(outFile, cmd)
if err != nil {
return err
}
return nil
}
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
func AddClientToDB(dbCmd, clientCmd *cobra.Command) error {
// formulate the filenames properly
dbFile := docs.GenerateFileName(RenderDir, dbCmd.CommandPath())
clFile := docs.GenerateFileName(RenderDir, clientCmd.CommandPath())
// get the manual additions sorted
dbAdditions := []byte(fmt.Sprintf("\n# Related Commands\n\n* [%s](%s)", "Eris Client", docs.GenerateURLFromFileName(clFile)))
clAdditions := []byte(fmt.Sprintf("\n# Related Commands\n\n* [%s](%s)", "Eris DB", docs.GenerateURLFromFileName(dbFile)))
// read and write the db file
dbTxt, err := ioutil.ReadFile(dbFile)
if err != nil {
return err
}
dbTxt = append(dbTxt, dbAdditions...)
err = ioutil.WriteFile(dbFile, dbTxt, 0644)
if err != nil {
return err
}
// read and write the client file
clTxt, err := ioutil.ReadFile(clFile)
if err != nil {
return err
}
clTxt = append(clTxt, clAdditions...)
err = ioutil.WriteFile(clFile, clTxt, 0644)
if err != nil {
return err
}
return nil
}
func main() {
// Repository maintainers should populate the top level command object.
erisDbCommand := commands.ErisDbCmd
commands.InitErisDbCli()
commands.AddCommands()
commands.AddGlobalFlags()
erisClientCommand := clientCommands.ErisClientCmd
clientCommands.InitErisClientInit()
clientCommands.AddClientCommands()
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Make the proper directory.
var err error
if _, err = os.Stat(RenderDir); os.IsNotExist(err) {
err = os.MkdirAll(RenderDir, 0755)
if err != nil {
panic(err)
}
}
// Generate specs and examples files.
Specs, err = docs.GenerateEntries(SpecsDir, (RenderDir + "specifications/"), Description)
if err != nil {
panic(err)
}
Examples, err = docs.GenerateEntries(ExamplesDir, (RenderDir + "examples/"), Description)
if err != nil {
panic(err)
}
// Get template from docs generator.
tmpl, err := docs.GenerateCommandsTemplate()
if err != nil {
panic(err)
}
// Render the templates.
if err = RenderFiles(erisDbCommand, tmpl); err != nil {
if err = RenderFiles(erisClientCommand, tmpl); err != nil {
panic(err)
}
if err = AddClientToDB(erisDbCommand, erisClientCommand); err != nil {
panic(err)
}