-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.go
More file actions
172 lines (156 loc) · 3.46 KB
/
utils.go
File metadata and controls
172 lines (156 loc) · 3.46 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bytes"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"text/template"
"time"
"github.com/fsnotify/fsnotify"
)
func addPathRecursively(root string, watcher *fsnotify.Watcher) error {
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() || slices.Contains(excludedFolders, strings.ToLower(d.Name())) {
return nil
}
return watcher.Add(path)
})
return err
}
func parseCommand(cmd string) *exec.Cmd {
cmd = strings.TrimSpace(cmd)
if cmd == "" {
return nil
}
return exec.Command("sh", "-c", cmd)
}
func wrapCmd(cmd *exec.Cmd, data EventData) *exec.Cmd {
if cmd != nil {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(),
fmt.Sprintf("WATCHER_EVENT=%s", data.Op),
fmt.Sprintf("WATCHER_PATH=%s", data.Path),
fmt.Sprintf("FILE=%s", data.Path),
fmt.Sprintf("FILE_BASE=%s", data.Base),
fmt.Sprintf("FILE_DIR=%s", data.Dir),
fmt.Sprintf("FILE_EXT=%s", data.Ext),
fmt.Sprintf("EVENT_TYPE=%s", data.Op),
fmt.Sprintf("EVENT_TIME=%s", data.Time),
fmt.Sprintf("TIMESTAMP=%d", data.Timestamp),
fmt.Sprintf("PWD=%s", data.PWD),
)
}
return cmd
}
type EventData struct {
Path string
Base string
Dir string
Ext string
Op string
Time string
Timestamp int64
PWD string
}
func getEventData(event fsnotify.Event) EventData {
absPath, err := filepath.Abs(event.Name)
if err != nil {
absPath = event.Name
}
now := time.Now()
pwd, _ := os.Getwd()
return EventData{
Path: absPath,
Base: filepath.Base(event.Name),
Dir: filepath.Dir(event.Name),
Ext: filepath.Ext(event.Name),
Op: strings.ToUpper(event.Op.String()),
Time: now.Format(time.RFC3339),
Timestamp: now.Unix(),
PWD: pwd,
}
}
func expandTemplate(text string, data EventData) string {
tmpl, err := template.New("cmd").Parse(text)
if err != nil {
fmt.Fprintf(logger, "watcher: error parsing template %q: %s\n", text, err.Error())
return text
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
fmt.Fprintf(logger, "watcher: error executing template %q: %s\n", text, err.Error())
return text
}
return buf.String()
}
func shouldProcess(path string, include, exclude []string) bool {
// Check exclude first
if matches(path, exclude) {
return false
}
// If include is empty, everything is included
if len(include) == 0 {
return true
}
return matches(path, include)
}
func matches(path string, patterns []string) bool {
for _, p := range patterns {
if matchesPattern(path, p) {
return true
}
}
return false
}
func matchesPattern(path, pattern string) bool {
if pattern == "" {
return true
}
// Try matching the full path
if m, _ := filepath.Match(pattern, path); m {
return true
}
// Try matching the base name
if m, _ := filepath.Match(pattern, filepath.Base(path)); m {
return true
}
return false
}
var excludedFolders = []string{
"node_modules",
"vendor",
".git",
".svn",
".hg",
".bzr",
".vscode",
"_vendor",
"godeps",
"dist",
"thirdparty",
"bin",
"__pycache__",
".cache",
"obj",
"testdata",
"examples",
"tmp",
"build",
}
func validPath(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// fatalf prints a formatted error message to stderr and exits with status code 1
func fatalf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}