forked from filhodanuvem/gitql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
66 lines (56 loc) · 1.37 KB
/
cmd.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
package main
import (
"flag"
"os"
"fmt"
"github.com/cloudson/gitql/runtime"
)
var path *string
var query string
func init() {
parseCommandLine()
}
func usage() {
fmt.Println("Gitql - Git query language")
fmt.Printf("Usage: %s [flags] [args] \n ", os.Args[0])
fmt.Printf("\nFlags: \n")
flag.PrintDefaults()
fmt.Printf("Arguments: \n")
fmt.Printf(" sql: A query to run\n")
}
func printTables() {
tables := runtime.PossibleTables()
for tableName, fields := range tables {
fmt.Printf("%s\n\t", tableName)
for i, field := range fields {
comma := "."
if i + 1 < len(fields) {
comma = ", "
}
fmt.Printf("%s%s", field, comma)
}
fmt.Println()
}
}
func parseCommandLine() {
path = flag.String("p", ".", "The (optional) path to run gitql")
version := flag.Bool("v", false, "The version of gitql")
showTables := flag.Bool("show-tables", false, "Show all tables")
flag.Usage = usage
flag.Parse()
if *version {
// @todo refactor to dynamic value
fmt.Println("Gitql 1.1.1")
os.Exit(0)
}
if *showTables {
fmt.Printf("Tables: \n\n")
printTables()
os.Exit(0)
}
query = flag.Arg(0)
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
}