-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
64 lines (51 loc) · 1.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"context"
"encoding/json"
"flag"
"log"
"os"
"github.com/dipdup-net/go-lib/tzkt/api"
"github.com/dipdup-net/go-lib/tzkt/data"
"github.com/go-playground/validator/v10"
)
type args struct {
Contract string `validate:"required_with=URL,len=36"`
URL string `validate:"url,required_with=Contract"`
Name string `validate:"omitempty"`
Output string `validate:"omitempty,dir"`
File string `validate:"required_without=URL"`
}
func main() {
var args args
flag.StringVar(&args.Contract, "c", "", "Contract address `KT1...`")
flag.StringVar(&args.URL, "u", "https://api.tzkt.io/", "TzKT base URL")
flag.StringVar(&args.Name, "n", "my_contract", "Contract name")
flag.StringVar(&args.Output, "o", "", "Output directory")
flag.StringVar(&args.File, "f", "", "Path to JSON schema file")
flag.Parse()
if err := validator.New().Struct(args); err != nil {
log.Panic(err)
}
var response data.ContractJSONSchema
var err error
if args.File == "" {
tzkt := api.New(args.URL)
response, err = tzkt.GetContractJSONSchema(context.Background(), args.Contract)
if err != nil {
log.Panic(err)
}
} else {
file, err := os.Open(args.File)
if err != nil {
log.Panic(err)
}
defer file.Close()
if err := json.NewDecoder(file).Decode(&response); err != nil {
log.Panic(err)
}
}
if err := Generate(response, args.Name, args.Contract, args.Output); err != nil {
log.Panic(err)
}
}