Skip to content

Commit

Permalink
Add inotify-proxy.yaml support
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuench committed Aug 29, 2020
1 parent 3ad09c0 commit 8f9a548
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
dist/

inotify-proxy

inotify-proxy.yaml
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Enables file watcher in a Docker container with a NFS mounted filesystem.
## Usage

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

### Config

If the file `inotify-proxy.yaml` exist in the current working directory, it will be applied.

Example config:

---
watch:
- dir: /tmp/watch1
- dir: /tmp/watch2
profile: magento2

The profile setting is optional.
The config loading can be skiped by adding the option `-no-config`.

## Supported Profiles

| Profile | Allowed file extensions |
|----------------|-------------------------------------------------|
| default | All extensions are allowed |
| javascript | .js .ts |
| magento2 | .css .html .less .sass .js .php .phtml .ts .xml |
| magento2-theme | .css .hs .less .sass .ts |
| vue-storefront | .css .js .sass ts |
| vue-storefront | .css .js .sass .ts |
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
github.com/gookit/color v1.2.7
github.com/karrick/godirwalk v1.16.1
github.com/stretchr/testify v1.3.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
25 changes: 25 additions & 0 deletions inotify-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"flag"
"github.com/cmuench/inotify-proxy/internal/config"
"github.com/cmuench/inotify-proxy/internal/util"
"github.com/cmuench/inotify-proxy/internal/watcher"
"github.com/gookit/color"
"strings"
Expand All @@ -14,10 +16,16 @@ func main() {

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

flag.Parse()

includedDirectories := flag.Args()
c := config.Config{}

if !*noConfig {
includedDirectories = loadConfig(c, includedDirectories, profilePtr)
}

// If no argument is defined, the current directory is used
if len(includedDirectories) == 0 {
Expand All @@ -29,3 +37,20 @@ func main() {

watcher.Watch(includedDirectories, *sleepPtr, *profilePtr)
}

func loadConfig(c config.Config, includedDirectories []string, profilePtr *string) []string {
if util.FileExists("inotify-proxy.yaml") {
color.Info.Println("load config")
c = config.ReadFile("inotify-proxy.yaml")

for _, watch := range c.Watch {
includedDirectories = append(includedDirectories, watch.Dir)
}

if c.Profile != "" {
*profilePtr = c.Profile
}
}

return includedDirectories
}
33 changes: 33 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package config

import (
"gopkg.in/yaml.v3"
"io/ioutil"
)

type Watch struct {
Dir string `yaml:"dir"`
}

type Config struct {
Watch []Watch `yaml:"watch"`
Profile string `yaml:"profile"`
}

func ReadFile(filename string) Config {
yamlData, err := ioutil.ReadFile(filename)

if err != nil {
panic(err)
}

var c Config
err = yaml.Unmarshal(yamlData, &c)

if err != nil {
panic(err)
}

return c
}

0 comments on commit 8f9a548

Please sign in to comment.