-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add --stdin flag * Move Log and TailOptions to separate file When Log and TailOptions are used by other classes it is now more obvious that they're not hard lined to Tail. * Create FileTail for tailing log files FileTail is heavily based on Tail but I saw no obvious way to abstract the common parts of the classes to not duplicate them. FileTail is so small it's hardly noticeable anyway. * Create stdin tailing when using --stdin * Document --stdin flag * Remove start/stop printing from FileTail Also moving file tailing to main thread as there are no parallel tails with that config.
- Loading branch information
Showing
11 changed files
with
611 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package stern | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
type FileTail struct { | ||
Options *TailOptions | ||
tmpl *template.Template | ||
in io.Reader | ||
out io.Writer | ||
errOut io.Writer | ||
} | ||
|
||
// NewFileTail returns a new tail of the input reader | ||
func NewFileTail(tmpl *template.Template, in io.Reader, out, errOut io.Writer, options *TailOptions) *FileTail { | ||
return &FileTail{ | ||
Options: options, | ||
tmpl: tmpl, | ||
in: in, | ||
out: out, | ||
errOut: errOut, | ||
} | ||
} | ||
|
||
// Start starts tailing | ||
func (t *FileTail) Start() error { | ||
reader := bufio.NewReader(t.in) | ||
err := t.ConsumeReader(reader) | ||
|
||
return err | ||
} | ||
|
||
// ConsumeReader reads the data from the reader and writes into the out | ||
// writer. | ||
func (t *FileTail) ConsumeReader(reader *bufio.Reader) error { | ||
for { | ||
line, err := reader.ReadBytes('\n') | ||
if len(line) != 0 { | ||
t.consumeLine(strings.TrimSuffix(string(line), "\n")) | ||
} | ||
|
||
if err != nil { | ||
if err != io.EOF { | ||
return err | ||
} | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// Print prints a color coded log message | ||
func (t *FileTail) Print(msg string) { | ||
vm := Log{ | ||
Message: msg, | ||
NodeName: "", | ||
Namespace: "", | ||
PodName: "", | ||
ContainerName: "", | ||
PodColor: color.New(color.Reset), | ||
ContainerColor: color.New(color.Reset), | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := t.tmpl.Execute(&buf, vm); err != nil { | ||
fmt.Fprintf(t.errOut, "expanding template failed: %s\n", err) | ||
return | ||
} | ||
|
||
fmt.Fprint(t.out, buf.String()) | ||
} | ||
|
||
func (t *FileTail) consumeLine(line string) { | ||
content := line | ||
|
||
if t.Options.IsExclude(content) || !t.Options.IsInclude(content) { | ||
return | ||
} | ||
|
||
msg := t.Options.HighlightMatchedString(content) | ||
t.Print(msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package stern | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"io" | ||
"strings" | ||
"testing" | ||
"text/template" | ||
) | ||
|
||
func TestConsumeFileTail(t *testing.T) { | ||
logLines := `line 1 | ||
line 2 | ||
line 3 | ||
line 4` | ||
tmpl := template.Must(template.New("").Parse(`{{printf "%s\n" .Message}}`)) | ||
|
||
tests := []struct { | ||
name string | ||
resumeReq *ResumeRequest | ||
expected []byte | ||
}{ | ||
{ | ||
name: "normal", | ||
expected: []byte(`line 1 | ||
line 2 | ||
line 3 | ||
line 4 | ||
`), | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
out := new(bytes.Buffer) | ||
tail := NewFileTail(tmpl, nil, out, io.Discard, &TailOptions{}) | ||
if err := tail.ConsumeReader(bufio.NewReader(strings.NewReader(logLines))); err != nil { | ||
t.Fatalf("%d: unexpected err %v", i, err) | ||
} | ||
|
||
if !bytes.Equal(tt.expected, out.Bytes()) { | ||
t.Errorf("%d: expected %s, but actual %s", i, tt.expected, out) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.