-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutilities.go
87 lines (77 loc) · 1.73 KB
/
utilities.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
package main
import (
"io"
"os"
"path/filepath"
"runtime/debug"
"strings"
"github.com/bmatcuk/doublestar/v4"
"github.com/coveooss/gotemplate/v3/template"
"github.com/coveooss/multilogger/errors"
goerrors "github.com/go-errors/errors"
)
var must = errors.Must
func cleanup() {
os.RemoveAll(tempFolder)
if err := recover(); err != nil {
switch err := err.(type) {
case errors.Managed:
errors.Print(err)
case *goerrors.Error:
errors.Printf(err.ErrorStack())
default:
errors.Printf("%T: %[1]v\n%s", err, string(debug.Stack()))
}
}
}
func readStdin() string {
if stdinContent != "" {
return stdinContent
}
stdinContent = string(must(io.ReadAll(os.Stdin)).([]byte))
return stdinContent
}
var stdinContent string
func exclude(files []string, patterns []string) (result []string, err error) {
if patterns = extend(patterns); len(patterns) == 0 {
// There is no exclusion pattern, so we return the list of files as is
result = files
return
}
result = make([]string, 0, len(files))
for i, file := range files {
file, err = filepath.Abs(file)
if err != nil {
return
}
var excluded bool
for _, pattern := range patterns {
pattern, err = filepath.Abs(pattern)
if err != nil {
return
}
if excluded, err = doublestar.PathMatch(pattern, file); err != nil {
return
} else if excluded {
template.InternalLog.Tracef("%s ignored", files[i])
break
}
}
if !excluded {
result = append(result, files[i])
}
}
return
}
func extend(values []string) []string {
result := make([]string, 0, len(values))
for i := range values {
for _, sv := range strings.Split(values[i], ",") {
sv = strings.TrimSpace(sv)
if sv != "" {
result = append(result, sv)
}
}
}
return result
}