-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd.go
163 lines (144 loc) · 3.71 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/gfleury/gstreamtop/conf"
"github.com/gfleury/gstreamtop/input"
"github.com/gfleury/gstreamtop/output"
profile "github.com/gfleury/gstreamtop/profiling"
)
func main() {
var mapFile, inputFile, outputer string
var inputFd *os.File
var o output.Outputer
var err error
var profiling bool
var cpuProfileHandler func()
c := &conf.Configuration{}
var cmdRunNamedQuery = &cobra.Command{
Use: "runNamedQuery mappingname queryName",
Short: "Run a Named SQL query.",
Long: `runNamedQuery runs SQL queries against text streams.`,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
for i, mapping := range c.Mappings {
if mapping.Name == args[0] {
err := o.CreateStreamFromConfigurationMapping(&c.Mappings[i], args[1:])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = o.Configure()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
break
}
}
if o == nil {
fmt.Printf("No mapping named %s found.\n", args[0])
os.Exit(1)
}
i, err := input.CreateStreamInputFromStreamOutput(o)
tables := o.Stream().Tables()
i.SetTable(tables[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
i.Run(inputFd)
o.Loop()
},
}
var cmdRunQuery = &cobra.Command{
Use: "runQuery mappingname \"SELECT COUNT(*) FROM table GROUP BY field\"",
Short: "Run a SQL query.",
Long: `runQuery runs a SQL queries against text streams.`,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
for i, mapping := range c.Mappings {
if mapping.Name == args[0] {
err := o.CreateStreamFromConfigurationMapping(&c.Mappings[i], nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
queries := args[1:]
for _, query := range queries {
err = o.Stream().Query(query)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
err = o.Configure()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
break
}
}
if o == nil {
fmt.Printf("No mapping named %s found.\n", args[0])
os.Exit(1)
}
i, err := input.CreateStreamInputFromStreamOutput(o)
tables := o.Stream().Tables()
i.SetTable(tables[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
i.Run(inputFd)
o.Loop()
},
}
var rootCmd = &cobra.Command{Use: "app"}
cobra.OnInitialize(func() {
switch outputer {
case "simpletable":
o = &output.SimpleTableOutput{}
case "table":
o = &output.TableOutput{}
case "prometheus":
o = &output.PrometheusOutput{}
}
if inputFile == "" {
inputFd = os.Stdin
} else {
inputFd, err = os.Open(inputFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
c.SetFileURL(mapFile)
err := c.ReadFile()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if profiling {
cpuProfileHandler = profile.EnableCPUProfile("gstreamtop.prof")
o.EnableProfile()
}
})
defer inputFd.Close()
if cpuProfileHandler != nil {
defer cpuProfileHandler()
}
rootCmd.PersistentFlags().StringVar(&mapFile, "map", "./mapping.yaml", "config file (default is https://raw.githubusercontent.com/gfleury/gstreamtop/master/mapping.yaml)")
rootCmd.PersistentFlags().StringVar(&inputFile, "input", "", "input file, default to stdin")
rootCmd.PersistentFlags().StringVar(&outputer, "output", "simpletable", "output method, use: simpletable/table")
rootCmd.PersistentFlags().BoolVar(&profiling, "profile", false, "enable profiling for CPU and Memory with pprof")
rootCmd.AddCommand(cmdRunNamedQuery)
rootCmd.AddCommand(cmdRunQuery)
err = rootCmd.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}