@@ -18,17 +18,19 @@ import (
18
18
19
19
const queryWaitInterval = 1
20
20
21
- var regexpWuery = regexp .MustCompile (`(?i)^source\s+(\S+)\s+start=(\S+)\s+end=(\S+)\s*\|\s*(.+)$` )
21
+ var regexpQuery = regexp .MustCompile (`(?i)^source\s+(\S+)\s+start=(\S+)\s+end=(\S+)\s*\|\s*(.+)$` )
22
+ var regexpQueryVertically = regexp .MustCompile (`(?i)\|\s*vertically\s*$` )
22
23
23
24
type queryCommand struct {
24
25
logGroupName string
25
26
queryString string
26
27
startTime int64
27
28
endTime int64
29
+ vertically bool
28
30
}
29
31
30
32
func parseQuery (str string ) (cmd Executable , err error ) {
31
- submatch := regexpWuery .FindStringSubmatch (str )
33
+ submatch := regexpQuery .FindStringSubmatch (str )
32
34
33
35
if len (submatch ) == 0 {
34
36
err = fmt .Errorf ("Invalid query: %s" , str )
@@ -37,6 +39,13 @@ func parseQuery(str string) (cmd Executable, err error) {
37
39
38
40
logGroupName := submatch [1 ]
39
41
queryString := strings .TrimSpace (submatch [4 ])
42
+ vertically := false
43
+
44
+ if regexpQueryVertically .MatchString (queryString ) {
45
+ queryString = regexpQueryVertically .ReplaceAllString (queryString , "" )
46
+ queryString = strings .TrimSpace (queryString )
47
+ vertically = true
48
+ }
40
49
41
50
var startTime , endTime time.Time
42
51
startTime , err = dateparse .ParseLocal (submatch [2 ])
@@ -58,6 +67,7 @@ func parseQuery(str string) (cmd Executable, err error) {
58
67
queryString : queryString ,
59
68
startTime : startTime .Unix (),
60
69
endTime : endTime .Unix (),
70
+ vertically : vertically ,
61
71
}
62
72
63
73
return
@@ -112,15 +122,31 @@ func escapeJson(str string) string {
112
122
return string (b )
113
123
}
114
124
115
- func printQueryResult (result * cloudwatchlogs.GetQueryResultsOutput , showptr bool , out io.Writer ) {
116
- if len (result .Results ) > 0 {
117
- fieldLen := len (result .Results [0 ])
125
+ func printResultsVertically (results [][]* cloudwatchlogs.ResultField , showptr bool , out io.Writer ) {
126
+ if len (results ) > 0 {
127
+ for i , result := range results {
128
+ fmt .Fprintf (out , "*************************** %d. row ***************************\n " , i + 1 )
129
+
130
+ for _ , field := range result {
131
+ if ! showptr && * field .Field == "@ptr" {
132
+ continue
133
+ }
134
+
135
+ fmt .Fprintf (out , "%s:\t %s\n " , * field .Field , * field .Value )
136
+ }
137
+ }
138
+ }
139
+ }
140
+
141
+ func printResultsHorizontally (results [][]* cloudwatchlogs.ResultField , showptr bool , out io.Writer ) {
142
+ if len (results ) > 0 {
143
+ fieldLen := len (results [0 ])
118
144
119
145
if ! showptr {
120
146
fieldLen --
121
147
}
122
148
123
- for _ , result := range result . Results {
149
+ for _ , result := range results {
124
150
fmt .Fprint (out , "{" )
125
151
126
152
for i , field := range result {
@@ -140,16 +166,6 @@ func printQueryResult(result *cloudwatchlogs.GetQueryResultsOutput, showptr bool
140
166
fmt .Fprintln (out , "}" )
141
167
}
142
168
}
143
-
144
- fmt .Fprintf (out , "// Status: %s\n " , * result .Status )
145
-
146
- fmt .Fprintf (
147
- out ,
148
- "// Statistics: BytesScanned=%.f RecordsMatched=%.f RecordsScanned=%.f\n " ,
149
- * result .Statistics .BytesScanned ,
150
- * result .Statistics .RecordsMatched ,
151
- * result .Statistics .RecordsScanned ,
152
- )
153
169
}
154
170
155
171
func (cmd * queryCommand ) Start (svc * cloudwatchlogs.CloudWatchLogs , flags * cli.Flags , out io.Writer ) (err error ) {
@@ -159,13 +175,27 @@ func (cmd *queryCommand) Start(svc *cloudwatchlogs.CloudWatchLogs, flags *cli.Fl
159
175
return
160
176
}
161
177
162
- result , err := waitQueryResult (svc , queryId )
178
+ resp , err := waitQueryResult (svc , queryId )
163
179
164
180
if err != nil {
165
181
return
166
182
}
167
183
168
- printQueryResult (result , flags .Showptr , out )
184
+ if cmd .vertically {
185
+ printResultsVertically (resp .Results , flags .Showptr , out )
186
+ } else {
187
+ printResultsHorizontally (resp .Results , flags .Showptr , out )
188
+ }
189
+
190
+ fmt .Fprintf (out , "// Status: %s\n " , * resp .Status )
191
+
192
+ fmt .Fprintf (
193
+ out ,
194
+ "// Statistics: BytesScanned=%.f RecordsMatched=%.f RecordsScanned=%.f\n " ,
195
+ * resp .Statistics .BytesScanned ,
196
+ * resp .Statistics .RecordsMatched ,
197
+ * resp .Statistics .RecordsScanned ,
198
+ )
169
199
170
200
return
171
201
}
0 commit comments