|
1 | | -package core |
2 | | - |
3 | | -import ( |
4 | | - "errors" |
5 | | - "io/ioutil" |
6 | | - "os" |
7 | | - "path" |
8 | | - "strings" |
9 | | - |
10 | | - "gopkg.in/yaml.v3" |
11 | | -) |
12 | | - |
13 | | -type Config struct { |
14 | | - GitHubAccessTokens []string `yaml:"github_access_tokens"` |
15 | | - SlackWebhook string `yaml:"slack_webhook,omitempty"` |
16 | | - BlacklistedExtensions []string `yaml:"blacklisted_extensions"` |
17 | | - BlacklistedPaths []string `yaml:"blacklisted_paths"` |
18 | | - BlacklistedEntropyExtensions []string `yaml:"blacklisted_entropy_extensions"` |
19 | | - Signatures []ConfigSignature `yaml:"signatures"` |
20 | | -} |
21 | | - |
22 | | -type ConfigSignature struct { |
23 | | - Name string `yaml:"name"` |
24 | | - Part string `yaml:"part"` |
25 | | - Match string `yaml:"match,omitempty"` |
26 | | - Regex string `yaml:"regex,omitempty"` |
27 | | - Verifier string `yaml:"verifier,omitempty"` |
28 | | -} |
29 | | - |
30 | | -func ParseConfig() (*Config, error) { |
31 | | - config := &Config{} |
32 | | - |
33 | | - dir, _ := os.Getwd() |
34 | | - data, err := ioutil.ReadFile(path.Join(dir, "config.yaml")) |
35 | | - if err != nil { |
36 | | - return config, err |
37 | | - } |
38 | | - |
39 | | - err = yaml.Unmarshal(data, config) |
40 | | - if err != nil { |
41 | | - return config, err |
42 | | - } |
43 | | - |
44 | | - for i := 0; i < len(config.GitHubAccessTokens); i++ { |
45 | | - config.GitHubAccessTokens[i] = os.ExpandEnv(config.GitHubAccessTokens[i]) |
46 | | - } |
47 | | - |
48 | | - if len(config.SlackWebhook) > 0 { |
49 | | - config.SlackWebhook = os.ExpandEnv(config.SlackWebhook) |
50 | | - } |
51 | | - |
52 | | - return config, nil |
53 | | -} |
54 | | - |
55 | | -func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { |
56 | | - *c = Config{} |
57 | | - type plain Config |
58 | | - err := unmarshal((*plain)(c)) |
59 | | - |
60 | | - if err != nil { |
61 | | - return err |
62 | | - } |
63 | | - |
64 | | - if len(c.GitHubAccessTokens) < 1 || strings.TrimSpace(strings.Join(c.GitHubAccessTokens, "")) == "" { |
65 | | - return errors.New("You need to provide at least one GitHub Access Token. See https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line") |
66 | | - } |
67 | | - |
68 | | - return nil |
69 | | -} |
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "gopkg.in/yaml.v3" |
| 11 | +) |
| 12 | + |
| 13 | +type Config struct { |
| 14 | + GitHubAccessTokens []string `yaml:"github_access_tokens"` |
| 15 | + SlackWebhook string `yaml:"slack_webhook,omitempty"` |
| 16 | + BlacklistedExtensions []string `yaml:"blacklisted_extensions"` |
| 17 | + BlacklistedPaths []string `yaml:"blacklisted_paths"` |
| 18 | + BlacklistedEntropyExtensions []string `yaml:"blacklisted_entropy_extensions"` |
| 19 | + Signatures []ConfigSignature `yaml:"signatures"` |
| 20 | +} |
| 21 | + |
| 22 | +type ConfigSignature struct { |
| 23 | + Name string `yaml:"name"` |
| 24 | + Part string `yaml:"part"` |
| 25 | + Match string `yaml:"match,omitempty"` |
| 26 | + Regex string `yaml:"regex,omitempty"` |
| 27 | + Verifier string `yaml:"verifier,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +func ParseConfig() (*Config, error) { |
| 31 | + config := &Config{} |
| 32 | + |
| 33 | + dir, _ := os.Getwd() |
| 34 | + data, err := ioutil.ReadFile(path.Join(dir, "config.yaml")) |
| 35 | + if err != nil { |
| 36 | + return config, err |
| 37 | + } |
| 38 | + |
| 39 | + err = yaml.Unmarshal(data, config) |
| 40 | + if err != nil { |
| 41 | + return config, err |
| 42 | + } |
| 43 | + |
| 44 | + for i := 0; i < len(config.GitHubAccessTokens); i++ { |
| 45 | + config.GitHubAccessTokens[i] = os.ExpandEnv(config.GitHubAccessTokens[i]) |
| 46 | + } |
| 47 | + |
| 48 | + if len(config.SlackWebhook) > 0 { |
| 49 | + config.SlackWebhook = os.ExpandEnv(config.SlackWebhook) |
| 50 | + } |
| 51 | + |
| 52 | + return config, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 56 | + *c = Config{} |
| 57 | + type plain Config |
| 58 | + err := unmarshal((*plain)(c)) |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + if len(c.GitHubAccessTokens) < 1 || strings.TrimSpace(strings.Join(c.GitHubAccessTokens, "")) == "" { |
| 65 | + return errors.New("You need to provide at least one GitHub Access Token. See https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line") |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | +} |
0 commit comments