forked from grafana/dashboard-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (155 loc) · 3.77 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"text/template"
"github.com/go-openapi/inflect"
)
type Inflection func(string) string
// Metadata for a supported language.
type Language struct {
Directory string
FileExtension string
FileNameInflection Inflection
OjectInflection Inflection
}
func main() {
args := os.Args[1:]
specVersion, language := args[0], args[1]
err := generate(
loadSpec(path.Join("_gen", specVersion, "spec.json")),
loadLanguage(language),
)
if err != nil {
log.Fatalln(err)
}
}
func loadSpec(file string) (s Spec) {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalln(err)
}
if err := json.Unmarshal(data, &s); err != nil {
log.Fatalln(err)
}
return
}
func loadLanguage(l string) Language {
languages := map[string]Language{
"jsonnet": {
Directory: "jsonnet",
FileExtension: "libsonnet",
FileNameInflection: inflect.CamelizeDownFirst,
OjectInflection: inflect.CamelizeDownFirst,
},
}
lang, ok := languages[l]
if !ok {
log.Fatalf("%q is not a supported language.", l)
}
return lang
}
func generate(s Spec, l Language) error {
// Create directories.
dir := path.Join("_gen", s.Info.Version, l.Directory)
os.MkdirAll(dir, os.ModePerm)
for _, d := range []string{"panel", "target", "template"} {
os.MkdirAll(path.Join(dir, d), os.ModePerm)
}
// Function that renders templates and writes them to files.
g := func(name string, tmplType string, data interface{}) error {
tmplFile := fmt.Sprintf("%s.tmpl", tmplType)
tmpl, err := template.New(tmplFile).Funcs(
template.FuncMap{
"objectInflection": l.OjectInflection,
"singularize": inflect.Singularize,
"add": func(x int, y int) int {
return x + y
},
"subtract": func(x int, y int) int {
return x - y
},
"repeat": func(s string, n int) string {
return strings.Repeat(s, n)
},
},
).ParseFiles(
path.Join("templates", l.Directory, tmplFile),
path.Join("templates", l.Directory, "_shared.tmpl"),
)
if err != nil {
return err
}
fileName := fmt.Sprintf("%s.%s", l.FileNameInflection(name), l.FileExtension)
dest := dir
if tmplType != "dashboard" && tmplType != "main" {
dest = path.Join(dir, tmplType)
}
f, err := os.Create(path.Join(dest, fileName))
if err != nil {
return err
}
return tmpl.Execute(f, data)
}
// Generate dashboard file.
err := g("dashboard", "dashboard", s.Components.Schemas["Dashboard"])
if err != nil {
return err
}
// Generate panel files.
for n, sc := range s.Components.Schemas["Panel"].Properties {
err = g(n, "panel", *sc)
if err != nil {
return err
}
}
// Generate target files.
for n, sc := range s.Components.Schemas["Target"].Properties {
err = g(n, "target", *sc)
if err != nil {
return err
}
}
// Generate template files.
for n, sc := range s.Components.Schemas["Template"].Properties {
err = g(n, "template", *sc)
if err != nil {
return err
}
}
// Generate main.
err = g("grafana", "main", s.Components.Schemas)
if err != nil {
return err
}
// Generate docs.
tmpl, err := template.New("docs.tmpl").Funcs(
template.FuncMap{
"objectInflection": l.OjectInflection,
"singularize": inflect.Singularize,
"inflectJoin": func(elems ...string) (s string) {
for i, e := range elems {
elems[i] = l.OjectInflection(e)
}
return strings.Join(elems, ".")
},
"indent": func(spaces int, s string) string {
pad := strings.Repeat(" ", spaces)
return pad + strings.Replace(s, "\n", "\n"+pad, -1)
},
},
).ParseFiles(path.Join("templates", "docs.tmpl"))
if err != nil {
return err
}
f, err := os.Create(path.Join(dir, "DOCS.md"))
if err != nil {
return err
}
return tmpl.Execute(f, s.Components.Schemas)
}