From 1da9636b8bbc1ee0dfd497f35577ff6d129e1aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Wed, 24 Oct 2018 17:22:07 +0100 Subject: [PATCH] Fix build for all OS after #8424 (#8717) * Fix build for all OS after #8424 * Apply PR comments --- libbeat/reader/readjson/docker_json.go | 28 +++++++++++++++-- libbeat/reader/readjson/docker_json_unix.go | 30 ------------------- .../reader/readjson/docker_json_windows.go | 30 ------------------- 3 files changed, 26 insertions(+), 62 deletions(-) delete mode 100644 libbeat/reader/readjson/docker_json_unix.go delete mode 100644 libbeat/reader/readjson/docker_json_windows.go diff --git a/libbeat/reader/readjson/docker_json.go b/libbeat/reader/readjson/docker_json.go index a5331bc9c67..eccbdaf7f9e 100644 --- a/libbeat/reader/readjson/docker_json.go +++ b/libbeat/reader/readjson/docker_json.go @@ -20,6 +20,7 @@ package readjson import ( "bytes" "encoding/json" + "runtime" "time" "github.com/elastic/beats/libbeat/common" @@ -42,6 +43,8 @@ type DockerJSONReader struct { // parse CRI flags criflags bool + + stripNewLine func(msg *reader.Message) } type logLine struct { @@ -54,13 +57,21 @@ type logLine struct { // New creates a new reader renaming a field func New(r reader.Reader, stream string, partial bool, forceCRI bool, CRIFlags bool) *DockerJSONReader { - return &DockerJSONReader{ + reader := DockerJSONReader{ stream: stream, partial: partial, reader: r, forceCRI: forceCRI, criflags: CRIFlags, } + + if runtime.GOOS == "windows" { + reader.stripNewLine = stripNewLineWin + } else { + reader.stripNewLine = stripNewLine + } + + return &reader } // parseCRILog parses logs in CRI log format. @@ -112,7 +123,7 @@ func (p *DockerJSONReader) parseCRILog(message *reader.Message, msg *logLine) er // Remove \n ending for partial messages message.Content = log[i] if partial { - stripNewLine(message) + p.stripNewLine(message) } return nil @@ -192,3 +203,16 @@ func (p *DockerJSONReader) Next() (reader.Message, error) { return message, err } } + +func stripNewLine(msg *reader.Message) { + l := len(msg.Content) + if l > 0 && msg.Content[l-1] == '\n' { + msg.Content = msg.Content[:l-1] + } +} + +func stripNewLineWin(msg *reader.Message) { + msg.Content = bytes.TrimRightFunc(msg.Content, func(r rune) bool { + return r == '\n' || r == '\r' + }) +} diff --git a/libbeat/reader/readjson/docker_json_unix.go b/libbeat/reader/readjson/docker_json_unix.go deleted file mode 100644 index 40eb75b5825..00000000000 --- a/libbeat/reader/readjson/docker_json_unix.go +++ /dev/null @@ -1,30 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// +build linux darwin - -package readjson - -import ( - "github.com/elastic/beats/libbeat/reader" -) - -func stripNewLine(msg *reader.Message) { - l := len(msg.Content) - if l > 0 && msg.Content[l-1] == '\n' { - msg.Content = msg.Content[:l-1] - } -} diff --git a/libbeat/reader/readjson/docker_json_windows.go b/libbeat/reader/readjson/docker_json_windows.go deleted file mode 100644 index e750b4095ee..00000000000 --- a/libbeat/reader/readjson/docker_json_windows.go +++ /dev/null @@ -1,30 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package readjson - -import ( - "bytes" - - "github.com/elastic/beats/libbeat/reader" -) - -func stripNewLine(msg *reader.Message) { - msg.Content = bytes.TrimRightFunc(msg.Content, func(r rune) bool { - return r == '\n' || r == '\r' - }) -}