Skip to content

Commit 7f20c23

Browse files
committed
fix: improve context handling and error reporting
- Added context cancellation handling in execCount to properly exit on context done. - Refactored printRes to return early when -all flag is set, improving readability. - Enhanced error message for invalid JSON in 'logmsg' field. - Simplified flag validation logic in warnInvalidFlagPlacement.
1 parent 8435fa3 commit 7f20c23

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

loggly.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ func execCount(ctx context.Context, query string, from string, to string) {
124124
res, err := c.Fetch(ctx, *q)
125125
for {
126126
select {
127+
case <-ctx.Done():
128+
check(ctx.Err())
129+
return
127130
case r := <-res:
128131
fmt.Println(r.Total)
129132
return
@@ -137,10 +140,11 @@ func execCount(ctx context.Context, query string, from string, to string) {
137140
func printRes(res search.Response) {
138141
if *allMsg {
139142
check(printJSON(res.Events))
140-
} else {
141-
if err := printLogMSG(res.Events); err != nil {
142-
fmt.Fprintf(os.Stderr, "Invalid JSON in the 'logmsg' field. Consider to filter the messages, or use the -all flag and parse the message yourself.\n\n%s", err.Error())
143-
}
143+
return
144+
}
145+
146+
if err := printLogMSG(res.Events); err != nil {
147+
fmt.Fprintf(os.Stderr, "Invalid JSON in the 'logmsg' field. Consider to filter the messages, or use the -all flag and parse the message yourself.\n\n%s", err.Error())
144148
}
145149
}
146150

@@ -183,9 +187,10 @@ func warnInvalidFlagPlacement(args []string) {
183187
flags.VisitAll(func(f *flag.Flag) {
184188
currentFlags["-"+f.Name] = true
185189
})
190+
186191
var invalidFlags []string
187192
for _, arg := range args {
188-
if strings.HasPrefix(arg, "-") && currentFlags[arg] {
193+
if currentFlags[arg] {
189194
invalidFlags = append(invalidFlags, arg)
190195
}
191196
}

0 commit comments

Comments
 (0)