Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 2363ce6

Browse files
committed
Support vertical print
1 parent 64e23c0 commit 2363ce6

File tree

3 files changed

+75
-20
lines changed

3 files changed

+75
-20
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,28 @@ ap-northeast-1> source /aws/lambda/my-lambda start=2018/11/19 end=2019/11/21 | f
5959
$ cat output.jsonl
6060
{"@timestamp":"2019-04-09 09:38:19.455","@message":"2019-04-09T09:38:19.455Z\tab83228c-6af1-4c0b-a304-e043aecbe84a\tLogEC2InstanceStateChange\n"}
6161
```
62+
63+
### Print vertically
64+
65+
```
66+
ap-northeast-1> source /aws/lambda/my-lambdaap-northeast-1> source /aws/lambda/test start=2018/11/19 end=2019/11/21 | field @timestamp, @message | limit 3 | vertically;
67+
*************************** 1. row ***************************
68+
@timestamp: 2019-04-09 09:38:19.455
69+
@message: REPORT RequestId: ab83228c-6af1-4c0b-a304-e043aecbe84a Duration: 0.66 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 48 MB
70+
71+
*************************** 2. row ***************************
72+
@timestamp: 2019-04-09 09:38:19.455
73+
@message: 2019-04-09T09:38:19.455Z ab83228c-6af1-4c0b-a304-e043aecbe84a LogEC2InstanceStateChange
74+
75+
*************************** 3. row ***************************
76+
@timestamp: 2019-04-09 09:38:19.455
77+
@message: 2019-04-09T09:38:19.455Z ab83228c-6af1-4c0b-a304-e043aecbe84a Received event: {
78+
"version": "0",
79+
"id": "4b22e040-e3dd-a0aa-84e0-a946526876a7",
80+
"detail-type": "AWS API Call via CloudTrail",
81+
"source": "aws.ec2",
82+
"account": "822997939312",
83+
"time": "2019-04-09T09:37:35Z",
84+
"region": "ap-northeast-1",
85+
...
86+
```

exec/head.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ func (cmd *headCommand) Start(svc *cloudwatchlogs.CloudWatchLogs, flags *cli.Fla
140140

141141
for i, event := range events {
142142
fmt.Fprintf(out, "*************************** %d. row ***************************\n", i+1)
143-
fmt.Fprintf(out, "Timestamp: %d\n", *event.Timestamp)
144-
fmt.Fprintf(out, "Message: %s\n", *event.Message)
143+
fmt.Fprintf(out, "Timestamp:\t%d\n", *event.Timestamp)
144+
fmt.Fprintf(out, "Message:\t%s\n", *event.Message)
145145
}
146146

147147
return

exec/query.go

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ import (
1818

1919
const queryWaitInterval = 1
2020

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*$`)
2223

2324
type queryCommand struct {
2425
logGroupName string
2526
queryString string
2627
startTime int64
2728
endTime int64
29+
vertically bool
2830
}
2931

3032
func parseQuery(str string) (cmd Executable, err error) {
31-
submatch := regexpWuery.FindStringSubmatch(str)
33+
submatch := regexpQuery.FindStringSubmatch(str)
3234

3335
if len(submatch) == 0 {
3436
err = fmt.Errorf("Invalid query: %s", str)
@@ -37,6 +39,13 @@ func parseQuery(str string) (cmd Executable, err error) {
3739

3840
logGroupName := submatch[1]
3941
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+
}
4049

4150
var startTime, endTime time.Time
4251
startTime, err = dateparse.ParseLocal(submatch[2])
@@ -58,6 +67,7 @@ func parseQuery(str string) (cmd Executable, err error) {
5867
queryString: queryString,
5968
startTime: startTime.Unix(),
6069
endTime: endTime.Unix(),
70+
vertically: vertically,
6171
}
6272

6373
return
@@ -112,15 +122,31 @@ func escapeJson(str string) string {
112122
return string(b)
113123
}
114124

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])
118144

119145
if !showptr {
120146
fieldLen--
121147
}
122148

123-
for _, result := range result.Results {
149+
for _, result := range results {
124150
fmt.Fprint(out, "{")
125151

126152
for i, field := range result {
@@ -140,16 +166,6 @@ func printQueryResult(result *cloudwatchlogs.GetQueryResultsOutput, showptr bool
140166
fmt.Fprintln(out, "}")
141167
}
142168
}
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-
)
153169
}
154170

155171
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
159175
return
160176
}
161177

162-
result, err := waitQueryResult(svc, queryId)
178+
resp, err := waitQueryResult(svc, queryId)
163179

164180
if err != nil {
165181
return
166182
}
167183

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+
)
169199

170200
return
171201
}

0 commit comments

Comments
 (0)