diff options
| author | Paul Buetow <paul@buetow.org> | 2025-06-20 13:51:06 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-06-20 13:51:06 +0300 |
| commit | 4a8f1690c3e2e6aec34f22b516f0598c6a0da070 (patch) | |
| tree | 82ecb4568df3e91c7958c2e9f491546e6f7e9701 /internal/io/fs/catprocessor.go | |
| parent | b0a1e7928d5147d2a9fe0df710bf7c75a777e0d0 (diff) | |
Fix dcat/dgrep serverless mode to show REMOTE protocol formatrefactor-trail-1
- Add serverless flag to CatProcessor and GrepProcessor
- Format output with REMOTE|hostname|transmittedPerc|count|sourceID|content in serverless mode
- Use actual system hostname instead of "serverless" placeholder
- Preserve plain mode behavior (no formatting when --plain is used)
- Fix grep processor to properly separate multiple matched lines
- Add shared getHostname utility function
- Update tests to include serverless parameter
This fixes the regression where dcat and dgrep in serverless mode were not
showing the dtail protocol format with transmission info and status details.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'internal/io/fs/catprocessor.go')
| -rw-r--r-- | internal/io/fs/catprocessor.go | 63 |
1 files changed, 54 insertions, 9 deletions
diff --git a/internal/io/fs/catprocessor.go b/internal/io/fs/catprocessor.go index 0c88114..533328b 100644 --- a/internal/io/fs/catprocessor.go +++ b/internal/io/fs/catprocessor.go @@ -2,19 +2,26 @@ package fs import ( "context" + "fmt" + "github.com/mimecast/dtail/internal/protocol" ) // CatProcessor handles cat-style output type CatProcessor struct { - plain bool - hostname string + plain bool + hostname string + serverless bool } // NewCatProcessor creates a new cat processor -func NewCatProcessor(plain, noColor bool, hostname string) *CatProcessor { +func NewCatProcessor(plain, noColor bool, hostname string, serverless bool) *CatProcessor { + // Debug: log the parameters + // fmt.Fprintf(os.Stderr, "DEBUG CatProcessor: hostname='%s', serverless=%v, plain=%v\n", hostname, serverless, plain) + return &CatProcessor{ - plain: plain, - hostname: hostname, + plain: plain, + hostname: hostname, + serverless: serverless, } } @@ -28,7 +35,8 @@ func (cp *CatProcessor) Cleanup() error { // ProcessLine processes a single line for cat output. // In plain mode, it preserves the original line exactly including line endings. -// In non-plain mode, it returns just the content - the baseHandler will format the protocol. +// In non-plain mode in server context, it returns just the content - the baseHandler will format the protocol. +// In non-plain mode in serverless context, it formats the output with REMOTE protocol. // Returns the line content and true (cat always outputs all lines). func (cp *CatProcessor) ProcessLine(line []byte, lineNum int, filePath string, stats *stats, sourceID string) ([]byte, bool) { // Update stats for matched line (cat always matches all lines) @@ -36,14 +44,51 @@ func (cp *CatProcessor) ProcessLine(line []byte, lineNum int, filePath string, s stats.updateLineMatched() } - // In both plain and non-plain modes, just return the line content - // The baseHandler will handle protocol formatting for non-plain mode + // In plain mode, just return the line content + if cp.plain { + result := make([]byte, len(line)) + copy(result, line) + return result, true + } + + // In non-plain serverless mode, we need to format with REMOTE protocol + // since there's no server baseHandler to do it for us + if cp.serverless { + // Format exactly like original basehandler.go for non-plain mode + // REMOTE|{hostname}|{TransmittedPerc}|{Count}|{SourceID}|{Content} + var transmittedPerc int + var count uint64 + if stats != nil { + // For cat, we always transmit all matched lines, so transmittedPerc should be 100 + transmittedPerc = 100 + count = stats.totalLineCount() + } + + // Use actual hostname from system, not "serverless" + actualHostname := getHostname() + + // Build the protocol line without the message delimiter + protocolLine := fmt.Sprintf("REMOTE%s%s%s%3d%s%v%s%s%s%s", + protocol.FieldDelimiter, actualHostname, protocol.FieldDelimiter, + transmittedPerc, protocol.FieldDelimiter, count, protocol.FieldDelimiter, + sourceID, protocol.FieldDelimiter, string(line)) + + // Return formatted line without color reset prefix + // The ColorWriter will handle proper coloring + result := []byte(protocolLine) + return result, true + } + + // In server mode, just return the line content + // The baseHandler will handle protocol formatting result := make([]byte, len(line)) copy(result, line) return result, true } func (cp *CatProcessor) Flush() []byte { - // Server should not send color codes - client handles colorization + // No need to add color reset codes here + // The ColorWriter handles all coloring return nil } + |
