tss/cmd/schemagen/main.go

62 lines
1.0 KiB
Go

package main
import (
"encoding/json"
"os"
"github.com/invopop/jsonschema"
"go.balki.me/tss/app"
"gopkg.in/yaml.v3"
)
var refsDoc = `Placeholder to put any yaml anchors to use elsewhere, E.g.
refs:
myvpsproxy: &myvpsproxy "socks5://vps.example.com:1080"
twiceaday: &twiceaday "00 1,13 * * *"`
type config struct {
app.Config
Refs map[string]any `yaml:"refs" jsonschema:"refs"`
}
func main() {
r := jsonschema.Reflector{
RequiredFromJSONSchemaTags: true,
DoNotReference: true,
ExpandedStruct: true,
}
err := r.AddGoComments("go.balki.me/tss", ".")
if err != nil {
panic(err)
}
r.CommentMap["main.config.Refs"] = refsDoc
s := r.Reflect(config{})
j, err := json.Marshal(s)
if err != nil {
panic(err)
}
err = os.WriteFile("./docs/schema.json", j, 0644)
if err != nil {
panic(err)
}
var a any
err = yaml.Unmarshal(j, &a)
if err != nil {
panic(err)
}
y, err := yaml.Marshal(a)
if err != nil {
panic(err)
}
err = os.WriteFile("./docs/schema.yaml", y, 0644)
if err != nil {
panic(err)
}
}