Skip to content

Commit

Permalink
unit tests for single Glob; fix general build issues
Browse files Browse the repository at this point in the history
  • Loading branch information
symonk committed Jul 21, 2024
1 parent 0a50b89 commit 3ab5122
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
18 changes: 17 additions & 1 deletion cmd/analyse.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"bufio"
"fmt"
"log/slog"
"os"

"github.com/spf13/cobra"
"github.com/symonk/log-analyse/internal/files"
Expand All @@ -19,13 +22,26 @@ var analyseCmd = &cobra.Command{
locator := files.NewFileLocator(cfg)
flattened, err := locator.Locate()
if err != nil {
slog.Error("unable to parse files", err)
slog.Error("unable to parse files", slog.Any("error", err))
}
slog.Info("files flattened", slog.Any("files", flattened))
// TODO: check files exist, do what we can or add a strict flag

// TODO: Asynchronously process all files, all lines scaling out massively
// TODO: matching patterns in the config file
for _, f := range flattened {
opened, err := os.Open(f.Path)
if err != nil {
panic(err)
}
// TODO: don't defer in the loop!
defer opened.Close()

scanner := bufio.NewScanner(opened)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}

// TODO: Collect matches for each of the thresholds

Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func initConfig() {

viper.AddConfigPath(baseDir)
viper.SetConfigType("yaml")
viper.SetConfigName(".log-analyse")
viper.SetConfigName("log-analyse")
}

// viper.AutomaticEnv()
Expand All @@ -62,7 +62,7 @@ func initConfig() {
os.Exit(2)
}
} else {
slog.Error("no config file could be found")
slog.Error("no config file could be found: ", slog.Any("error", err))
os.Exit(1)
}
slog.Info("Successfully built a config")
Expand Down
19 changes: 19 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,31 @@ files:
`)

var single = []byte(`
---
files:
- glob: "~/logs/*.txt"
threshold:
hits: 5
period: 30s
patterns:
- ".*FATAL.*"
- ".*payment failed.*"
notify: "email"
`)

func TestCanUnmarshalConfigSuccessfully(t *testing.T) {
c, err := loadConfigFile(basic)
assert.Nil(t, err)
assert.Len(t, c.Files, 2)
}

func TestCanLoadSingleConfigBlock(t *testing.T) {
c, err := loadConfigFile(single)
assert.Nil(t, err)
assert.Len(t, c.Files, 1)
}

func TestReturnGlobs(t *testing.T) {
fConfigs := []FileConfig{{Glob: "foo"}, {Glob: "bar"}, {Glob: "baz"}}
c := Config{Files: fConfigs}
Expand Down
6 changes: 3 additions & 3 deletions internal/files/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

type IndividualFile struct {
path string
threshold config.Threshold
Path string
Threshold config.Threshold
}

type Collector interface {
Expand All @@ -35,7 +35,7 @@ func (f FileCollector) Locate() ([]IndividualFile, error) {
return files, err
}
for _, f := range flattened {
files = append(files, IndividualFile{path: f, threshold: file.Threshold})
files = append(files, IndividualFile{Path: f, Threshold: file.Threshold})
}
}
// TODO: Handle duplicate paths here; multiple config blocks can overlap
Expand Down

0 comments on commit 3ab5122

Please sign in to comment.