go-tdlib/cmd/generate-json.go

68 lines
1.6 KiB
Go
Raw Normal View History

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