forked from andyleap/gencode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (55 loc) · 1.31 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
65
66
67
68
69
70
71
72
73
74
75
// gencode project main.go
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"github.com/andyleap/gencode/schema"
"github.com/kr/pretty"
_ "github.com/andyleap/gencode/backends/golang"
)
func main() {
if len(os.Args) == 1 {
os.Exit(1)
}
backend, ok := schema.Backends[os.Args[1]]
if !ok {
fmt.Fprintln(os.Stderr, "No such backend, available backends are:")
for name := range schema.Backends {
fmt.Fprintln(os.Stderr, name)
}
os.Exit(1)
}
flags := backend.Flags()
SchemaFile := ""
Debug := false
flags.StringVar(&SchemaFile, "schema", "", "Schema file to process")
flags.BoolVar(&Debug, "debug", false, "Pretty print the resulting schema defs")
flags.Parse(os.Args[2:])
if SchemaFile == "" {
log.Fatal("No file specified")
}
file, err := os.Open(SchemaFile)
if err != nil {
log.Fatalf("Error opening schema file: %s", err)
}
if file == nil {
log.Fatalf("error opening file %s", file)
}
s, err := schema.ParseSchema(file)
if Debug {
pretty.Print(s)
}
if err != nil {
log.Fatalf("Error parsing schema: %s", err)
}
code, err := backend.Generate(s)
if err != nil {
log.Fatalf("Error generating output: %s", err)
}
err = ioutil.WriteFile(backend.GeneratedFilename(SchemaFile), []byte(code), 0666)
if err != nil {
log.Fatalf("Error writing output file: %s", err)
}
}