Newer
Older
package main
import (
"fmt"
"os"
"strings"
"text/template"
"github.com/eris-ltd/common/go/docs"
commands "github.com/eris-ltd/eris-db/cmd"
"github.com/eris-ltd/eris-db/version"
"github.com/spf13/cobra"
clientCommands "github.com/eris-ltd/eris-db/client/cmd"
)
// Repository maintainers should customize the next two lines.
var Description = "Blockchain Client" // should match the docs site name
19
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
81
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
}
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()
clientCommands.AddGlobalFlags()
// 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)
}