Skip to content

Commit 2f24c5f

Browse files
authored
Merge pull request #18 from hyeoksuhan/develop
eblogs
2 parents 85211cb + 82de327 commit 2f24c5f

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

cmd/cmd_eblogs.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type grepOpt struct {
4040
}
4141

4242
func (o grepOpt) isEmpty() bool {
43-
return o.regexp == ""
43+
return strings.TrimSpace(o.regexp) == ""
4444
}
4545

4646
func init() {
@@ -227,6 +227,9 @@ func streamlogs(ctx context.Context, wg *sync.WaitGroup, input streamlogsInput)
227227
}
228228

229229
scanner := bufio.NewScanner(stdout)
230+
// to prevent 'token too long' error
231+
buf := make([]byte, 0, 64*1024)
232+
scanner.Buffer(buf, 1024*1024)
230233

231234
err = cmd.Start()
232235
if err != nil {
@@ -235,26 +238,42 @@ func streamlogs(ctx context.Context, wg *sync.WaitGroup, input streamlogsInput)
235238

236239
grepOpt := input.grepOpt
237240
re := regexp.MustCompile(grepOpt.regexp)
241+
cTarget := input.colorf(target)
238242
remain := 0
239243
for scanner.Scan() {
240244
line := scanner.Text()
241-
match := re.FindAllString(line, -1)
242-
matched := len(match) > 0
243245

244-
if matched {
245-
remain = grepOpt.aCount
246+
if grepOpt.isEmpty() {
247+
printTargetLog(cTarget, line)
248+
continue
246249
}
247250

248-
if !grepOpt.isEmpty() && !matched && remain < 0 {
251+
match := re.FindAllString(line, -1)
252+
matched := len(match) > 0
253+
loggable := matched || remain > 0
254+
255+
if !loggable {
249256
continue
250257
}
251258

252259
for _, m := range match {
253260
line = strings.Replace(line, m, input.colorf(m), 1)
254261
}
255262

256-
fmt.Printf("[%s] %s\r\n", input.colorf(target), line)
257-
remain--
263+
printTargetLog(cTarget, line)
264+
265+
if grepOpt.aCount == 0 {
266+
continue
267+
}
268+
269+
if matched {
270+
remain = grepOpt.aCount
271+
continue
272+
}
273+
274+
if remain--; remain == 0 {
275+
fmt.Printf(input.colorf("--") + "\r\n")
276+
}
258277
}
259278

260279
err = scanner.Err()
@@ -285,6 +304,10 @@ func streamlogs(ctx context.Context, wg *sync.WaitGroup, input streamlogsInput)
285304
return
286305
}
287306

307+
func printTargetLog(target string, log string) {
308+
fmt.Printf("[%s] %s\r\n", target, log)
309+
}
310+
288311
func terminateSession(svc gossm.SSMservice, sid string) error {
289312
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
290313
defer cancel()

0 commit comments

Comments
 (0)