2023-01-05 20:51:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
"go.elara.ws/go-lemmy/cmd/gen/extractor"
|
2023-04-21 02:27:38 +00:00
|
|
|
"go.elara.ws/go-lemmy/cmd/gen/generator"
|
|
|
|
"go.elara.ws/logger"
|
|
|
|
"go.elara.ws/logger/log"
|
2023-01-05 20:51:45 +00:00
|
|
|
)
|
|
|
|
|
2023-01-07 23:25:18 +00:00
|
|
|
func init() {
|
|
|
|
log.Logger = logger.NewPretty(os.Stderr)
|
|
|
|
}
|
|
|
|
|
2023-01-05 20:51:45 +00:00
|
|
|
func main() {
|
2023-09-25 02:28:46 +00:00
|
|
|
jsonFile := flag.String("json-file", "lemmy.json", "Path to the JSON file generated from lemmy's docs")
|
2023-01-05 20:51:45 +00:00
|
|
|
outDir := flag.String("out-dir", "out", "Directory to write output in")
|
|
|
|
flag.Parse()
|
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
e, err := extractor.New(*jsonFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error creating extractor").Err(err).Send()
|
2023-01-05 20:51:45 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
routes := e.Routes()
|
|
|
|
structs := e.Structs(routes)
|
2023-01-05 20:51:45 +00:00
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
err = os.MkdirAll(filepath.Join(*outDir, "types"), 0o755)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error creating types directory").Err(err).Send()
|
2023-01-05 20:51:45 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
otf, err := os.Create(filepath.Join(*outDir, "types/types.gen.go"))
|
2023-01-05 20:51:45 +00:00
|
|
|
if err != nil {
|
2023-09-25 02:28:46 +00:00
|
|
|
log.Fatal("Error creating types output file").Err(err).Send()
|
2023-01-05 20:51:45 +00:00
|
|
|
}
|
2023-09-25 02:28:46 +00:00
|
|
|
defer otf.Close()
|
2023-01-05 20:51:45 +00:00
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
err = generator.NewStruct(otf, "types").Generate(structs)
|
2023-01-05 20:51:45 +00:00
|
|
|
if err != nil {
|
2023-09-25 02:28:46 +00:00
|
|
|
log.Fatal("Error generating output routes file").Err(err).Send()
|
2023-01-05 20:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orf, err := os.Create(filepath.Join(*outDir, "routes.gen.go"))
|
|
|
|
if err != nil {
|
2023-01-07 23:25:18 +00:00
|
|
|
log.Fatal("Error creating routes output file").Err(err).Send()
|
2023-01-05 20:51:45 +00:00
|
|
|
}
|
|
|
|
defer orf.Close()
|
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
err = generator.NewRoutes(orf, "lemmy").Generate(routes)
|
2023-01-05 20:51:45 +00:00
|
|
|
if err != nil {
|
2023-01-07 23:25:18 +00:00
|
|
|
log.Fatal("Error generating output routes file").Err(err).Send()
|
2023-01-05 20:51:45 +00:00
|
|
|
}
|
|
|
|
}
|