Skip to content

Commit

Permalink
implement stream reading
Browse files Browse the repository at this point in the history
  • Loading branch information
solnicki committed Nov 18, 2024
1 parent 487c905 commit e6d36c7
Show file tree
Hide file tree
Showing 34 changed files with 638 additions and 1,015 deletions.
103 changes: 103 additions & 0 deletions cmd/logveil/logveil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package logveil

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

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

type InputReader interface {
ReadLine() (map[string]string, error)
}

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

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

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

inputFile, err := os.Open(config.InputPath)
if err != nil {
slog.Error("opening input file for reading", "error", err)
return
}
defer inputFile.Close()

var inputReader InputReader
if *config.IsLmExport {
inputReader, err = reader.NewLmExportReader(inputFile)
if err != nil {
slog.Error("creating lm export reader", "error", err)
return
}
} else {
inputReader, err = reader.NewLmBackupReader(inputFile)
slog.Error("creating lm backup reader", "error", err)
if err != nil {
return
}
}

var outputFile *os.File
if config.OutputPath != "" {
outputFile, err := os.Create(config.OutputPath)
if err != nil {
slog.Error("opening output file for writing", "error", err)
return
}
defer outputFile.Close()
} else {
slog.Debug("output path empty - defaulting to stdout")
outputFile = os.Stdout
}

outputWriter := bufio.NewWriter(outputFile)
defer outputWriter.Flush()

anonymizingData, err := loader.Load(config.AnonymizationDataPath)
if err != nil {
slog.Error("loading anonymizing data from dir %s: %v", config.AnonymizationDataPath, err)
return
}

anonymizer := anonymizer.New(anonymizingData)

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

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

func RunAnonymizationLoop(inputReader InputReader, outputWriter io.Writer, anonymizer *anonymizer.Anonymizer) error {
for {
logLine, err := inputReader.ReadLine()
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return fmt.Errorf("reading line: %v", err)
}

anonymizedLogLine := anonymizer.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.Run()
}
Binary file added examples/.DS_Store
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit e6d36c7

Please sign in to comment.