mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-17 17:27:57 +00:00
Add: proper error handling for files
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -25,6 +26,10 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
errDirectoriesUnsupported = errors.New("directories are not supported")
|
||||
)
|
||||
|
||||
// timelineFromPaths takes every path, search them using a list of regexes
|
||||
// and organize them in a timeline that will be ready to aggregate or read
|
||||
func timelineFromPaths(paths []string, regexes types.RegexMap) (types.Timeline, error) {
|
||||
@@ -34,6 +39,14 @@ func timelineFromPaths(paths []string, regexes types.RegexMap) (types.Timeline,
|
||||
compiledRegex := prepareGrepArgument(regexes)
|
||||
|
||||
for _, path := range paths {
|
||||
osinfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if osinfo.IsDir() {
|
||||
return nil, errDirectoriesUnsupported
|
||||
}
|
||||
|
||||
stdout := make(chan string)
|
||||
|
||||
go func() {
|
||||
@@ -193,7 +206,6 @@ func iterateOnGrepResults(path string, regexes types.RegexMap, grepStdout <-chan
|
||||
}
|
||||
ctx, displayer = regex.Handle(ctx, line)
|
||||
li := types.NewLogInfo(date, displayer, line, regex, key, ctx, filetype)
|
||||
|
||||
lt = lt.Add(li)
|
||||
}
|
||||
|
||||
|
31
src/go/pt-galera-log-explainer/internal_test.go
Normal file
31
src/go/pt-galera-log-explainer/internal_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTimelineFromPaths(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
path: "tests/logs/",
|
||||
expectedErr: errDirectoriesUnsupported,
|
||||
},
|
||||
{
|
||||
path: "tests/logs/non_existing",
|
||||
expectedErr: os.ErrNotExist,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
_, err := timelineFromPaths([]string{test.path}, nil)
|
||||
if !errors.Is(err, test.expectedErr) {
|
||||
t.Fatalf("with path %s, expected error %v, got %v", test.path, test.expectedErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user