diff --git a/README.md b/README.md index 262205b..5664780 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,10 @@ signatures: # list of signatures to check name: '' # name of the signature ``` +Note that some configuration can be also overwritten with environment varibles. There are: +- `github_access_tokens`, environment variable `GITHUB_ACCESS_TOKENS` +- `slack_webhook`, environment variable `SLACK_WEBHOOK` + #### Signatures shhgit comes with 120 signatures. You can remove or add more by editing `config.yaml`. diff --git a/core/config.go b/core/config.go index e1bfe8c..ec0a11a 100644 --- a/core/config.go +++ b/core/config.go @@ -1,3 +1,4 @@ +<<<<<<< HEAD package core import ( @@ -67,3 +68,72 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } +======= +package core + +import ( + "errors" + "io/ioutil" + "os" + "path" + "strings" + + "github.com/kelseyhightower/envconfig" + "gopkg.in/yaml.v3" +) + +type Config struct { + GitHubAccessTokens []string `yaml:"github_access_tokens" envconfig:"GITHUB_ACCESS_TOKENS"` + SlackWebhook string `yaml:"slack_webhook,omitempty" envconfig:"SLACK_WEBHOOK"` + BlacklistedExtensions []string `yaml:"blacklisted_extensions"` + BlacklistedPaths []string `yaml:"blacklisted_paths"` + BlacklistedEntropyExtensions []string `yaml:"blacklisted_entropy_extensions"` + Signatures []ConfigSignature `yaml:"signatures"` +} + +type ConfigSignature struct { + Name string `yaml:"name"` + Part string `yaml:"part"` + Match string `yaml:"match,omitempty"` + Regex string `yaml:"regex,omitempty"` + Verifier string `yaml:"verifier,omitempty"` +} + +func ParseConfig() (*Config, error) { + config := &Config{} + + dir, _ := os.Getwd() + data, err := ioutil.ReadFile(path.Join(dir, "config.yaml")) + if err != nil { + return config, err + } + + err = yaml.Unmarshal(data, config) + if err != nil { + return config, err + } + + return config, nil +} + +func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { + *c = Config{} + type plain Config + err := unmarshal((*plain)(c)) + + if err != nil { + return err + } + + err = envconfig.Process("", c) + if err != nil { + return err + } + + if len(c.GitHubAccessTokens) < 1 || strings.TrimSpace(strings.Join(c.GitHubAccessTokens, "")) == "" { + 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") + } + + return nil +} +>>>>>>> Environment variable handling with envconfig diff --git a/go.mod b/go.mod index a0de0e8..3397f73 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/fatih/color v1.7.0 github.com/google/go-github v17.0.0+incompatible github.com/google/go-querystring v1.0.0 // indirect + github.com/kelseyhightower/envconfig v1.4.0 github.com/mattn/go-colorable v0.1.2 // indirect github.com/mattn/go-isatty v0.0.9 // indirect golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 diff --git a/go.sum b/go.sum index 7709a88..1f9e358 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,8 @@ github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY= github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=