feat: initial commit

This commit is contained in:
sh0rez 2020-03-22 16:47:47 +01:00
commit 5977373772
No known key found for this signature in database
GPG key ID: 87C71DF9F8181FF1
6 changed files with 454 additions and 0 deletions

75
doc-util.libsonnet Normal file
View file

@ -0,0 +1,75 @@
local d = {
new(name, url): {
name: name,
'import': url,
},
withAPI(obj): {
api: obj.object,
},
withHelp(help): {
help: help,
},
obj: {
// obj creates an object, optionally with fields
new(help='', fields={}): { object: {
help: help,
fields: fields,
} },
},
Obj: self.obj.new,
// fn creates a function, optionally with arguments
fn: {
new(help='', args=[]): { 'function': {
help: help,
args: args,
} },
},
Fn: self.fn.new,
// arg creates a function argument of given name,
// type and optionally a default value
arg: {
new(name, type, default=null): {
name: name,
type: type,
default: default,
},
},
Arg: self.arg.new,
// T contains constants for the Jsonnet types
T: {
string: 'string',
number: 'number',
bool: 'bool',
object: 'object',
array: 'array',
any: 'any',
func: 'function',
},
};
local root = d.Obj('grafana.libsonnet is the offical Jsonnet library for Grafana', {
new: d.Fn('new returns Grafana resources with sane defaults'),
addConfig: d.Fn('addConfig adds config entries to grafana.ini', [
d.Arg('config', d.T.object),
]),
datasource: d.Obj('ds-util makes creating datasources easy', {
new: d.Fn('new creates a new datasource', [
d.Arg('name', d.T.string),
d.Arg('type', d.T.string),
]),
sheesh: d.Obj("sheesh is sheeshing around", {
shit: d.Fn("enough sheesh", [
d.Arg("lel", d.T.any),
d.Arg("lol", d.T.func),
])
})
}),
});
d.new('grafana', 'github.com/sh0rez/grafana.libsonnet')
+ d.withAPI(std.prune(root))
+ d.withHelp("`grafana.libsonnet` is the offical Jsonnet package for using Grafana with Kubernetes")

8
go.mod Normal file
View file

@ -0,0 +1,8 @@
module github.com/sh0rez/docsonnet
go 1.14
require (
github.com/Masterminds/sprig/v3 v3.0.2
github.com/google/go-cmp v0.4.0
)

42
go.sum Normal file
View file

@ -0,0 +1,42 @@
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.0.3 h1:znjIyLfpXEDQjOIEWh+ehwpTU14UzUPub3c3sm36u14=
github.com/Masterminds/semver/v3 v3.0.3/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/sprig/v3 v3.0.2 h1:wz22D0CiSctrliXiI9ZO3HoNApweeRGftyDN+BQa3B8=
github.com/Masterminds/sprig/v3 v3.0.2/go.mod h1:oesJ8kPONMONaZgtiHNzUShJbksypC5kWczhZAf6+aU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 h1:0hQKqeLdqlt5iIwVOBErRisrHJAN57yOiPRQItI20fU=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

120
main.go Normal file
View file

@ -0,0 +1,120 @@
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
)
type Doc struct {
Name string `json:"name"`
Import string `json:"import"`
Help string `json:"help"`
API Object `json:"api"`
}
func main() {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalln(err)
}
var d Doc
if err := json.Unmarshal(data, &d); err != nil {
log.Fatalln(err)
}
if _, err := render(d); err != nil {
log.Fatalln(err)
}
}
type Object struct {
Name string `json:"-"`
Help string `json:"help"`
// children
Fields Fields `json:"fields"`
}
type Fields map[string]Field
func (fPtr *Fields) UnmarshalJSON(data []byte) error {
if *fPtr == nil {
*fPtr = make(Fields)
}
f := *fPtr
tmp := make(map[string]Field)
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
for k, v := range tmp {
switch {
case v.Function != nil:
v.Function.Name = k
case v.Object != nil:
v.Object.Name = k
case v.Value != nil:
v.Value.Name = k
}
f[k] = v
}
return nil
}
// Field represents any field of an object.
type Field struct {
// Function value
Function *Function `json:"function,omitempty"`
// Object value
Object *Object `json:"object,omitempty"`
// Any other value
Value *Value `json:"value,omitempty"`
}
func (o Field) MarshalJSON() ([]byte, error) {
if o.Function == nil && o.Object == nil && o.Value == nil {
return nil, errors.New("field has no value")
}
type fake Field
return json.Marshal(fake(o))
}
type Function struct {
Name string `json:"-"`
Help string `json:"help"`
Args []Argument `json:"args,omitempty"`
}
type Type string
const (
TypeString = "string"
TypeNumber = "number"
TypeBool = "boolean"
TypeObject = "object"
TypeArray = "array"
TypeAny = "any"
TypeFunc = "function"
)
type Value struct {
Name string `json:"-"`
Help string `json:"help"`
Type Type `json:"type"`
}
type Argument struct {
Type Type `json:"type"`
Name string `json:"name"`
Default interface{} `json:"default"`
}

