Skip to content

Commit c6db4a2

Browse files
Add .wokeignore functionality
1 parent b25dffc commit c6db4a2

File tree

5 files changed

+87
-65
lines changed

5 files changed

+87
-65
lines changed

Diff for: cmd/root.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"time"
3030

3131
"github.com/caitlinelfring/woke/pkg/config"
32+
"github.com/caitlinelfring/woke/pkg/ignore"
3233
"github.com/caitlinelfring/woke/pkg/parser"
3334
"github.com/caitlinelfring/woke/pkg/rule"
3435
"github.com/rs/zerolog"
@@ -82,11 +83,8 @@ Provide a list file globs for files you'd like to check.`,
8283
if stdin {
8384
results, _ = p.Parse(os.Stdin)
8485
} else {
85-
if len(args) == 0 {
86-
args = []string{defaultGlob}
87-
}
88-
c.SetFiles(args)
89-
results = p.ParseFiles(c.GetFiles())
86+
ignorer, _ := ignore.NewIgnore(c.IgnoreFiles)
87+
results = p.ParseFiles(args, ignorer)
9088
}
9189

9290
cmd.Println(results.String())

Diff for: pkg/config/config.go

+3-33
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package config
33
import (
44
"io/ioutil"
55

6-
"github.com/caitlinelfring/woke/pkg/ignore"
76
"github.com/caitlinelfring/woke/pkg/rule"
8-
"github.com/caitlinelfring/woke/pkg/util"
97

108
"gopkg.in/yaml.v2"
119
)
@@ -14,51 +12,23 @@ import (
1412
type Config struct {
1513
Rules []*rule.Rule `yaml:"rules"`
1614
IgnoreFiles []string `yaml:"ignore_files"`
17-
18-
hasAbsolutePath bool
19-
20-
ignoreMatcherFunc func(string) bool
21-
22-
files []string
2315
}
2416

2517
func NewConfig(filename string) (*Config, error) {
2618
var c Config
19+
c.AddDefaultRules()
2720

2821
if filename != "" {
2922
if err := c.load(filename); err != nil {
3023
return &c, err
3124
}
25+
// Ignore the config filename, it will always match on its own rules
26+
c.IgnoreFiles = append(c.IgnoreFiles, filename)
3227
}
33-
c.AddDefaultRules()
34-
35-
// Ignore the config filename, it will always match on its own rules
36-
ignorer, err := ignore.NewIgnore(append(c.IgnoreFiles, filename))
37-
if err != nil {
38-
return &c, err
39-
}
40-
c.ignoreMatcherFunc = ignorer.Match
4128

4229
return &c, nil
4330
}
4431

45-
// SetFiles computes the list of files that will be checked
46-
func (c *Config) SetFiles(fileGlobs []string) {
47-
allFiles, hasAbsolutePath, _ := util.GetFilesInGlobs(fileGlobs)
48-
c.hasAbsolutePath = hasAbsolutePath
49-
for _, f := range allFiles {
50-
if c.ignoreMatcherFunc(f) {
51-
continue
52-
}
53-
c.files = append(c.files, f)
54-
}
55-
}
56-
57-
// GetFiles returns files that may be parsed
58-
func (c *Config) GetFiles() []string {
59-
return c.files
60-
}
61-
6232
// AddDefaultRules adds the config Rules to DefaultRules
6333
func (c *Config) AddDefaultRules() {
6434
c.Rules = append(c.Rules, rule.DefaultRules...)

Diff for: pkg/ignore/ignore.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package ignore
22

33
import (
4+
"errors"
45
"io/ioutil"
6+
"os"
57
"strings"
68

9+
"github.com/rs/zerolog/log"
710
gitignore "github.com/sabhiram/go-gitignore"
811
)
912

@@ -33,10 +36,21 @@ func (i *Ignore) Match(f string) bool {
3336

3437
func compileIgnoreLines(lines []string) (*gitignore.GitIgnore, error) {
3538
lines = append(lines, DefaultIgnores...)
39+
lines = append(lines, readIgnoreFile(".gitignore")...)
40+
lines = append(lines, readIgnoreFile(".wokeignore")...)
3641

37-
if buffer, err := ioutil.ReadFile(".gitignore"); err == nil {
38-
lines = append(lines, strings.Split(string(buffer), "\n")...)
42+
return gitignore.CompileIgnoreLines(lines...)
43+
}
44+
45+
func readIgnoreFile(file string) []string {
46+
buffer, err := ioutil.ReadFile(file)
47+
if err != nil {
48+
_event := log.Warn()
49+
if errors.Is(err, os.ErrNotExist) {
50+
_event = log.Debug()
51+
}
52+
_event.Err(err).Str("file", file).Msg("skipping ignorefile")
3953
}
4054

41-
return gitignore.CompileIgnoreLines(lines...)
55+
return strings.Split(string(buffer), "\n")
4256
}

Diff for: pkg/parser/files.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package parser
2+
3+
import (
4+
"github.com/caitlinelfring/woke/pkg/ignore"
5+
"github.com/caitlinelfring/woke/pkg/util"
6+
)
7+
8+
const defaultGlob = "**"
9+
10+
// Parsable contains the list of files that can be parsed
11+
type Parsable struct {
12+
Files []string
13+
}
14+
15+
// ParsableFiles returns a list of files that can be parsed after the ignorer has
16+
// excluded files that should be ignored
17+
func ParsableFiles(fileGlobs []string, ignorer *ignore.Ignore) *Parsable {
18+
if len(fileGlobs) == 0 {
19+
fileGlobs = []string{defaultGlob}
20+
}
21+
22+
var p Parsable
23+
allFiles, _, _ := util.GetFilesInGlobs(fileGlobs)
24+
for _, f := range allFiles {
25+
if ignorer.Match(f) {
26+
continue
27+
}
28+
p.Files = append(p.Files, f)
29+
}
30+
31+
return &p
32+
}

Diff for: pkg/parser/parser.go

+32-24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"time"
77

8+
"github.com/caitlinelfring/woke/pkg/ignore"
89
"github.com/caitlinelfring/woke/pkg/rule"
910
"github.com/caitlinelfring/woke/pkg/util"
1011
"github.com/rs/zerolog/log"
@@ -16,30 +17,9 @@ type Parser struct {
1617
}
1718

1819
// ParseFiles parses all files provided and returns the results
19-
func (p *Parser) ParseFiles(files []string) rule.Results {
20-
results := rule.Results{}
21-
22-
for _, f := range files {
23-
file, err := os.Open(f)
24-
if err != nil {
25-
log.Error().Err(err).Str("file", file.Name()).Msg("could not open file")
26-
continue
27-
}
28-
defer file.Close()
29-
30-
if err = util.IsTextFile(file); err != nil {
31-
log.Debug().Err(err).Str("file", file.Name()).Msg("not a text file")
32-
continue
33-
}
34-
35-
r, err := p.Parse(file)
36-
if err != nil {
37-
log.Debug().Err(err).Msg("parser failed")
38-
continue
39-
}
40-
results.Push(r.Results...)
41-
}
42-
return results
20+
func (p *Parser) ParseFiles(files []string, ignorer *ignore.Ignore) rule.Results {
21+
parsable := ParsableFiles(files, ignorer)
22+
return p.parseFiles(parsable.Files)
4323
}
4424

4525
// Parse reads the file and returns results of places where rules are broken
@@ -70,3 +50,31 @@ func (p *Parser) Parse(file *os.File) (results rule.Results, err error) {
7050

7151
return results, scanner.Err()
7252
}
53+
54+
func (p *Parser) parseFiles(files []string) rule.Results {
55+
results := rule.Results{}
56+
57+
for _, f := range files {
58+
file, err := os.Open(f)
59+
if err != nil {
60+
log.Error().Err(err).Str("file", f).Msg("could not open file")
61+
continue
62+
}
63+
defer file.Close()
64+
65+
if err = util.IsTextFile(file); err != nil {
66+
log.Debug().Err(err).Str("file", file.Name()).Msg("not a text file")
67+
continue
68+
}
69+
70+
r, err := p.Parse(file)
71+
if err != nil {
72+
log.Debug().Err(err).Msg("parser failed")
73+
continue
74+
}
75+
76+
results.Push(r.Results...)
77+
}
78+
79+
return results
80+
}

0 commit comments

Comments
 (0)