/
whitelist.go
54 lines (43 loc) · 1.32 KB
/
whitelist.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
42
43
44
45
46
47
48
49
50
51
52
53
54
package crawler
import (
"fmt"
"io/ioutil"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
var fileReaderInject = ioutil.ReadFile
// Whitelist contain a list of Public Administrations.
type Whitelist []PA
// PA is a Public Administration.
type PA struct {
Name string `yaml:"name"`
CodiceIPA string `yaml:"codice-iPA"`
Organizations []string `yaml:"orgs"`
Repositories []string `yaml:"repos"`
UnknownIPA bool `yaml:"unknown-iPA"`
}
// ReadAndParseWhitelist read the whitelist and return the parsed content in a slice of PA.
func ReadAndParseWhitelist(whitelistFile string) ([]PA, error) {
// Open and read whitelist file.
data, err := fileReaderInject(whitelistFile)
if err != nil {
return nil, fmt.Errorf("error in reading %s file: %v", whitelistFile, err)
}
// Parse whitelist file.
whitelist, err := parseWhitelistFile(data)
if err != nil {
return nil, fmt.Errorf("error in parsing %s file: %v", whitelistFile, err)
}
log.Infof("Loaded and parsed %s", whitelistFile)
return whitelist, err
}
// parseWhitelistFile parses the whitelist file to build a slice of PA.
func parseWhitelistFile(data []byte) ([]PA, error) {
var whitelist []PA
// Unmarshal the yml in domains list.
err := yaml.Unmarshal(data, &whitelist)
if err != nil {
return nil, err
}
return whitelist, err
}