go(localbreach): tweak code,test for multidoc yaml
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2023-08-08 23:20:09 +02:00
parent 7602c7157e
commit d9c3fc1d68
Signed by: wanderer
SSH Key Fingerprint: SHA256:MdCZyJ2sHLltrLBp0xQO0O1qTW9BT/xl5nXkDvhlMCI
2 changed files with 37 additions and 9 deletions

@ -4,6 +4,8 @@
package localbreach
import (
"bytes"
"io"
"os"
"time"
@ -25,27 +27,38 @@ type BreachDataSchema struct {
}
// Load loads local breach data.
func Load(path string) (*BreachDataSchema, error) {
func Load(path string) (*[]BreachDataSchema, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
localBreach, err := loadLocalBreach(b)
lb, err := loadLocalBreach(b)
if err != nil {
return nil, err
}
return localBreach, nil
return lb, nil
}
func loadLocalBreach(b []byte) (*BreachDataSchema, error) {
localBreach := new(BreachDataSchema)
func loadLocalBreach(b []byte) (*[]BreachDataSchema, error) {
r := bytes.NewReader(b)
decoder := yaml.NewDecoder(r)
lb := make([]BreachDataSchema, 0)
err := yaml.Unmarshal(b, localBreach)
if err != nil {
return nil, err
var bds BreachDataSchema
for {
if err := decoder.Decode(&bds); err != nil {
if err != io.EOF {
return nil, err
}
break // get out when there are no more documents to read.
}
lb = append(lb, bds)
}
return localBreach, nil
return &lb, nil
}

@ -18,4 +18,19 @@ data:
- password1
- qwertyasdf
- 0987654321
---
name: test breach 2
time: 2022-11-12
isVerified: false
containsPasswds: true
containsHashes: false
containsUsernames: false
containsEmails: true
data:
emails:
- aa@bb.cc
- dfjsnlads@gmail.com
passwds:
- password1
- ""
...