opengist/internal/git/output_parser.go

34 lines
668 B
Go
Raw Normal View History

package git
import (
"bytes"
"io"
)
func truncateCommandOutput(out io.Reader, maxBytes int64) (string, bool, error) {
2023-03-18 17:22:27 +00:00
var buf []byte
var err error
if maxBytes < 0 {
buf, err = io.ReadAll(out)
2023-03-18 17:22:27 +00:00
} else {
buf, err = io.ReadAll(io.LimitReader(out, maxBytes))
}
2023-03-18 17:22:27 +00:00
if err != nil {
return "", false, err
}
2023-03-18 17:22:27 +00:00
truncated := len(buf) >= int(maxBytes)
// Remove the last line if it's truncated
if truncated {
// Find the index of the last newline character
lastNewline := bytes.LastIndexByte(buf, '\n')
2023-03-18 17:22:27 +00:00
if lastNewline > 0 {
// Trim the data buffer up to the last newline character
buf = buf[:lastNewline]
}
}
2023-03-18 17:22:27 +00:00
return string(buf), truncated, nil
}