Skip to content

Commit

Permalink
feat: --test flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Dec 7, 2024
1 parent 1540607 commit c683909
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

42 changes: 42 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"os"
"regexp"
"sync"

"github.com/jasonlvhit/gocron"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)")
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ type Flags struct {
LogLevel int
MemLimit int
MSTeamsHook string
Test bool
Version bool
}
21 changes: 21 additions & 0 deletions pkg/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

0 comments on commit c683909

Please sign in to comment.