dockerimg: use more modern APIs
- bytes.Lines instead of bufio.NewScanner
- bytes.CutPrefix instead of HasPrefix/TrimPrefix
Simpler, cheaper.
There is a slight change in that bytes.Lines includes the
trailing newline, and bufio.Scanner does not.
But we only do prefix work to start, and then we do a TrimSpace,
so this ends up being a wash.
diff --git a/dockerimg/dockerimg.go b/dockerimg/dockerimg.go
index f934c2f..ffb0dd7 100644
--- a/dockerimg/dockerimg.go
+++ b/dockerimg/dockerimg.go
@@ -2,7 +2,6 @@
package dockerimg
import (
- "bufio"
"bytes"
"context"
"crypto/rand"
@@ -214,13 +213,13 @@
fmt.Fprintf(os.Stderr, "docker logs failed: %v\n", err)
return
}
- scanner := bufio.NewScanner(bytes.NewReader(out))
- for scanner.Scan() {
- logLine := scanner.Text()
- if !strings.HasPrefix(logLine, "structured logs:") {
+ prefix := []byte("structured logs:")
+ for line := range bytes.Lines(out) {
+ rest, ok := bytes.CutPrefix(line, prefix)
+ if !ok {
continue
}
- logFile := strings.TrimSpace(strings.TrimPrefix(logLine, "structured logs:"))
+ logFile := string(bytes.TrimSpace(rest))
srcPath := fmt.Sprintf("%s:%s", cntrName, logFile)
logFileName := filepath.Base(logFile)
dstPath := filepath.Join(config.ContainerLogDest, logFileName)