diff --git a/README.md b/README.md index 39062ac..34ed6f4 100644 --- a/README.md +++ b/README.md @@ -103,4 +103,5 @@ BenchmarkLogRotation-10 1036 1175464 ns/op 12930 B/op - **v1.0.1** Health Check - **v1.0.2** `--post`, `--post-min`, `--health-check-every` and preview added - **v1.0.7** Added percentage +- **v1.0.10** Better tint and added `-test` flag and `-f` for short form of `--file-path` diff --git a/main.go b/main.go index e664cb1..6931525 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" "os" + "regexp" "sync" "github.com/jasonlvhit/gocron" @@ -27,6 +28,11 @@ func main() { wantsVersion() validate() + if f.Test { + testIt() + return + } + var err error newFilePaths, err := pkg.FilesByPattern(f.FilePath) if err != nil { @@ -215,8 +221,37 @@ 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") flag.StringVar(&f.DBPath, "db-path", pkg.GetHomedir()+"/.go-watch-logs.db", "path to store db file") flag.StringVar(&f.Match, "match", "", "regex for matching errors (empty to match all lines)") flag.StringVar(&f.Ignore, "ignore", "", "regex for ignoring errors (empty to ignore none)") @@ -229,6 +264,13 @@ func flags() { flag.IntVar(&f.FilePathsCap, "file-paths-cap", 100, "max number of file paths to watch") flag.IntVar(&f.Min, "min", 1, "on minimum num of matches, it should notify") flag.BoolVar(&f.Version, "version", false, "") + flag.BoolVar(&f.Test, "test", false, `Quickly test paths or regex + +echo test123 | go-watch-logs --match=123 --test +# will test if the input matches the regex +go-watch-logs --file-path=./ssl_access.*log --test +# will test if the file paths are found and list them + `) flag.StringVar(&f.Proxy, "proxy", "", "http proxy for webhooks") flag.StringVar(&f.MSTeamsHook, "ms-teams-hook", "", "ms teams webhook") diff --git a/pkg/flags.go b/pkg/flags.go index caa6884..04a9202 100644 --- a/pkg/flags.go +++ b/pkg/flags.go @@ -16,5 +16,6 @@ type Flags struct { LogLevel int MemLimit int MSTeamsHook string + Test bool Version bool } diff --git a/pkg/system.go b/pkg/system.go index b630bd0..08741cf 100644 --- a/pkg/system.go +++ b/pkg/system.go @@ -70,3 +70,24 @@ func sendPanicCheck(f *Flags, m *runtime.MemStats) { slog.Info("Successfully sent to MS Teams") } } + +func ReadFromPipeInput() string { + fi, _ := os.Stdin.Stat() + if (fi.Mode() & os.ModeCharDevice) == 0 { + buf := make([]byte, 0, 4096) + tmp := make([]byte, 256) + for { + n, err := os.Stdin.Read(tmp) + if n == 0 { + break + } + if err != nil { + slog.Error("Error reading from pipe", "error", err.Error()) + break + } + buf = append(buf, tmp[:n]...) + } + return string(buf) + } + return "" +}