Skip to content

Commit b190da6

Browse files
author
Dean Karn
committed
create binary
1 parent c1a8634 commit b190da6

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ksql
1111

1212
#### How to install CLI
1313
```shell
14-
~ go install github.com/go-playground/ksql
14+
~ go install github.com/go-playground/ksql/cmd/ksql
1515
```
1616

1717
#### Usage

cmd/ksql/main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
8+
"github.com/go-playground/ksql"
9+
)
10+
11+
func main() {
12+
args := os.Args[1:]
13+
isPipe := isInputFromPipe()
14+
if (len(args) < 2 && !isPipe) || (len(args) < 1 && isPipe) {
15+
usage()
16+
return
17+
}
18+
19+
ex, err := ksql.Parse([]byte(args[0]))
20+
if err != nil {
21+
usage()
22+
return
23+
}
24+
25+
var input []byte
26+
27+
if isPipe {
28+
input, err = ioutil.ReadAll(os.Stdin)
29+
if err != nil {
30+
usage()
31+
return
32+
}
33+
} else {
34+
input = []byte(args[1])
35+
}
36+
37+
result, err := ex.Calculate(input)
38+
if err != nil {
39+
usage()
40+
return
41+
}
42+
fmt.Println(result)
43+
}
44+
45+
func usage() {
46+
fmt.Println("ksql <expression> <json>")
47+
fmt.Println("or")
48+
fmt.Println("echo '{{}}' | ksql <expression> -")
49+
}
50+
51+
func isInputFromPipe() bool {
52+
fileInfo, _ := os.Stdin.Stat()
53+
return fileInfo.Mode()&os.ModeCharDevice == 0
54+
}

0 commit comments

Comments
 (0)