Main program parses stdin
This commit is contained in:
parent
23577d7274
commit
75ff8bf4d5
|
@ -0,0 +1,44 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/localhots/punk/buffer"
|
||||
"github.com/localhots/punk/parser"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
sel string
|
||||
verbose bool
|
||||
)
|
||||
flag.StringVar(&sel, "s", "", "Selector")
|
||||
flag.BoolVar(&verbose, "v", false, "Verbose parsing")
|
||||
flag.Parse()
|
||||
|
||||
if len(sel) == 0 && !verbose {
|
||||
fmt.Println("No selectors given and parser is not verbose")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
sels := strings.Split(sel, " ")
|
||||
if len(sel) == 0 {
|
||||
sels = []string{}
|
||||
}
|
||||
buf := buffer.NewStreamBuffer(os.Stdin)
|
||||
pars := parser.New(buf, sels)
|
||||
if verbose {
|
||||
pars.Debug()
|
||||
}
|
||||
res := pars.ParseStream()
|
||||
for {
|
||||
if m, ok := <-res; ok {
|
||||
fmt.Println(m.Sel, m.Val)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
22
run.go
22
run.go
|
@ -1,22 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/kr/pretty"
|
||||
"github.com/localhots/punk/buffer"
|
||||
"github.com/localhots/punk/parser"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, _ := os.Open("test.json")
|
||||
// b, _ := ioutil.ReadAll(f)
|
||||
buf := buffer.NewStreamBuffer(f)
|
||||
|
||||
p := parser.New(buf, []string{
|
||||
"/prices/*",
|
||||
"/bananas/[*]/weight",
|
||||
})
|
||||
res := p.Parse()
|
||||
pretty.Println(res)
|
||||
}
|
Loading…
Reference in New Issue