Skip to content

Commit

Permalink
Merge pull request #8 from logmanager-oss/code_refactor/make_anonymiz…
Browse files Browse the repository at this point in the history
…er_work_with_streams

implement stream reading
  • Loading branch information
tender-barbarian authored Nov 19, 2024
2 parents 487c905 + a6f90e2 commit 0f9a78a
Show file tree
Hide file tree
Showing 35 changed files with 693 additions and 1,005 deletions.
71 changes: 71 additions & 0 deletions cmd/logveil/logveil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package logveil

import (
"bufio"
"errors"
"fmt"
"io"
"log/slog"

"github.com/logmanager-oss/logveil/internal/anonymizer"
"github.com/logmanager-oss/logveil/internal/config"
"github.com/logmanager-oss/logveil/internal/files"
"github.com/logmanager-oss/logveil/internal/reader"
"github.com/logmanager-oss/logveil/internal/writer"
)

func Start() {
slog.Info("Anonymization process started...")

config := &config.Config{}
config.LoadAndValidate()

if *config.IsVerbose {
slog.SetLogLoggerLevel(slog.LevelDebug)
}

filesHandler := &files.FilesHandler{}
defer filesHandler.Close()

inputReader, err := reader.CreateInputReader(config, filesHandler)
if err != nil {
return
}
outputWriter, err := writer.CreateOutputWriter(config, filesHandler)
if err != nil {
return
}
anonymizerDoer, err := anonymizer.CreateAnonymizer(config)
if err != nil {
return
}

err = RunAnonymizationLoop(inputReader, outputWriter, anonymizerDoer)
if err != nil {
slog.Error("running anonymisation loop", "error", err)
return
}

slog.Info("All done. Exiting...")
}

func RunAnonymizationLoop(inputReader reader.InputReader, outputWriter *bufio.Writer, anonymizerDoer *anonymizer.Anonymizer) error {
defer outputWriter.Flush()

for {
logLine, err := inputReader.ReadLine()
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return fmt.Errorf("reading line: %v", err)
}

anonymizedLogLine := anonymizerDoer.Anonymize(logLine)

_, err = fmt.Fprintln(outputWriter, anonymizedLogLine)
if err != nil {
return fmt.Errorf("writing log line to buffer: %v", err)
}
}
}
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import "github.com/logmanager-oss/logveil/internal/anonymizer"
import "github.com/logmanager-oss/logveil/cmd/logveil"

func main() {
anonymizer.Run()
logveil.Start()
}
Loading

0 comments on commit 0f9a78a

Please sign in to comment.