Skip to content

Commit d7915f6

Browse files
committed
Make structure more modular
1 parent 9b37b4b commit d7915f6

File tree

8 files changed

+163
-132
lines changed

8 files changed

+163
-132
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
linux:
44
GOOS=linux GOARCH=amd64 go build -o inotify-proxy inotify-proxy.go
55

6+
test:
7+
GOOS=linux GOARCH=amd64 go test -v ./...
8+
69
all: linux

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/cmuench/inotify-proxy
22

33
go 1.12
44

5-
require github.com/gookit/color v1.2.7
5+
require (
6+
github.com/gookit/color v1.2.7
7+
github.com/stretchr/testify v1.3.0
8+
)

inotify-proxy.go

Lines changed: 3 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -2,122 +2,11 @@ package main
22

33
import (
44
"flag"
5-
"github.com/cmuench/inotify-proxy/internal/profile"
5+
"github.com/cmuench/inotify-proxy/internal/watcher"
66
"github.com/gookit/color"
7-
"os"
8-
"path/filepath"
97
"strings"
10-
"time"
118
)
129

13-
type NodeInfo struct {
14-
modificationUnixTime int64
15-
}
16-
17-
var fileMap = make(map[string]NodeInfo)
18-
var selectedProfile = ""
19-
20-
func shouldSkipFile(path string) bool {
21-
22-
fileExtension := filepath.Ext(path)
23-
24-
// Exclude some directories by default
25-
26-
if strings.Contains(path, "node_modules/") {
27-
return true
28-
}
29-
30-
if strings.Contains(path, ".idea/") {
31-
return true
32-
}
33-
34-
// Check profiles
35-
36-
if selectedProfile == "" {
37-
return false
38-
}
39-
40-
if selectedProfile == "magento2-theme" {
41-
if profile.Magento2ThemeProfile.IsAllowedFileExtension(fileExtension) {
42-
return false
43-
}
44-
}
45-
46-
if selectedProfile == "magento2" {
47-
if profile.Magento2.IsAllowedFileExtension(fileExtension) {
48-
return false
49-
}
50-
}
51-
52-
if selectedProfile == "vue-storefront" {
53-
if profile.VueStorefront.IsAllowedFileExtension(fileExtension) {
54-
return false
55-
}
56-
}
57-
58-
return true
59-
}
60-
61-
func isFileChanged(path string, fileInfo os.FileInfo) bool {
62-
63-
if shouldSkipFile(path) {
64-
return false
65-
}
66-
67-
nodeInfo, found := fileMap[path]
68-
69-
currentModificationTime := fileInfo.ModTime()
70-
71-
changed := false
72-
73-
if !found {
74-
nodeInfo := NodeInfo{
75-
modificationUnixTime: currentModificationTime.Unix(),
76-
}
77-
fileMap[path] = nodeInfo
78-
79-
color.Info.Println("Watching: " + path)
80-
} else {
81-
if nodeInfo.modificationUnixTime < currentModificationTime.Unix() {
82-
changed = true
83-
84-
currentTime := time.Now()
85-
86-
err := os.Chtimes(path, currentModificationTime, currentTime)
87-
88-
if err != nil {
89-
panic("Error touching file" + path)
90-
}
91-
92-
fileMap[path] = NodeInfo{
93-
modificationUnixTime: currentTime.Unix(),
94-
}
95-
}
96-
}
97-
98-
return changed
99-
}
100-
101-
func visit(path string, fileInfo os.FileInfo, err error) error {
102-
103-
if err != nil {
104-
return err
105-
}
106-
107-
if fileInfo.IsDir() {
108-
return nil
109-
}
110-
111-
fileChanged := isFileChanged(path, fileInfo)
112-
113-
if fileChanged {
114-
color.Style{color.FgGreen, color.OpBold}.Printf("Changed: %s | %s\n", path, time.Now().Format("2006-01-02T15:04:05"))
115-
}
116-
117-
return nil
118-
}
119-
120-
12110
func main() {
12211
sleepPtr := flag.Int("sleep", 2, "Cycle time in seconds. Defines time to sleep after each filesystem walk. Default 2s")
12312
profilePtr := flag.String("profile", "", "Defines a special profile with extensions to look for. This speeds up the process. Available profiles are 'magento2-theme'")
@@ -126,26 +15,13 @@ func main() {
12615

12716
includedDirectories := flag.Args()
12817

129-
selectedProfile = *profilePtr
130-
13118
// If no argument is defined, the current directory is used
13219
if len(includedDirectories) == 0 {
13320
includedDirectories = append(includedDirectories, ".")
13421
}
13522

136-
color.Style{color.FgCyan, color.OpBold}.Println("PROFILE: " + selectedProfile)
23+
color.Style{color.FgCyan, color.OpBold}.Println("PROFILE: " + *profilePtr)
13724
color.Style{color.FgCyan, color.OpBold}.Println("DIRECTORIES: " + strings.Join(includedDirectories, ","))
13825

139-
for {
140-
141-
for _, directoryToWalk := range includedDirectories {
142-
err := filepath.Walk(directoryToWalk, visit)
143-
144-
if err != nil {
145-
panic(err)
146-
}
147-
}
148-
149-
time.Sleep(time.Duration(*sleepPtr) * time.Second)
150-
}
26+
watcher.Watch(includedDirectories, *sleepPtr, *profilePtr)
15127
}

internal/profile/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ func (l *Profile) IsAllowedFileExtension(extension string) bool {
1515
}
1616

1717
var Magento2ThemeProfile = Profile{
18-
fileExtensions: []string{ ".css", ".js", ".less", ".sass" },
18+
fileExtensions: []string{ ".css", ".js", ".less", ".sass", ".ts" },
1919
}
2020

21-
var Magento2 = Profile{
22-
fileExtensions: []string{ ".css", ".html", ".less", ".sass", ".js", ".php", ".phtml", ".xml" },
21+
var Magento2Profile = Profile{
22+
fileExtensions: []string{ ".css", ".html", ".less", ".sass", ".js", ".php", ".phtml", ".ts", ".xml" },
2323
}
2424

25-
var VueStorefront = Profile{
25+
var VueStorefrontProfile = Profile{
2626
fileExtensions: []string{ ".css", ".js", ".sass", ".ts" },
2727
}

internal/profile/validator/path.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package validator
2+
3+
import (
4+
"github.com/cmuench/inotify-proxy/internal/profile"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
func IsPathValid(path string, selectedProfile string) bool {
10+
11+
fileExtension := filepath.Ext(path)
12+
13+
// Exclude some directories by default
14+
15+
if strings.Contains(path, "node_modules/") {
16+
return false
17+
}
18+
19+
if strings.Contains(path, ".idea/") {
20+
return false
21+
}
22+
23+
switch selectedProfile {
24+
case "magento2":
25+
return profile.Magento2Profile.IsAllowedFileExtension(fileExtension)
26+
case "magento2-theme":
27+
return profile.Magento2ThemeProfile.IsAllowedFileExtension(fileExtension)
28+
case "vue-storefront":
29+
return profile.VueStorefrontProfile.IsAllowedFileExtension(fileExtension)
30+
default:
31+
return true
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package validator
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestDefaultProfile(t *testing.T) {
9+
selectedProfile := ""
10+
assert.True(t, IsPathValid("README.md", selectedProfile))
11+
}
12+
13+
func TestMagentoProfile(t *testing.T) {
14+
selectedProfile := "magento2"
15+
16+
assert.False(t, IsPathValid("README.md", selectedProfile))
17+
assert.True(t, IsPathValid("foo.js", selectedProfile))
18+
assert.True(t, IsPathValid("foo.ts", selectedProfile))
19+
assert.True(t, IsPathValid("foo.php", selectedProfile))
20+
assert.True(t, IsPathValid("foo.phtml", selectedProfile))
21+
assert.True(t, IsPathValid("foo.html", selectedProfile))
22+
}

internal/watcher/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package watcher
2+
3+
type NodeInfo struct {
4+
modificationUnixTime int64
5+
}
6+
7+
var fileMap = make(map[string]NodeInfo)
8+
9+
var selectedProfile = ""

internal/watcher/walker.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package watcher
2+
3+
import (
4+
"github.com/cmuench/inotify-proxy/internal/profile/validator"
5+
"github.com/gookit/color"
6+
"os"
7+
"path/filepath"
8+
"time"
9+
)
10+
11+
func Watch(includedDirectories []string, watchFrequenceSeconds int, profile string) {
12+
selectedProfile = profile
13+
14+
for {
15+
for _, directoryToWalk := range includedDirectories {
16+
err := filepath.Walk(directoryToWalk, visit)
17+
18+
if err != nil {
19+
panic(err)
20+
}
21+
}
22+
23+
time.Sleep(time.Duration(watchFrequenceSeconds) * time.Second)
24+
}
25+
}
26+
27+
func isFileChanged(path string, fileInfo os.FileInfo) bool {
28+
29+
if !validator.IsPathValid(path, selectedProfile) {
30+
return false
31+
}
32+
33+
nodeInfo, found := fileMap[path]
34+
35+
currentModificationTime := fileInfo.ModTime()
36+
37+
changed := false
38+
39+
if !found {
40+
nodeInfo := NodeInfo{
41+
modificationUnixTime: currentModificationTime.Unix(),
42+
}
43+
fileMap[path] = nodeInfo
44+
45+
color.Info.Println("Watching: " + path)
46+
} else {
47+
if nodeInfo.modificationUnixTime < currentModificationTime.Unix() {
48+
changed = true
49+
50+
currentTime := time.Now()
51+
52+
err := os.Chtimes(path, currentModificationTime, currentTime)
53+
54+
if err != nil {
55+
panic("Error touching file" + path)
56+
}
57+
58+
fileMap[path] = NodeInfo{
59+
modificationUnixTime: currentTime.Unix(),
60+
}
61+
}
62+
}
63+
64+
return changed
65+
}
66+
67+
68+
func visit(path string, fileInfo os.FileInfo, err error) error {
69+
70+
if err != nil {
71+
return err
72+
}
73+
74+
if fileInfo.IsDir() {
75+
return nil
76+
}
77+
78+
fileChanged := isFileChanged(path, fileInfo)
79+
80+
if fileChanged {
81+
color.Style{color.FgGreen, color.OpBold}.Printf("Changed: %s | %s\n", path, time.Now().Format("2006-01-02T15:04:05"))
82+
}
83+
84+
return nil
85+
}

0 commit comments

Comments
 (0)