Skip to content

Commit

Permalink
Simplify ignores
Browse files Browse the repository at this point in the history
  • Loading branch information
caitlinelfring committed Aug 24, 2020
1 parent 696d41f commit 8c3e280
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 93 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/caitlinelfring/woke
go 1.14

require (
github.com/gobwas/glob v0.2.3
github.com/rs/zerolog v1.19.0
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94
github.com/spf13/cobra v1.0.0
gopkg.in/yaml.v2 v2.3.0
)
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand All @@ -24,8 +25,6 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -66,6 +65,7 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
Expand All @@ -81,6 +81,8 @@ github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.19.0 h1:hYz4ZVdUgjXTBUmrkrw55j1nHx68LfOKIQk5IYtyScg=
github.com/rs/zerolog v1.19.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 h1:G04eS0JkAIVZfaJLjla9dNxkJCPiKIGZlw9AfOhzOD0=
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94/go.mod h1:b18R55ulyQ/h3RaWyloPyER7fWQVZvimKKhnI5OfrJQ=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
Expand All @@ -97,6 +99,7 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
Expand Down
109 changes: 19 additions & 90 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package config

import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/caitlinelfring/woke/pkg/rule"
"github.com/caitlinelfring/woke/pkg/util"
"github.com/gobwas/glob"
"github.com/rs/zerolog/log"

gitignore "github.com/sabhiram/go-gitignore"
"gopkg.in/yaml.v2"
)

Expand All @@ -21,44 +16,40 @@ type Config struct {
Rules []*rule.Rule `yaml:"rules"`
IgnoreFiles []string `yaml:"ignore_files"`

ignoreFilesGlob []glob.Glob
_gitIgnore *gitignore.GitIgnore
hasAbsolutePath bool

files []string
}

// DefaultIgnore is the default list of file globs that will be ignored
var DefaultIgnore = []string{
".git/*",
".git",
}

func NewConfig(filename string) (*Config, error) {
var c Config
var err error

// No filename given, use default rules
if filename == "" {
c.SetDefaultRules()
} else {
err = c.load(filename)
if len(c.Rules) == 0 {
c.SetDefaultRules()

if filename != "" {
if err := c.load(filename); err != nil {
return &c, err
}
}
c.AddDefaultRules()

// Ignore the config filename, it will always match on its own rules
c.IgnoreFiles = append(c.IgnoreFiles, filename)
c.compileIgnoreGlobs()

return &c, err
return &c, nil
}

// SetFiles computes the list of files that will be checked
func (c *Config) SetFiles(fileGlobs []string) {
allFiles, hasAbsolutePath, _ := util.GetFilesInGlobs(fileGlobs)
c.hasAbsolutePath = hasAbsolutePath
for _, f := range allFiles {
if c.ignoreFile(f) {
if c._gitIgnore.MatchesPath(f) {
continue
}
c.files = append(c.files, f)
Expand All @@ -68,85 +59,23 @@ func (c *Config) SetFiles(fileGlobs []string) {
// compileIgnoreGlobs pre-compiles globs
// See https://github.com/gobwas/glob#performance
func (c *Config) compileIgnoreGlobs() {
c.ignoreFilesGlob = make([]glob.Glob, 0)

for _, ignore := range c.IgnoreFiles {
c.addIgnoreGlob(ignore)
}

for _, ignore := range gitIgnore() {
c.addIgnoreGlob(ignore)
}

for _, ignore := range DefaultIgnore {
c.addIgnoreGlob(ignore)
}
}

func (c *Config) addIgnoreGlob(path string) {
if c.hasAbsolutePath {
var err error
path, err = filepath.Abs(path)
if err != nil {
log.Error().
Err(err).
Str("path", path).
Msg("failed to get absolute path")
return
}
}
c.ignoreFilesGlob = append(c.ignoreFilesGlob, glob.MustCompile(path))
}

func gitIgnore() (lines []string) {
buffer, err := os.Open(".gitignore")
if err != nil {
return
}

defer func() {
if err = buffer.Close(); err != nil {
log.Error().
Err(err).
Msg("gitignore buffer failed to close")
}
}()

commentRe := regexp.MustCompile(`#(.*)$`)
scanner := bufio.NewScanner(buffer)
for scanner.Scan() {
// Remove comments
text := commentRe.ReplaceAllString(scanner.Text(), "")
text = strings.TrimSpace(text)
if text == "" {
continue
}
log.Debug().Str("entry", text).Msg("adding gitignore entry")
lines = append(lines, text)
}
if err = scanner.Err(); err != nil {
log.Info().Err(err).Msg("gitignore scanner error")
}
return
}

func (c *Config) ignoreFile(f string) bool {
for _, ignore := range c.ignoreFilesGlob {
if ignore.Match(f) {
return true
}
ignoreLines := []string{}
if buffer, err := ioutil.ReadFile(".gitignore"); err == nil {
ignoreLines = append(ignoreLines, strings.Split(string(buffer), "\n")...)
}
return false
ignoreLines = append(ignoreLines, c.IgnoreFiles...)
ignoreLines = append(ignoreLines, DefaultIgnore...)
c._gitIgnore, _ = gitignore.CompileIgnoreLines(ignoreLines...)
}

// GetFiles returns files that may be parsed
func (c *Config) GetFiles() []string {
return c.files
}

// SetDefaultRules sets the config Rules to DefaultRules
func (c *Config) SetDefaultRules() {
c.Rules = rule.DefaultRules
// AddDefaultRules adds the config Rules to DefaultRules
func (c *Config) AddDefaultRules() {
c.Rules = append(c.Rules, rule.DefaultRules...)
}

func (c *Config) load(filename string) error {
Expand Down

0 comments on commit 8c3e280

Please sign in to comment.