-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquery.go
More file actions
45 lines (38 loc) · 737 Bytes
/
query.go
File metadata and controls
45 lines (38 loc) · 737 Bytes
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
package main
import (
"log"
"os"
. "github.com/morcmarc/csvtoolkit/query"
"github.com/codegangsta/cli"
)
var queryCommand = cli.Command{
Name: "query",
Usage: "Query CSV",
Action: queryAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "query, q",
Value: "",
Usage: "query string",
},
},
}
func queryAction(c *cli.Context) {
// Validate first argument which should be a file path
if len(c.Args()) == 0 {
log.Fatalln("No input given")
}
iFile := c.Args()[0]
// Read file
csv, err := os.Open(iFile)
if err != nil {
log.Fatalf("Could not open file: %s", iFile)
}
defer csv.Close()
qs := c.String("query")
if qs == "" {
log.Fatalf("Missing query string\n")
}
q := NewQuery(csv)
q.Run(qs)
}