Skip to content

Commit

Permalink
fixing writer
Browse files Browse the repository at this point in the history
Signed-off-by: Jaydip Gabani <[email protected]>
  • Loading branch information
JaydipGabani committed Nov 26, 2024
1 parent f275ae5 commit 7018172
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/pubsub/diskwriter/diskwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path"
"syscall"
Expand Down Expand Up @@ -39,7 +38,7 @@ func (r *DiskWriter) Publish(_ context.Context, data interface{}, _ string) erro
if r.file == nil {
// Open a new file and acquire a lock
filePath := path.Join(r.Path, "violations.txt")
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
Expand All @@ -53,6 +52,11 @@ func (r *DiskWriter) Publish(_ context.Context, data interface{}, _ string) erro
}

r.file = file
err = r.file.Truncate(0)
if err != nil {
r.file = nil
return fmt.Errorf("failed to truncate file: %w", err)
}
}

_, err = r.file.WriteString(string(jsonData) + "\n")
Expand Down

0 comments on commit 7018172

Please sign in to comment.