forked from maxwells/sanitize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitelist_parser.go
41 lines (33 loc) · 923 Bytes
/
whitelist_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package sanitize
import (
"encoding/json"
"os"
)
// Load a new whitelist from a JSON file
func WhitelistFromFile(filepath string) (*Whitelist, error) {
bytes, err := readFileToBytes(filepath)
if err != nil {
return nil, err
}
whitelist, err := NewWhitelist(bytes)
return whitelist, nil
}
// helper function to read entirety of provided file into byte slice
func readFileToBytes(filepath string) ([]byte, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
// prepare byte slice to read json file into
fileInfo, err := f.Stat()
bytes := make([]byte, fileInfo.Size())
_, err = f.Read(bytes)
return bytes, err
}
// Create a new whitelist from JSON configuration
func NewWhitelist(jsonData []byte) (*Whitelist, error) {
// unmarshal json file into contract-free interface
configuration := &Whitelist{}
err := json.Unmarshal(jsonData, configuration)
return configuration, err
}