forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheql_main.go
131 lines (112 loc) · 2.97 KB
/
eql_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"fmt"
"os"
"strings"
"qlang.io/cl/interpreter"
"qlang.io/cl/qlang"
"qlang.io/exec"
"qlang.io/lib/eqlang"
"qlang.io/lib/qlang.all"
)
// -----------------------------------------------------------------------------
const usage = `
Usage:
eql <templatefile> [-i -j <jsoninput> -o <outputfile> --key1=value1 --key2=value2 ...]
eql <templatedir> [-i -j <jsoninput> -o <outputdir> --key1=value1 --key2=value2 ...]
<templatefile>: the template file to format.
<templatedir>: the template dir to format.
-i: use stdin as json input.
-j <jsoninput>: json input string.
-o <outputfile>: output result into this file, default is stdout.
-o <outputdir>: output result into this directory, default is <templatedir> format result.
--key=value: (key, value) pair that overrides json input specified by -i or -j <jsoninput>.
`
func main() {
qall.InitSafe(false)
qlang.Import("", interpreter.Exports)
qlang.SetDumpCode(os.Getenv("QLANG_DUMPCODE"))
libs := os.Getenv("QLANG_PATH")
if libs == "" {
libs = os.Getenv("HOME") + "/qlang"
}
lang := qlang.New()
lang.SetLibs(libs)
vars = lang.Context
eql := eqlang.New(lang)
parseFlags()
if source == "" {
fmt.Fprintln(os.Stderr, usage)
return
}
fi, err := os.Stat(source)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-2)
}
if fi.IsDir() {
global := lang.CopyVars()
err = eql.ExecuteDir(global, source, output)
} else {
err = eql.ExecuteFile(source, output)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-3)
}
}
// -----------------------------------------------------------------------------
var (
source string
output string
vars *exec.Context
)
func parseFlags() {
vars.SetVar("imports", "")
for i := 1; i < len(os.Args); i++ {
switch arg := os.Args[i]; arg {
case "-o":
if i+1 >= len(os.Args) {
fmt.Fprintln(os.Stderr, "ERROR: switch -o doesn't have parameters, please use -o <output>")
os.Exit(10)
}
output = os.Args[i+1]
i++
case "-i":
ret, err := eqlang.InputFile("-")
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: stdin isn't valid json - %v\n", err)
os.Exit(12)
}
vars.ResetVars(ret)
case "-j":
if i+1 >= len(os.Args) {
fmt.Fprintln(os.Stderr, "ERROR: switch -j doesn't have parameters, please use -j <jsoninput>")
os.Exit(10)
}
ret, err := eqlang.Input(os.Args[i+1])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: invalid parameter `-j <jsoninput>` - %v\n", err)
os.Exit(12)
}
vars.ResetVars(ret)
i++
default:
if strings.HasPrefix(arg, "--") {
kv := arg[2:]
pos := strings.Index(kv, "=")
if pos < 0 {
fmt.Fprintf(os.Stderr, "ERROR: invalid switch `%s`\n", arg)
os.Exit(11)
}
vars.SetVar(kv[:pos], kv[pos+1:])
} else if arg[0] == '-' {
fmt.Fprintf(os.Stderr, "ERROR: unknown switch `%s`\n", arg)
os.Exit(12)
} else {
source = arg
}
}
}
}
// -----------------------------------------------------------------------------