-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (75 loc) · 1.86 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// ridl - re-targetable IDL compiler
// Copyright © 2016 A.Newman.
//
// This file is licensed using the GNU Public License, version 2.
// See the file LICENSE for details.
//
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
)
const (
StdoutFilename = "-"
)
var (
templateNames = NewStringSlice()
templateDirs = NewStringSlice()
outputFilename = flag.String("o", "", "write output to `filename` (use '-' for stdout)")
debugFlag = flag.Bool("debug", false, "enable debug output")
dryRunFlag = flag.Bool("n", false, "do not generate output, only parse files")
)
func main() {
myname := filepath.Base(os.Args[0])
log.SetFlags(0)
log.SetPrefix(myname + ": ")
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "usage:", myname, "[options] path")
flag.PrintDefaults()
}
versionFlag := flag.Bool("version", false, "output version and exit")
flag.Var(templateNames, "t", "generate output using `template`")
flag.Var(templateDirs, "T", "search for templates in `dir`")
typeMapFlag := flag.String("typemap", "", "type mapping `filename`")
writeTypeMapFlag := flag.Bool("write-typemap", false, "output type mapping JSON and exit")
if s := os.Getenv("RIDLPATH"); s != "" {
*templateDirs = filepath.SplitList(s)
}
flag.Parse()
if *versionFlag {
fmt.Print(versionNumber)
os.Exit(0)
}
initTypeMap()
if *typeMapFlag != "" {
if err := readTypeMap(*typeMapFlag); err != nil {
log.Fatal(err)
}
}
if *writeTypeMapFlag {
writeTypeMap(os.Stdout)
os.Exit(0)
}
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
for _, path := range flag.Args() {
var err error
if isDir(path) {
err = ridlDir(path, templateNames.Slice())
} else {
err = ridlFile(path, templateNames.Slice())
}
if err != nil {
log.Fatal(err)
}
}
}
func isDir(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}