-
Notifications
You must be signed in to change notification settings - Fork 67
/
apigen.go
135 lines (109 loc) · 3.11 KB
/
apigen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2015-2021 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain
// rights in this software.
// apigen creates the api documentation by invoking minimega's JSON API output
// (-cli), and applying the data to a minidoc api template. It is expected to
// be invoked by the build script.
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"minicli"
log "minilog"
"os/exec"
"sort"
"strings"
"text/template"
"time"
)
var (
f_template = flag.String("template", "", "api generation template")
f_bin = flag.String("bin", "", "binary to extract json api doc from")
f_sections = flag.String("sections", "", "CSV of section names")
)
type apigen struct {
Date string
Sections map[string][]*minicli.Handler
}
func main() {
flag.Parse()
log.Init()
if *f_bin == "" {
log.Fatalln("must specify binary")
}
if *f_template == "" {
log.Fatalln("must specify template")
}
log.Debug("using binary: %v", *f_bin)
log.Debug("using doc template: %v", *f_template)
// invoke minimega and get the doc json
doc, err := exec.Command(*f_bin, "-cli").Output()
if err != nil {
log.Fatalln(err)
}
log.Debug("got doc: %v", string(doc))
// decode the JSON for our template
var handlers []*minicli.Handler
err = json.Unmarshal(doc, &handlers)
if err != nil {
log.Fatalln(err)
}
// populate the apigen date for the template
year, month, day := time.Now().Date()
api := apigen{
Date: fmt.Sprintf("%v %v %v", day, month, year),
Sections: map[string][]*minicli.Handler{},
}
// sort handlers
sort.Slice(handlers, func(i, j int) bool { return handlers[i].SharedPrefix < handlers[j].SharedPrefix })
// collapse handlers based on SharedPrefix
matched := 0
for i := range handlers {
j := i - matched
if j > 0 && handlers[j].SharedPrefix == handlers[j-1].SharedPrefix {
handlers[j-1].Patterns = append(handlers[j-1].Patterns, handlers[j].Patterns...)
handlers[j-1].HelpShort += "\n" + handlers[j].HelpShort
handlers[j-1].HelpLong += "\n" + handlers[j].HelpLong
// delete matched handler
handlers = append(handlers[:j], handlers[j+1:]...)
matched++
}
}
for _, s := range strings.Split(*f_sections, ",") {
matches := []*minicli.Handler{}
for i := range handlers {
j := i - len(matches)
p := handlers[j].SharedPrefix
if strings.HasPrefix(p, "clear") {
p = strings.TrimPrefix(p, "clear ")
}
if strings.HasPrefix(p, s) {
matches = append(matches, handlers[j])
// delete matched handler
handlers = append(handlers[:j], handlers[j+1:]...)
}
}
log.Info("found %v matches for section %v", len(matches), s)
if len(matches) == 0 {
log.Warn("no matches found for section: %v", s)
}
api.Sections[s] = matches
}
// add section for unmatched handlers
if len(handlers) > 0 {
api.Sections[""] = handlers
}
// run the template and print to stdout
var out bytes.Buffer
t, err := template.ParseFiles(*f_template)
if err != nil {
log.Fatalln(err)
}
err = t.Execute(&out, api)
if err != nil {
log.Fatalln(err)
}
fmt.Println(out.String())
}