-
Notifications
You must be signed in to change notification settings - Fork 56
/
check.go
156 lines (127 loc) · 3.75 KB
/
check.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package header
import (
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/apache/skywalking-eyes/internal/logger"
lcs "github.com/apache/skywalking-eyes/pkg/license"
"github.com/bmatcuk/doublestar/v2"
)
// Check checks the license headers of the specified paths/globs.
func Check(config *ConfigHeader, result *Result) error {
for _, pattern := range config.Paths {
if err := checkPattern(pattern, result, config); err != nil {
return err
}
}
return nil
}
var seen = make(map[string]bool)
func checkPattern(pattern string, result *Result, config *ConfigHeader) error {
paths, err := glob(pattern)
if err != nil {
return err
}
for _, path := range paths {
if yes, err := config.ShouldIgnore(path); yes || err != nil {
result.Ignore(path)
continue
}
if err := checkPath(path, result, config); err != nil {
return err
}
seen[path] = true
}
return nil
}
func glob(pattern string) (matches []string, err error) {
if pattern == "." {
return doublestar.Glob("./")
}
return doublestar.Glob(pattern)
}
func checkPath(path string, result *Result, config *ConfigHeader) error {
defer func() { seen[path] = true }()
if seen[path] {
return nil
}
if yes, err := config.ShouldIgnore(path); yes || err != nil {
return err
}
pathInfo, err := os.Stat(path)
if err != nil {
return err
}
switch mode := pathInfo.Mode(); {
case mode.IsDir():
if err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if p == path { // when p is symbolic link file, it causes infinite recursive calls
return nil
}
return checkPath(p, result, config)
}); err != nil {
return err
}
case mode.IsRegular():
return CheckFile(path, config, result)
}
return nil
}
// CheckFile checks whether or not the file contains the configured license header.
func CheckFile(file string, config *ConfigHeader, result *Result) error {
if yes, err := config.ShouldIgnore(file); yes || err != nil {
if !seen[file] {
result.Ignore(file)
}
return err
}
logger.Log.Debugln("Checking file:", file)
bs, err := os.ReadFile(file)
if err != nil {
return err
}
if t := http.DetectContentType(bs); !strings.HasPrefix(t, "text/") {
logger.Log.Debugln("Ignoring file:", file, "; type:", t)
return nil
}
content := lcs.NormalizeHeader(string(bs))
expected, pattern := config.NormalizedLicense(), config.NormalizedPattern()
if satisfy(content, config, expected, pattern) {
result.Succeed(file)
} else {
logger.Log.Debugln("Content is:", content)
result.Fail(file)
}
return nil
}
func satisfy(content string, config *ConfigHeader, license string, pattern *regexp.Regexp) bool {
if index := strings.Index(content, license); strings.TrimSpace(license) != "" && index >= 0 {
return index < config.LicenseLocationThreshold
}
if pattern == nil {
return false
}
index := pattern.FindStringIndex(content)
return len(index) == 2 && index[0] < config.LicenseLocationThreshold
}