Skip to content

Commit

Permalink
Add default .woke.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
caitlinelfring committed Sep 3, 2020
1 parent 94f1ddb commit de427db
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ This has whitelist from stdin

A set of default rules is provided in [`pkg/rule/default.go`](https://github.com/get-woke/woke/blob/main/pkg/rule/default.go).

Configure your custom rules config in `.woke.yaml` or `.woke.yml`, `woke` will pick up one of these files in the cwd of where you run `woke` from.
This file will be picked up automatically up your customizations automatically!

See [example.yaml](https://github.com/get-woke/woke/blob/example.yaml) for an example of adding custom rules.
You can supply your own rules with `-c path/to/rules.yaml`
You can also supply your own rules with `-c path/to/rules.yaml` if you want to handle different rulesets.

The syntax for rules is very basic. You just need a name, a list of terms to match that violate the rule,
and a list of alternative suggestions.
Expand Down
41 changes: 39 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"io/ioutil"
"os"

"github.com/get-woke/woke/pkg/rule"
"github.com/rs/zerolog"
Expand All @@ -10,6 +11,8 @@ import (
"gopkg.in/yaml.v2"
)

var defaultConfigFilenames = []string{".woke.yaml", ".woke.yml"}

// Config contains a list of rules
type Config struct {
Rules []*rule.Rule `yaml:"rules"`
Expand All @@ -26,6 +29,12 @@ func NewConfig(filename string) (*Config, error) {
}
// Ignore the config filename, it will always match on its own rules
c.IgnoreFiles = append(c.IgnoreFiles, filename)
} else {
if defaultCfg := loadDefaultConfigFiles(); defaultCfg != nil {
c = *defaultCfg

c.IgnoreFiles = append(c.IgnoreFiles, defaultConfigFilenames...)
}
}

c.AddDefaultRules()
Expand Down Expand Up @@ -61,9 +70,37 @@ func (c *Config) AddDefaultRules() {
}

func (c *Config) load(filename string) error {
yamlFile, err := ioutil.ReadFile(filename)
cfg, err := loadConfig(filename)
if err != nil {
return err
}
return yaml.Unmarshal(yamlFile, c)
*c = *cfg
return nil
}

func loadConfig(filename string) (*Config, error) {
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var c Config
err = yaml.Unmarshal(yamlFile, &c)

return &c, err
}

func loadDefaultConfigFiles() (cfg *Config) {
for _, file := range defaultConfigFilenames {
log.Debug().Str("cfg", file).Msg("trying default config file")
if _, err := os.Stat(file); os.IsNotExist(err) {
continue
}
var err error
cfg, err = loadConfig(file)
if err == nil && cfg != nil {
log.Debug().Str("cfg", file).Msg("found default config file!")
return
}
}
return
}
5 changes: 5 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func TestNewConfig(t *testing.T) {
IgnoreFiles: []string(nil),
}
assert.Equal(t, expectedEmpty, c)

defaultConfigFilenames = []string{"testdata/default.yaml"}
c, err = NewConfig("")
assert.NoError(t, err)
assert.EqualValues(t, defaultConfigFilenames, c.IgnoreFiles)
})

t.Run("config-missing", func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/testdata/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rules:
- name: rule1

0 comments on commit de427db

Please sign in to comment.