48
main_test.go Normal file
View file

@ -0,0 +1,48 @@
package main
import (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestRemarshal(t *testing.T) {
o := Object{
Help: "grafana.libsonnet is the offical Jsonnet library for Grafana",
Fields: map[string]Field{
"new": {Function: &Function{
Name: "new",
Help: "new returns Grafana resources with sane defaults",
}},
"addConfig": {Function: &Function{
Name: "addConfig",
Help: "addConfig adds config entries to grafana.ini",
}},
"datasource": {Object: &Object{
Name: "datasource",
Help: "ds-util makes creating datasources easy",
Fields: map[string]Field{
"new": {Function: &Function{
Name: "new",
Help: "new creates a new datasource",
}},
},
}},
},
}
data, err := json.Marshal(o)
if err != nil {
t.Fatal(err)
}
var got Object
if err := json.Unmarshal(data, &got); err != nil {
t.Fatal(err)
}
if str := cmp.Diff(o, got); str != "" {
t.Fatal(str)
}
}

161
render.go Normal file
View file

@ -0,0 +1,161 @@
package main
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
)
var indexTmpl = strings.Replace(`
# package {{.Name}}
´´´jsonnet
local {{.Name}} = import {{.Import}}
´´´
{{.Help}}
## Index
{{ range .Index }}{{ $l := mul .Level 2}}{{repeat (int $l) " "}}* ´{{ .Line }}´
{{ end }}
{{ range .Fields }} {{.Render}}
{{ end }}
`, "´", "`", -1)
type Renderable interface {
Render() string
}
var objTmpl = strings.Replace(`
## {{ .Name }}
{{ .Help }}
`, "´", "`", -1)
type obj Object
func (o obj) Render() string {
t := template.Must(template.New("").Parse(objTmpl))
var buf bytes.Buffer
if err := t.Execute(&buf, o); err != nil {
panic(err)
}
return buf.String()
}
type fn Function
func (f fn) Params() string {
args := make([]string, 0, len(f.Args))
for _, a := range f.Args {
arg := a.Name
if a.Default != nil {
arg = fmt.Sprint("%s=%v", arg, a.Default)
}
args = append(args, arg)
}
return strings.Join(args, ", ")
}
func (f fn) Signature() string {
return fmt.Sprintf("%s(%s)", f.Name, f.Params())
}
var fnTmpl = strings.Replace(`
### fn {{ .Name }}
´´´
{{ .Signature }}
´´´
{{ .Help }}
`, "´", "`", -1)
func (f fn) Render() string {
t := template.Must(template.New("").Parse(fnTmpl))
var buf bytes.Buffer
if err := t.Execute(&buf, f); err != nil {
panic(err)
}
return buf.String()
}
func render(d Doc) (string, error) {
tmpl := template.Must(template.New("").Funcs(sprig.TxtFuncMap()).Parse(indexTmpl))
if err := tmpl.Execute(os.Stdout, &doc{
Name: d.Name,
Import: d.Import,
Help: d.Help,
Index: buildIndex(d.API.Fields, 0),
Fields: renderables(d.API.Fields, ""),
}); err != nil {
return "", err
}
return "", nil
}
type doc struct {
Name string
Import string
Help string
Index []indexElem
Fields []Renderable
}
type indexElem struct {
Line string
Level int
}
func renderables(fields map[string]Field, prefix string) []Renderable {
rs := []Renderable{}
for _, f := range fields {
switch {
case f.Function != nil:
fnc := fn(*f.Function)
fnc.Name = strings.TrimPrefix(prefix+"."+fnc.Name, ".")
rs = append(rs, fnc)
case f.Object != nil:
o := obj(*f.Object)
o.Name = strings.TrimPrefix(prefix+"."+o.Name, ".")
rs = append(rs, o)
childs := renderables(o.Fields, o.Name)
rs = append(rs, childs...)
}
}
return rs
}
func buildIndex(fields map[string]Field, level int) []indexElem {
elems := []indexElem{}
for _, f := range fields {
line := indexLine(f)
elems = append(elems, indexElem{
Line: line,
Level: level,
})
if f.Object != nil {
childs := buildIndex(f.Object.Fields, level+1)
elems = append(elems, childs...)
}
}
return elems
}
func indexLine(f Field) string {
switch {
case f.Function != nil:
return "fn " + fn(*f.Function).Signature()
case f.Object != nil:
return fmt.Sprintf("obj %s", f.Object.Name)
}
panic("wtf")
}