diff --git a/main.go b/main.go index cd5197f..67f644e 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "fmt" "log/slog" "os" - "regexp" "sync" "github.com/jasonlvhit/gocron" @@ -29,7 +28,7 @@ func main() { validate() if f.Test { - testIt() + pkg.TestIt(f.FilePath, f.Match) return } @@ -221,34 +220,6 @@ func notify(result *pkg.ScanResult) { } } -func testIt() { - fps, err := pkg.FilesByPattern(f.FilePath) - if err != nil { - slog.Error("Error finding files", "error", err.Error()) - } - slog.Info("Files found", "count", len(fps)) - for _, filePath := range fps { - slog.Info("Found file", "filePath", filePath) - } - str := pkg.ReadFromPipeInput() - if str == "" { - slog.Error("No input found") - slog.Info("Usage echo 'test123' | go-watch-logs --match=123 -test") - return - } - str = str[:len(str)-1] // strip new line - re, err := regexp.Compile(f.Match) - if err != nil { - slog.Error("Error compiling regex", "error", err.Error()) - return - } - if re.Match([]byte(str)) { - slog.Info("Matched", "Match Regex", f.Match, "input", str, "Match Found", re.FindString(str)) - } else { - slog.Warn("Not matched", "Match", f.Match, "str", str) - } -} - func flags() { flag.StringVar(&f.FilePath, "file-path", "", "full path to the log file") flag.StringVar(&f.FilePath, "f", "", "(short for --file-path) full path to the log file") diff --git a/pkg/testit.go b/pkg/testit.go new file mode 100644 index 0000000..1205c4b --- /dev/null +++ b/pkg/testit.go @@ -0,0 +1,33 @@ +package pkg + +import ( + "log/slog" + "regexp" +) + +func TestIt(filepath string, match string) { + fps, err := FilesByPattern(filepath) + if err != nil { + slog.Error("Error finding files", "error", err.Error()) + } + slog.Info("Files found", "count", len(fps)) + for _, filePath := range fps { + slog.Info("Found file", "filePath", filePath) + } + str := ReadFromPipeInput() + if str == "" { + slog.Error("No input found, see --help for usage") + return + } + str = str[:len(str)-1] // strip new line + re, err := regexp.Compile(match) + if err != nil { + slog.Error("Error compiling regex", "error", err.Error()) + return + } + if re.Match([]byte(str)) { + slog.Info("Matched", "Match Regex", match, "input", str, "Match Found", re.FindString(str)) + } else { + slog.Warn("Not matched", "Match", match, "str", str) + } +}