Skip to content

Commit 7369285

Browse files
committed
Environment variable handling with envconfig
Documentation for environment variables
1 parent f4fdbda commit 7369285

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ signatures: # list of signatures to check
8888
name: '' # name of the signature
8989
```
9090

91+
Note that some configuration can be also overwritten with environment varibles. There are:
92+
- `github_access_tokens`, environment variable `GITHUB_ACCESS_TOKENS`
93+
- `slack_webhook`, environment variable `SLACK_WEBHOOK`
94+
9195
#### Signatures
9296

9397
shhgit comes with 120 signatures. You can remove or add more by editing `config.yaml`.

core/config.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
package core
23

34
import (
@@ -67,3 +68,72 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
6768

6869
return nil
6970
}
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

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/fatih/color v1.7.0
77
github.com/google/go-github v17.0.0+incompatible
88
github.com/google/go-querystring v1.0.0 // indirect
9+
github.com/kelseyhightower/envconfig v1.4.0
910
github.com/mattn/go-colorable v0.1.2 // indirect
1011
github.com/mattn/go-isatty v0.0.9 // indirect
1112
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO
2323
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
2424
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
2525
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
26+
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
27+
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
2628
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY=
2729
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
2830
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=

0 commit comments

Comments
 (0)