Skip to content

Commit 96fa2e0

Browse files
committed
chore: group provides csv and blocklist file name
1 parent 2f1bd07 commit 96fa2e0

4 files changed

Lines changed: 41 additions & 12 deletions

File tree

app/instances_processor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (p instancesProcessor) Process(
8989
}
9090

9191
if blocklistsDataProvider.BlockListExists(group) {
92-
p.logger.Info(fmt.Sprintf(" using blocklist '%s.yml'", group.SanitizedGroupName()))
92+
p.logger.Info(fmt.Sprintf(" using blocklist '%s'", group.BlocklistFileName()))
9393
}
9494

9595
personData, err := csv.NewPersonData(persons, group, fileDataProvider, blocklistsDataProvider, p.logger)
@@ -107,7 +107,7 @@ func (p instancesProcessor) Process(
107107
csvFilePath := filepath.Join(
108108
rootDir,
109109
instance.Hostname,
110-
group.SanitizedGroupName()+".csv",
110+
group.CSVFileName(),
111111
)
112112

113113
err = csvWriter.Write(csvFilePath, personData.Header(), personData.Records())

config/group.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ type Group struct {
1010
Fields []Field `yaml:"fields"`
1111
}
1212

13-
func (g Group) SanitizedGroupName() string {
13+
func (g Group) CSVFileName() string {
14+
return g.sanitizedGroupName() + ".csv"
15+
}
16+
17+
func (g Group) BlocklistFileName() string {
18+
return g.sanitizedGroupName() + ".yml"
19+
}
20+
21+
func (g Group) sanitizedGroupName() string {
1422
fileName := g.Name
1523
fileName = strings.ReplaceAll(fileName, " ", "_")
1624
fileName = strings.ReplaceAll(fileName, ",", ".")

config/group_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var _ = Describe("Group", func() {
2424
os.Remove(tempFile.Name())
2525
})
2626

27-
var _ = Describe("SanitizedGroupName", func() {
27+
var _ = Describe("CSVFileName", func() {
2828
It("sanitizes group names correctly", func() {
2929
yamlContent := `
3030
---
@@ -44,7 +44,31 @@ instances:
4444
Expect(err).ToNot(HaveOccurred())
4545
Expect(cfg).ToNot(BeNil())
4646

47-
Expect(cfg.Instances[0].Groups[0].SanitizedGroupName()).To(Equal("foo-_.aeoeueAeOeUe-group"))
47+
Expect(cfg.Instances[0].Groups[0].CSVFileName()).To(Equal("foo-_.aeoeueAeOeUe-group.csv"))
48+
})
49+
})
50+
51+
var _ = Describe("BlocklistFileName", func() {
52+
It("sanitizes group names correctly", func() {
53+
yamlContent := `
54+
---
55+
instances:
56+
- hostname: foo
57+
token_name: foo
58+
groups:
59+
- name: foo- ,äöüÄÖÜ-group
60+
fields:
61+
- foo_field_1
62+
`
63+
_, err := tempFile.Write([]byte(yamlContent))
64+
Expect(err).ToNot(HaveOccurred())
65+
tempFile.Close()
66+
67+
cfg, err := config.LoadConfig(tempFile.Name())
68+
Expect(err).ToNot(HaveOccurred())
69+
Expect(cfg).ToNot(BeNil())
70+
71+
Expect(cfg.Instances[0].Groups[0].BlocklistFileName()).To(Equal("foo-_.aeoeueAeOeUe-group.yml"))
4872
})
4973
})
5074
})

csv/blocklist_data_provider.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ func NewBlockListDataProvider(dataDir string, logger logger.Logger) BlockListDat
4444

4545
func (bp *blockListDataProvider) IsBlocked(personJson map[string]json.RawMessage, group config.Group) (bool, error) {
4646
var entry cacheEntry
47-
48-
sanitizedGroupName := group.SanitizedGroupName()
49-
50-
entry, err := bp.loadBlocklist(sanitizedGroupName)
47+
entry, err := bp.loadBlocklist(group.BlocklistFileName())
5148
if err != nil {
52-
return false, fmt.Errorf("failed to load blocklist for group %s: %w", sanitizedGroupName, err)
49+
return false, fmt.Errorf("failed to load blocklist %s: %w", group.BlocklistFileName(), err)
5350
}
5451

5552
for _, blockedAddress := range entry.data {
@@ -89,7 +86,7 @@ func (bp *blockListDataProvider) loadBlocklist(name string) (cacheEntry, error)
8986
return entry, entry.err
9087
}
9188

92-
path := filepath.Join(bp.dataDir, name+".yml")
89+
path := filepath.Join(bp.dataDir, name)
9390

9491
yamlData, err := os.ReadFile(path)
9592
if errors.Is(err, fs.ErrNotExist) {
@@ -135,7 +132,7 @@ func (bp *blockListDataProvider) loadBlocklist(name string) (cacheEntry, error)
135132
}
136133

137134
func (bp *blockListDataProvider) BlockListExists(group config.Group) bool {
138-
blocklistFilePath := filepath.Join(bp.dataDir, group.SanitizedGroupName()+".yml")
135+
blocklistFilePath := filepath.Join(bp.dataDir, group.BlocklistFileName())
139136
_, err := os.Stat(blocklistFilePath)
140137
if err != nil {
141138
if os.IsNotExist(err) {

0 commit comments

Comments
 (0)