Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variable handling with envconfig #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
70 changes: 70 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
package core

import (
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions go.mod
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
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