go-tdlib/cmd/generate-json.go

68 lines
1.5 KiB
Go
Raw Permalink Normal View History

2018-08-30 14:55:42 +00:00
package main
import (
2018-10-23 12:49:10 +00:00
"bufio"
"encoding/json"
"flag"
2022-01-26 20:48:37 +00:00
"github.com/c0re100/gotdlib/tlparser"
2018-10-23 12:49:10 +00:00
"log"
"net/http"
"os"
"path/filepath"
"strings"
2018-08-30 14:55:42 +00:00
)
func main() {
2018-10-23 12:49:10 +00:00
var version string
var outputFilePath string
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
flag.StringVar(&version, "version", "", "TDLib version")
flag.StringVar(&outputFilePath, "output", "./td_api.json", "json schema file")
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
flag.Parse()
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
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)
return
}
defer resp.Body.Close()
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
schema, err := tlparser.Parse(resp.Body)
if err != nil {
log.Fatalf("schema parse error: %s", err)
return
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
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)
return
}
defer resp.Body.Close()
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
err = tlparser.ParseCode(resp.Body, schema)
if err != nil {
log.Fatalf("parse code error: %s", err)
return
}
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
err = os.MkdirAll(filepath.Dir(outputFilePath), os.ModePerm)
if err != nil {
log.Fatalf("make dir error: %s", filepath.Dir(outputFilePath))
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
file, err := os.OpenFile(outputFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
if err != nil {
log.Fatalf("open file error: %s", err)
return
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
data, err := json.MarshalIndent(schema, "", strings.Repeat(" ", 4))
if err != nil {
log.Fatalf("json marshal error: %s", err)
return
}
bufio.NewWriter(file).Write(data)
2018-08-30 14:55:42 +00:00
}