|
| 1 | +<<<<<<< HEAD |
1 | 2 | package core |
2 | 3 |
|
3 | 4 | import ( |
@@ -67,3 +68,72 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { |
67 | 68 |
|
68 | 69 | return nil |
69 | 70 | } |
| 71 | +======= |
| 72 | +package core |
| 73 | + |
| 74 | +import ( |
| 75 | + "errors" |
| 76 | + "io/ioutil" |
| 77 | + "os" |
| 78 | + "path" |
| 79 | + "strings" |
| 80 | + |
| 81 | + "github.com/kelseyhightower/envconfig" |
| 82 | + "gopkg.in/yaml.v3" |
| 83 | +) |
| 84 | + |
| 85 | +type Config struct { |
| 86 | + GitHubAccessTokens []string `yaml:"github_access_tokens" envconfig:"GITHUB_ACCESS_TOKENS"` |
| 87 | + SlackWebhook string `yaml:"slack_webhook,omitempty" envconfig:"SLACK_WEBHOOK"` |
| 88 | + BlacklistedExtensions []string `yaml:"blacklisted_extensions"` |
| 89 | + BlacklistedPaths []string `yaml:"blacklisted_paths"` |
| 90 | + BlacklistedEntropyExtensions []string `yaml:"blacklisted_entropy_extensions"` |
| 91 | + Signatures []ConfigSignature `yaml:"signatures"` |
| 92 | +} |
| 93 | + |
| 94 | +type ConfigSignature struct { |
| 95 | + Name string `yaml:"name"` |
| 96 | + Part string `yaml:"part"` |
| 97 | + Match string `yaml:"match,omitempty"` |
| 98 | + Regex string `yaml:"regex,omitempty"` |
| 99 | + Verifier string `yaml:"verifier,omitempty"` |
| 100 | +} |
| 101 | + |
| 102 | +func ParseConfig() (*Config, error) { |
| 103 | + config := &Config{} |
| 104 | + |
| 105 | + dir, _ := os.Getwd() |
| 106 | + data, err := ioutil.ReadFile(path.Join(dir, "config.yaml")) |
| 107 | + if err != nil { |
| 108 | + return config, err |
| 109 | + } |
| 110 | + |
| 111 | + err = yaml.Unmarshal(data, config) |
| 112 | + if err != nil { |
| 113 | + return config, err |
| 114 | + } |
| 115 | + |
| 116 | + return config, nil |
| 117 | +} |
| 118 | + |
| 119 | +func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 120 | + *c = Config{} |
| 121 | + type plain Config |
| 122 | + err := unmarshal((*plain)(c)) |
| 123 | + |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + err = envconfig.Process("", c) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + if len(c.GitHubAccessTokens) < 1 || strings.TrimSpace(strings.Join(c.GitHubAccessTokens, "")) == "" { |
| 134 | + 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") |
| 135 | + } |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
| 139 | +>>>>>>> Environment variable handling with envconfig |
0 commit comments