Skip to content

Commit 2fbca2f

Browse files
authored
Merge pull request #2 from cmuench/feature/yaml-config
inotify-proxy.yaml support
2 parents 0d2f54f + 3a33628 commit 2fbca2f

File tree

9 files changed

+137
-5
lines changed

9 files changed

+137
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
dist/
2020

2121
inotify-proxy
22+
23+
inotify-proxy.yaml

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Enables file watcher in a Docker container with a NFS mounted filesystem.
1111
## Usage
1212

1313
Usage of ./inotify-proxy:
14+
-no-config
15+
Do not load config.
1416
-profile string
1517
Defines a special profile with extensions to look for. This speeds up the process. Available profiles are 'magento2-theme' (default "default")
1618
-sleep int
@@ -27,11 +29,27 @@ Enables file watcher in a Docker container with a NFS mounted filesystem.
2729
# Multiple pathes to watch ...
2830
./inotify-proxy project/path1 project/path2
2931

32+
### Config
33+
34+
If the file `inotify-proxy.yaml` exist in the current working directory, it will be applied.
35+
36+
Example config:
37+
38+
---
39+
watch:
40+
- dir: /tmp/watch1
41+
- dir: /tmp/watch2
42+
profile: magento2
43+
44+
The profile setting is optional.
45+
The config loading can be skiped by adding the option `-no-config`.
46+
3047
## Supported Profiles
3148

3249
| Profile | Allowed file extensions |
3350
|----------------|-------------------------------------------------|
3451
| default | All extensions are allowed |
52+
| javascript | .js .ts |
3553
| magento2 | .css .html .less .sass .js .php .phtml .ts .xml |
3654
| magento2-theme | .css .hs .less .sass .ts |
37-
| vue-storefront | .css .js .sass ts |
55+
| vue-storefront | .css .js .sass .ts |

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ require (
66
github.com/gookit/color v1.2.7
77
github.com/karrick/godirwalk v1.16.1
88
github.com/stretchr/testify v1.3.0
9+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
910
)

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
99
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1010
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
1111
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
12-
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
13-
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
1412
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1513
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
16-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
17-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
14+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
15+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

inotify-proxy.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package main
22

33
import (
44
"flag"
5+
"github.com/cmuench/inotify-proxy/internal/config"
6+
"github.com/cmuench/inotify-proxy/internal/util"
57
"github.com/cmuench/inotify-proxy/internal/watcher"
68
"github.com/gookit/color"
9+
"os"
710
"strings"
811
)
912

@@ -14,10 +17,16 @@ func main() {
1417

1518
sleepPtr := flag.Int("sleep", 2, "Cycle time in seconds. Defines time to sleep after each filesystem walk. Default 2s")
1619
profilePtr := flag.String("profile", "default", "Defines a special profile with extensions to look for. This speeds up the process. Available profiles are 'magento2-theme'")
20+
noConfig := flag.Bool("no-config", false, "Do not load config.")
1721

1822
flag.Parse()
1923

2024
includedDirectories := flag.Args()
25+
c := config.Config{}
26+
27+
if !*noConfig {
28+
includedDirectories = loadConfig(c, includedDirectories, profilePtr)
29+
}
2130

2231
// If no argument is defined, the current directory is used
2332
if len(includedDirectories) == 0 {
@@ -29,3 +38,25 @@ func main() {
2938

3039
watcher.Watch(includedDirectories, *sleepPtr, *profilePtr)
3140
}
41+
42+
func loadConfig(c config.Config, includedDirectories []string, profilePtr *string) []string {
43+
if util.FileExists("inotify-proxy.yaml") {
44+
color.Info.Println("load config")
45+
c, err := config.ReadFile("inotify-proxy.yaml");
46+
47+
if err != nil {
48+
color.Errorf("error: Invalid config provided.\n")
49+
os.Exit(1)
50+
}
51+
52+
for _, watch := range c.Watch {
53+
includedDirectories = append(includedDirectories, watch.Dir)
54+
}
55+
56+
if c.Profile != "" {
57+
*profilePtr = c.Profile
58+
}
59+
}
60+
61+
return includedDirectories
62+
}

internal/config/config.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package config
2+
3+
import (
4+
"gopkg.in/yaml.v3"
5+
"io/ioutil"
6+
)
7+
8+
type Watch struct {
9+
Dir string `yaml:"dir"`
10+
}
11+
12+
type Config struct {
13+
Watch []Watch `yaml:"watch"`
14+
Profile string `yaml:"profile"`
15+
}
16+
17+
func ReadFile(filename string) (Config, error) {
18+
var (
19+
c Config
20+
err error
21+
yamlData []byte
22+
)
23+
yamlData, err = ioutil.ReadFile(filename)
24+
25+
if err != nil {
26+
return c, err
27+
}
28+
29+
c, err = Parse(yamlData)
30+
31+
return c, err
32+
}
33+
34+
func Parse(yamlData []byte) (Config, error) {
35+
var c Config
36+
err := yaml.Unmarshal(yamlData, &c)
37+
38+
if err != nil {
39+
return c, err
40+
}
41+
42+
return c, nil
43+
}

internal/config/config_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package config
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestParseValidYaml(t *testing.T) {
9+
10+
validYamlData := `
11+
---
12+
watch:
13+
- dir: /tmp/watch1
14+
- dir: /tmp/watch2
15+
profile: magento2
16+
17+
`
18+
c, err := Parse([]byte(validYamlData))
19+
20+
assert.NoError(t, err, "Config is valid and should not throw an error")
21+
assert.IsType(t, Config{}, c)
22+
}
23+
24+
func TestParseInvalidYaml(t *testing.T) {
25+
invalidYamlData := `
26+
---
27+
watch
28+
29+
`
30+
_, err := Parse([]byte(invalidYamlData))
31+
32+
assert.Error(t, err, "Config is invalid and should throw an error")
33+
}

internal/profile/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ var Magento2 = Profile{
6060
var VueStorefront = Profile{
6161
fileExtensions: []string{".css", ".js", ".sass", ".ts"},
6262
}
63+
64+
var Javascript = Profile{
65+
fileExtensions: []string{".js", ".ts"},
66+
}

internal/profile/validator/path.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func IsPathValid(path string, profileName string) bool {
1515
selectedProfile = profile.Magento2Theme
1616
case "vue-storefront":
1717
selectedProfile = profile.VueStorefront
18+
case "javascript":
19+
selectedProfile = profile.Javascript
1820
default:
1921
selectedProfile = profile.Default
2022
}

0 commit comments

Comments
 (0)