forked from hashicorp/otto
/
config.go
96 lines (81 loc) · 2.24 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package detect
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
)
// Config is the format of the configuration files
type Config struct {
Detectors []*Detector
}
// Merge merges another config into this one. This will modify this
// Config object. Detectors in c2 are tried after detectors in this
// Config. Conflicts are ignored as lower priority detectors, meaning that
// if two detectors are for type "go", both will be tried.
func (c *Config) Merge(c2 *Config) error {
c.Detectors = append(c.Detectors, c2.Detectors...)
return nil
}
// Detector is something that detects a single type.
type Detector struct {
// Type is the type that will match if this detector matches
Type string
// File is a list of file globs to look for. If any are found, it is
// a match.
File []string
// Contents is a content matcher. The key is a filename and the
// path is the file contents regular expression.
Contents map[string]string
// Priority is used to break ties when two detectors conflict.
// The detector with the higher (larger number) priority wins.
// This defaults to 0. Negative numbers can be used to lower
// priority and positive numbers can be used to increase it.
//
// In most cases, a priority never needs to be set. Please set this
// with care.
Priority int
}
// Detect will return true if this detector matches within the given
// directory.
func (d *Detector) Detect(dir string) (bool, error) {
// First test files
for _, pattern := range d.File {
matches, err := filepath.Glob(filepath.Join(dir, pattern))
if err != nil {
return false, err
}
if len(matches) > 0 {
return true, nil
}
}
// Test contents
for k, v := range d.Contents {
path := filepath.Join(dir, k)
if _, err := os.Stat(path); err != nil {
continue
}
if ok, err := d.matchContents(path, v); err != nil {
return false, err
} else if ok {
return true, nil
}
}
return false, nil
}
func (d *Detector) matchContents(path string, raw string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer f.Close()
re, err := regexp.Compile(raw)
if err != nil {
return false, err
}
return re.MatchReader(bufio.NewReader(f)), nil
}
func (d *Detector) GoString() string {
return fmt.Sprintf("*%#v", *d)
}