Skip to content

Commit

Permalink
Merge pull request #2 from cmuench/feature/yaml-config
Browse files Browse the repository at this point in the history
inotify-proxy.yaml support
  • Loading branch information
cmuench committed Aug 29, 2020
2 parents 0d2f54f + 3a33628 commit 2fbca2f
Show file tree
Hide file tree
Showing 9 changed files with 137 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=
31 changes: 31 additions & 0 deletions inotify-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ 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"
"os"
"strings"
)

Expand All @@ -14,10 +17,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 +38,25 @@ 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, err := config.ReadFile("inotify-proxy.yaml");

if err != nil {
color.Errorf("error: Invalid config provided.\n")
os.Exit(1)
}

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

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

return includedDirectories
}
43 changes: 43 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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, error) {
var (
c Config
err error
yamlData []byte
)
yamlData, err = ioutil.ReadFile(filename)

if err != nil {
return c, err
}

c, err = Parse(yamlData)

return c, err
}

func Parse(yamlData []byte) (Config, error) {
var c Config
err := yaml.Unmarshal(yamlData, &c)

if err != nil {
return c, err
}

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

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestParseValidYaml(t *testing.T) {

validYamlData := `
---
watch:
- dir: /tmp/watch1
- dir: /tmp/watch2
profile: magento2
`
c, err := Parse([]byte(validYamlData))

assert.NoError(t, err, "Config is valid and should not throw an error")
assert.IsType(t, Config{}, c)
}

func TestParseInvalidYaml(t *testing.T) {
invalidYamlData := `
---
watch
`
_, err := Parse([]byte(invalidYamlData))

assert.Error(t, err, "Config is invalid and should throw an error")
}
4 changes: 4 additions & 0 deletions internal/profile/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ var Magento2 = Profile{
var VueStorefront = Profile{
fileExtensions: []string{".css", ".js", ".sass", ".ts"},
}

var Javascript = Profile{
fileExtensions: []string{".js", ".ts"},
}
2 changes: 2 additions & 0 deletions internal/profile/validator/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func IsPathValid(path string, profileName string) bool {
selectedProfile = profile.Magento2Theme
case "vue-storefront":
selectedProfile = profile.VueStorefront
case "javascript":
selectedProfile = profile.Javascript
default:
selectedProfile = profile.Default
}
Expand Down

0 comments on commit 2fbca2f

Please sign in to comment.