Skip to content

Commit

Permalink
Environment variable handling with envconfig
Browse files Browse the repository at this point in the history
Documentation for environment variables
  • Loading branch information
Hi-Fi committed Mar 6, 2020
1 parent c26ac4a commit dd984a0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
18 changes: 8 additions & 10 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"path"
"strings"

"github.com/kelseyhightower/envconfig"
"gopkg.in/yaml.v3"
)

type Config struct {
GitHubAccessTokens []string `yaml:"github_access_tokens"`
SlackWebhook string `yaml:"slack_webhook,omitempty"`
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"`
Expand Down Expand Up @@ -41,14 +42,6 @@ func ParseConfig() (*Config, error) {
return config, err
}

for i := 0; i < len(config.GitHubAccessTokens); i++ {
config.GitHubAccessTokens[i] = os.ExpandEnv(config.GitHubAccessTokens[i])
}

if len(config.SlackWebhook) > 0 {
config.SlackWebhook = os.ExpandEnv(config.SlackWebhook)
}

return config, nil
}

Expand All @@ -61,6 +54,11 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
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")
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit dd984a0

Please sign in to comment.