go-tdlib/cmd/generate-json.go

65 lines
1.4 KiB
Go
Raw Normal View History

2021-10-22 16:24:12 +02:00
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"os"
"path/filepath"
"strings"
2021-10-22 16:31:26 +02:00
"git.zio.sh/astra/go-tdlib/tlparser"
2021-10-22 16:24:12 +02:00
)
func main() {
var version string
var outputFilePath string
flag.StringVar(&version, "version", "", "TDLib version")
flag.StringVar(&outputFilePath, "output", "./td_api.json", "json schema file")
flag.Parse()
resp, err := http.Get("https://raw.githubusercontent.com/tdlib/td/" + version + "/td/generate/scheme/td_api.tl")
if err != nil {
log.Fatalf("http.Get error: %s", err)
}
defer resp.Body.Close()
schema, err := tlparser.Parse(resp.Body)
if err != nil {
log.Fatalf("schema parse error: %s", err)
}
resp, err = http.Get("https://raw.githubusercontent.com/tdlib/td/" + version + "/td/telegram/Td.cpp")
if err != nil {
log.Fatalf("http.Get error: %s", err)
}
defer resp.Body.Close()
err = tlparser.ParseCode(resp.Body, schema)
if err != nil {
log.Fatalf("parse code error: %s", err)
}
err = os.MkdirAll(filepath.Dir(outputFilePath), os.ModePerm)
if err != nil {
log.Fatalf("make dir error: %s", filepath.Dir(outputFilePath))
}
2024-10-06 19:09:53 +02:00
file, err := os.Create(outputFilePath)
2021-10-22 16:24:12 +02:00
if err != nil {
log.Fatalf("open file error: %s", err)
}
2024-10-06 19:09:53 +02:00
defer file.Close()
2021-10-22 16:24:12 +02:00
2024-10-06 19:09:53 +02:00
enc := json.NewEncoder(file)
enc.SetIndent("", strings.Repeat(" ", 4))
err = enc.Encode(schema)
2021-10-22 16:24:12 +02:00
if err != nil {
2024-10-06 19:09:53 +02:00
log.Fatalf("enc.Encode error: %s", err)
2021-10-22 16:24:12 +02:00
}
}