From 3ad09c0e5c3c3812b582beb6a48768a22ba3aaf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCnch?= Date: Sat, 29 Aug 2020 16:09:51 +0200 Subject: [PATCH 1/3] Add new javascript profile --- internal/profile/types.go | 4 ++++ internal/profile/validator/path.go | 2 ++ 2 files changed, 6 insertions(+) diff --git a/internal/profile/types.go b/internal/profile/types.go index 292ca71..726c38c 100644 --- a/internal/profile/types.go +++ b/internal/profile/types.go @@ -60,3 +60,7 @@ var Magento2 = Profile{ var VueStorefront = Profile{ fileExtensions: []string{".css", ".js", ".sass", ".ts"}, } + +var Javascript = Profile{ + fileExtensions: []string{".js", ".ts"}, +} diff --git a/internal/profile/validator/path.go b/internal/profile/validator/path.go index 898071e..c91c8d8 100644 --- a/internal/profile/validator/path.go +++ b/internal/profile/validator/path.go @@ -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 } From 8f9a54839d4996b564883d30838b05730397d700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCnch?= Date: Sat, 29 Aug 2020 16:10:41 +0200 Subject: [PATCH 2/3] Add inotify-proxy.yaml support --- .gitignore | 2 ++ README.md | 20 +++++++++++++++++++- go.mod | 1 + go.sum | 6 ++---- inotify-proxy.go | 25 +++++++++++++++++++++++++ internal/config/config.go | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 internal/config/config.go diff --git a/.gitignore b/.gitignore index 68c5776..5bedd2b 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ dist/ inotify-proxy + +inotify-proxy.yaml \ No newline at end of file diff --git a/README.md b/README.md index 19110c6..166f404 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 | diff --git a/go.mod b/go.mod index db7cb39..91114bb 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index d7aa59b..53e5a24 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/inotify-proxy.go b/inotify-proxy.go index 9ef8990..c8c28b8 100644 --- a/inotify-proxy.go +++ b/inotify-proxy.go @@ -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" @@ -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 { @@ -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 +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..129be0f --- /dev/null +++ b/internal/config/config.go @@ -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 +} + From 3a3362880170fd1e7c3f8129fa133d2b8ddf8961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCnch?= Date: Sat, 29 Aug 2020 16:38:28 +0200 Subject: [PATCH 3/3] Add better error handling for config loading process --- inotify-proxy.go | 8 +++++++- internal/config/config.go | 24 +++++++++++++++++------- internal/config/config_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 internal/config/config_test.go diff --git a/inotify-proxy.go b/inotify-proxy.go index c8c28b8..fa524a6 100644 --- a/inotify-proxy.go +++ b/inotify-proxy.go @@ -6,6 +6,7 @@ import ( "github.com/cmuench/inotify-proxy/internal/util" "github.com/cmuench/inotify-proxy/internal/watcher" "github.com/gookit/color" + "os" "strings" ) @@ -41,7 +42,12 @@ func main() { 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") + 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) diff --git a/internal/config/config.go b/internal/config/config.go index 129be0f..2460b2b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -14,20 +14,30 @@ type Config struct { Profile string `yaml:"profile"` } -func ReadFile(filename string) Config { - yamlData, err := ioutil.ReadFile(filename) +func ReadFile(filename string) (Config, error) { + var ( + c Config + err error + yamlData []byte + ) + yamlData, err = ioutil.ReadFile(filename) if err != nil { - panic(err) + return c, err } + c, err = Parse(yamlData) + + return c, err +} + +func Parse(yamlData []byte) (Config, error) { var c Config - err = yaml.Unmarshal(yamlData, &c) + err := yaml.Unmarshal(yamlData, &c) if err != nil { - panic(err) + return c, err } - return c + return c, nil } - diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..937bd39 --- /dev/null +++ b/internal/config/config_test.go @@ -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") +}