From 3198627879e15e4551a2433d16d37c9efc735849 Mon Sep 17 00:00:00 2001 From: Jesse Newland Date: Sat, 9 Feb 2019 20:39:09 -0600 Subject: [PATCH] --watch --- cmd/root.go | 99 +++++-- go.mod | 2 + go.sum | 4 + .../andreaskoch/go-fswatch/.travis.yml | 2 + .../github.com/andreaskoch/go-fswatch/LICENSE | 27 ++ .../andreaskoch/go-fswatch/README.md | 90 +++++++ .../andreaskoch/go-fswatch/debug.go | 33 +++ .../github.com/andreaskoch/go-fswatch/file.go | 192 ++++++++++++++ .../andreaskoch/go-fswatch/folder.go | 250 ++++++++++++++++++ .../andreaskoch/go-fswatch/folderchange.go | 46 ++++ .../github.com/andreaskoch/go-fswatch/util.go | 14 + .../andreaskoch/go-fswatch/watcher.go | 15 ++ .../sabhiram/go-gitignore/.gitignore | 28 ++ .../sabhiram/go-gitignore/.travis.yml | 18 ++ .../github.com/sabhiram/go-gitignore/LICENSE | 22 ++ .../sabhiram/go-gitignore/README.md | 15 ++ .../sabhiram/go-gitignore/ignore.go | 223 ++++++++++++++++ .../sabhiram/go-gitignore/version_gen.go | 12 + vendor/modules.txt | 4 + 19 files changed, 1077 insertions(+), 19 deletions(-) create mode 100644 vendor/github.com/andreaskoch/go-fswatch/.travis.yml create mode 100644 vendor/github.com/andreaskoch/go-fswatch/LICENSE create mode 100644 vendor/github.com/andreaskoch/go-fswatch/README.md create mode 100644 vendor/github.com/andreaskoch/go-fswatch/debug.go create mode 100644 vendor/github.com/andreaskoch/go-fswatch/file.go create mode 100644 vendor/github.com/andreaskoch/go-fswatch/folder.go create mode 100644 vendor/github.com/andreaskoch/go-fswatch/folderchange.go create mode 100644 vendor/github.com/andreaskoch/go-fswatch/util.go create mode 100644 vendor/github.com/andreaskoch/go-fswatch/watcher.go create mode 100644 vendor/github.com/sabhiram/go-gitignore/.gitignore create mode 100644 vendor/github.com/sabhiram/go-gitignore/.travis.yml create mode 100644 vendor/github.com/sabhiram/go-gitignore/LICENSE create mode 100644 vendor/github.com/sabhiram/go-gitignore/README.md create mode 100644 vendor/github.com/sabhiram/go-gitignore/ignore.go create mode 100644 vendor/github.com/sabhiram/go-gitignore/version_gen.go diff --git a/cmd/root.go b/cmd/root.go index 7bfe13d..b3fdc91 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,9 +4,12 @@ import ( "context" "fmt" "os" + "path/filepath" + fswatch "github.com/andreaskoch/go-fswatch" "github.com/nektos/act/actions" "github.com/nektos/act/common" + gitignore "github.com/sabhiram/go-gitignore" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -23,6 +26,7 @@ func Execute(ctx context.Context, version string) { Version: version, SilenceUsage: true, } + rootCmd.Flags().BoolP("watch", "w", false, "watch the contents of the local repo and run when files change") rootCmd.Flags().BoolP("list", "l", false, "list actions") rootCmd.Flags().StringP("action", "a", "", "run action") rootCmd.Flags().BoolVarP(&runnerConfig.ReuseContainers, "reuse", "r", false, "reuse action containers to maintain state") @@ -52,36 +56,93 @@ func newRunCommand(runnerConfig *actions.RunnerConfig) func(*cobra.Command, []st runnerConfig.EventName = args[0] } - // create the runner - runner, err := actions.NewRunner(runnerConfig) + err := parseAndRun(cmd, runnerConfig) if err != nil { return err } - defer runner.Close() - - // check if we should just print the graph - list, err := cmd.Flags().GetBool("list") + watch, err := cmd.Flags().GetBool("watch") if err != nil { return err } - if list { - return drawGraph(runner) + if watch { + return watchAndRun(runnerConfig.Ctx, func() { + err = parseAndRun(cmd, runnerConfig) + }) } - // check if we are running just a single action - actionName, err := cmd.Flags().GetString("action") - if err != nil { - return err - } - if actionName != "" { - return runner.RunActions(actionName) - } - - // run the event in the RunnerRonfig - return runner.RunEvent() + return err } } +func parseAndRun(cmd *cobra.Command, runnerConfig *actions.RunnerConfig) error { + // create the runner + runner, err := actions.NewRunner(runnerConfig) + if err != nil { + return err + } + defer runner.Close() + + // check if we should just print the graph + list, err := cmd.Flags().GetBool("list") + if err != nil { + return err + } + if list { + return drawGraph(runner) + } + + // check if we are running just a single action + actionName, err := cmd.Flags().GetString("action") + if err != nil { + return err + } + if actionName != "" { + return runner.RunActions(actionName) + } + + // run the event in the RunnerRonfig + return runner.RunEvent() +} + +func watchAndRun(ctx context.Context, fn func()) error { + recurse := true + checkIntervalInSeconds := 2 + dir, err := os.Getwd() + if err != nil { + return err + } + + var ignore *gitignore.GitIgnore + if _, err := os.Stat(filepath.Join(dir, ".gitignore")); !os.IsNotExist(err) { + ignore, _ = gitignore.CompileIgnoreFile(filepath.Join(dir, ".gitignore")) + } else { + ignore = &gitignore.GitIgnore{} + } + + folderWatcher := fswatch.NewFolderWatcher( + dir, + recurse, + ignore.MatchesPath, + checkIntervalInSeconds, + ) + + folderWatcher.Start() + log.Debugf("Watching %s for changes", dir) + + go func() { + for folderWatcher.IsRunning() { + for changes := range folderWatcher.ChangeDetails() { + log.Debugf("%s", changes.String()) + fn() + log.Debugf("Watching %s for changes", dir) + } + } + }() + <-ctx.Done() + folderWatcher.Stop() + return nil +} + func drawGraph(runner actions.Runner) error { eventNames := runner.ListEvents() for _, eventName := range eventNames { diff --git a/go.mod b/go.mod index 6e8b3f6..75fc024 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ require ( github.com/Microsoft/go-winio v0.4.11 // indirect github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect github.com/actions/workflow-parser v1.0.0 + github.com/andreaskoch/go-fswatch v1.0.0 github.com/containerd/continuity v0.0.0-20181203112020-004b46473808 // indirect github.com/docker/distribution v2.7.1+incompatible // indirect github.com/docker/docker v1.13.1 @@ -24,6 +25,7 @@ require ( github.com/opencontainers/image-spec v1.0.1 // indirect github.com/opencontainers/runc v0.1.1 // indirect github.com/pkg/errors v0.8.1 // indirect + github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 github.com/sirupsen/logrus v1.3.0 github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 // indirect github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c // indirect diff --git a/go.sum b/go.sum index 8d099d1..8ff9c36 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/actions/workflow-parser v1.0.0 h1:Zz2Ke31f3OMYCSzU2pqZSsk/Oz+lWXfEiXM github.com/actions/workflow-parser v1.0.0/go.mod h1:jz9ZVl8zUIcjMfDQearQjvUHIBhx9l1ys4keDd6be34= github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs= github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= +github.com/andreaskoch/go-fswatch v1.0.0 h1:la8nP/HiaFCxP2IM6NZNUCoxgLWuyNFgH0RligBbnJU= +github.com/andreaskoch/go-fswatch v1.0.0/go.mod h1:r5/iV+4jfwoY2sYqBkg8vpF04ehOvEl4qPptVGdxmqo= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= @@ -91,6 +93,8 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 h1:G04eS0JkAIVZfaJLjla9dNxkJCPiKIGZlw9AfOhzOD0= +github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94/go.mod h1:b18R55ulyQ/h3RaWyloPyER7fWQVZvimKKhnI5OfrJQ= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME= diff --git a/vendor/github.com/andreaskoch/go-fswatch/.travis.yml b/vendor/github.com/andreaskoch/go-fswatch/.travis.yml new file mode 100644 index 0000000..85d02cf --- /dev/null +++ b/vendor/github.com/andreaskoch/go-fswatch/.travis.yml @@ -0,0 +1,2 @@ +language: go +go: 1.1 \ No newline at end of file diff --git a/vendor/github.com/andreaskoch/go-fswatch/LICENSE b/vendor/github.com/andreaskoch/go-fswatch/LICENSE new file mode 100644 index 0000000..7f1dffa --- /dev/null +++ b/vendor/github.com/andreaskoch/go-fswatch/LICENSE @@ -0,0 +1,27 @@ +"New BSD License" + +Copyright (c) 2013, Andreas Koch "Andyk" +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/vendor/github.com/andreaskoch/go-fswatch/README.md b/vendor/github.com/andreaskoch/go-fswatch/README.md new file mode 100644 index 0000000..1f05e54 --- /dev/null +++ b/vendor/github.com/andreaskoch/go-fswatch/README.md @@ -0,0 +1,90 @@ +# fswatch + +fswatch is a go library for watching file system changes to **does not** depend on inotify. + +## Motivation + +Why not use [inotify](http://en.wikipedia.org/wiki/Inotify)? Even though there are great libraries like [fsnotify](https://github.com/howeyc/fsnotify) that offer cross platform file system change notifications - the approach breaks when you want to watch a lot of files or folder. + +For example the default ulimit for Mac OS is set to 512. If you need to watch more files you have to increase the ulimit for open files per process. And this sucks. + +## Usage + +### Watching a single file + +If you want to watch a single file use the `NewFileWatcher` function to create a new file watcher: + +```go +go func() { + fileWatcher := fswatch.NewFileWatcher("Some-file").Start() + + for fileWatcher.IsRunning() { + + select { + case <-fileWatcher.Modified: + + go func() { + // file changed. do something. + }() + + case <-fileWatcher.Moved: + + go func() { + // file moved. do something. + }() + } + + } +}() +``` + +### Watching a folder + +To watch a whole folder for new, modified or deleted files you can use the `NewFolderWatcher` function. + +Parameters: + +1. The directory path +2. A flag indicating whether the folder shall be watched recursively +3. An expression which decides which files are skipped + + +```go +go func() { + + recurse := true + + skipNoFile := func(path string) bool { + return false + } + + folderWatcher := fswatch.NewFolderWatcher("some-directory", recurse, skipNoFile).Start() + + for folderWatcher.IsRunning() { + + select { + case <-folderWatcher.Change: + + go func() { + // some file changed, was added, moved or deleted. + }() + + } + } + +}() +``` + +## Build Status + +[![Build Status](https://travis-ci.org/andreaskoch/go-fswatch.png?branch=master)](https://travis-ci.org/andreaskoch/go-fswatch) + +## Contribute + +If you have an idea + +- how to reliably increase the limit for the maximum number of open files from within the application +- how to overcome the limitations of inotify without having to resort to checking the files for changes over and over again +- or how to make the existing code more efficient + +please send me a message or a pull request. All contributions are welcome. \ No newline at end of file diff --git a/vendor/github.com/andreaskoch/go-fswatch/debug.go b/vendor/github.com/andreaskoch/go-fswatch/debug.go new file mode 100644 index 0000000..a0bd893 --- /dev/null +++ b/vendor/github.com/andreaskoch/go-fswatch/debug.go @@ -0,0 +1,33 @@ +// Copyright 2013 Andreas Koch. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fswatch + +import ( + "fmt" +) + +var ( + debugIsEnabled = false + debugMessages chan string +) + +func EnableDebug() chan string { + debugIsEnabled = true + debugMessages = make(chan string, 10) + return debugMessages +} + +func DisableDebug() { + debugIsEnabled = false + close(debugMessages) +} + +func log(format string, v ...interface{}) { + if !debugIsEnabled { + return + } + + debugMessages <- fmt.Sprint(fmt.Sprintf(format, v...)) +} diff --git a/vendor/github.com/andreaskoch/go-fswatch/file.go b/vendor/github.com/andreaskoch/go-fswatch/file.go new file mode 100644 index 0000000..fcd9d68 --- /dev/null +++ b/vendor/github.com/andreaskoch/go-fswatch/file.go @@ -0,0 +1,192 @@ +// Copyright 2013 Andreas Koch. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fswatch + +import ( + "fmt" + "os" + "time" +) + +var numberOfFileWatchers int + +func init() { + numberOfFolderWatchers = 0 +} + +func NumberOfFileWatchers() int { + return numberOfFileWatchers +} + +type FileWatcher struct { + modified chan bool + moved chan bool + stopped chan bool + + file string + running bool + wasStopped bool + checkInterval time.Duration + + previousModTime time.Time +} + +func NewFileWatcher(filePath string, checkIntervalInSeconds int) *FileWatcher { + + if checkIntervalInSeconds < 1 { + panic(fmt.Sprintf("Cannot create a file watcher with a check interval of %v seconds.", checkIntervalInSeconds)) + } + + return &FileWatcher{ + modified: make(chan bool), + moved: make(chan bool), + stopped: make(chan bool), + + file: filePath, + checkInterval: time.Duration(checkIntervalInSeconds), + } +} + +func (fileWatcher *FileWatcher) String() string { + return fmt.Sprintf("Filewatcher %q", fileWatcher.file) +} + +func (fileWatcher *FileWatcher) SetFile(filePath string) { + fileWatcher.file = filePath +} + +func (filewatcher *FileWatcher) Modified() chan bool { + return filewatcher.modified +} + +func (filewatcher *FileWatcher) Moved() chan bool { + return filewatcher.moved +} + +func (filewatcher *FileWatcher) Stopped() chan bool { + return filewatcher.stopped +} + +func (fileWatcher *FileWatcher) Start() { + fileWatcher.running = true + sleepInterval := time.Second * fileWatcher.checkInterval + + go func() { + + // increment watcher count + numberOfFileWatchers++ + + var modTime time.Time + previousModTime := fileWatcher.getPreviousModTime() + + if timeIsSet(previousModTime) { + modTime = previousModTime + } else { + currentModTime, err := getLastModTimeFromFile(fileWatcher.file) + if err != nil { + + // send out the notification + log("File %q has been moved or is inaccessible.", fileWatcher.file) + go func() { + fileWatcher.moved <- true + }() + + // stop this file watcher + fileWatcher.Stop() + + } else { + + modTime = currentModTime + } + + } + + for fileWatcher.wasStopped == false { + + newModTime, err := getLastModTimeFromFile(fileWatcher.file) + if err != nil { + + // send out the notification + log("File %q has been moved.", fileWatcher.file) + go func() { + fileWatcher.moved <- true + }() + + // stop this file watcher + fileWatcher.Stop() + + continue + } + + // detect changes + if modTime.Before(newModTime) { + + // send out the notification + log("File %q has been modified.", fileWatcher.file) + go func() { + fileWatcher.modified <- true + }() + + } else { + + log("File %q has not changed.", fileWatcher.file) + + } + + // assign the new modtime + modTime = newModTime + + time.Sleep(sleepInterval) + + } + + fileWatcher.running = false + + // capture the entry list for a restart + fileWatcher.captureModTime(modTime) + + // inform channel-subscribers + go func() { + fileWatcher.stopped <- true + }() + + // decrement the watch counter + numberOfFileWatchers-- + + // final log message + log("Stopped file watcher %q", fileWatcher.String()) + }() +} + +func (fileWatcher *FileWatcher) Stop() { + log("Stopping file watcher %q", fileWatcher.String()) + fileWatcher.wasStopped = true +} + +func (fileWatcher *FileWatcher) IsRunning() bool { + return fileWatcher.running +} + +func (fileWatcher *FileWatcher) getPreviousModTime() time.Time { + return fileWatcher.previousModTime +} + +// Remember the last mod time for a later restart +func (fileWatcher *FileWatcher) captureModTime(modTime time.Time) { + fileWatcher.previousModTime = modTime +} + +func getLastModTimeFromFile(file string) (time.Time, error) { + fileInfo, err := os.Stat(file) + if err != nil { + return time.Time{}, err + } + + return fileInfo.ModTime(), nil +} + +func timeIsSet(t time.Time) bool { + return time.Time{} == t +} diff --git a/vendor/github.com/andreaskoch/go-fswatch/folder.go b/vendor/github.com/andreaskoch/go-fswatch/folder.go new file mode 100644 index 0000000..e2a4d06 --- /dev/null +++ b/vendor/github.com/andreaskoch/go-fswatch/folder.go @@ -0,0 +1,250 @@ +// Copyright 2013 Andreas Koch. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fswatch + +import ( + "fmt" + "io/ioutil" + "path/filepath" + "time" +) + +var numberOfFolderWatchers int + +func init() { + numberOfFolderWatchers = 0 +} + +func NumberOfFolderWatchers() int { + return numberOfFolderWatchers +} + +type FolderWatcher struct { + changeDetails chan *FolderChange + + modified chan bool + moved chan bool + stopped chan bool + + recurse bool + skipFile func(path string) bool + + debug bool + folder string + running bool + wasStopped bool + checkInterval time.Duration + + previousEntries []string +} + +func NewFolderWatcher(folderPath string, recurse bool, skipFile func(path string) bool, checkIntervalInSeconds int) *FolderWatcher { + + if checkIntervalInSeconds < 1 { + panic(fmt.Sprintf("Cannot create a folder watcher with a check interval of %v seconds.", checkIntervalInSeconds)) + } + + return &FolderWatcher{ + + modified: make(chan bool), + moved: make(chan bool), + stopped: make(chan bool), + + changeDetails: make(chan *FolderChange), + + recurse: recurse, + skipFile: skipFile, + + debug: true, + folder: folderPath, + checkInterval: time.Duration(checkIntervalInSeconds), + } +} + +func (folderWatcher *FolderWatcher) String() string { + return fmt.Sprintf("Folderwatcher %q", folderWatcher.folder) +} + +func (folderWatcher *FolderWatcher) Modified() chan bool { + return folderWatcher.modified +} + +func (folderWatcher *FolderWatcher) Moved() chan bool { + return folderWatcher.moved +} + +func (folderWatcher *FolderWatcher) Stopped() chan bool { + return folderWatcher.stopped +} + +func (folderWatcher *FolderWatcher) ChangeDetails() chan *FolderChange { + return folderWatcher.changeDetails +} + +func (folderWatcher *FolderWatcher) Start() { + folderWatcher.running = true + sleepInterval := time.Second * folderWatcher.checkInterval + + go func() { + + // get existing entries + var entryList []string + directory := folderWatcher.folder + + previousEntryList := folderWatcher.getPreviousEntryList() + + if previousEntryList != nil { + + // use the entry list from a previous run + entryList = previousEntryList + + } else { + + // use a new entry list + newEntryList, _ := getFolderEntries(directory, folderWatcher.recurse, folderWatcher.skipFile) + entryList = newEntryList + } + + // increment watcher count + numberOfFolderWatchers++ + + for folderWatcher.wasStopped == false { + + // get new entries + updatedEntryList, _ := getFolderEntries(directory, folderWatcher.recurse, folderWatcher.skipFile) + + // check for new items + newItems := make([]string, 0) + modifiedItems := make([]string, 0) + + for _, entry := range updatedEntryList { + + if isNewItem := !sliceContainsElement(entryList, entry); isNewItem { + // entry is new + newItems = append(newItems, entry) + continue + } + + // check if the file changed + if newModTime, err := getLastModTimeFromFile(entry); err == nil { + + // check if file has been modified + timeOfLastCheck := time.Now().Add(sleepInterval * -1) + + if timeOfLastCheck.Before(newModTime) { + + // existing entry has been modified + modifiedItems = append(modifiedItems, entry) + } + + } + } + + // check for moved items + movedItems := make([]string, 0) + for _, entry := range entryList { + isMoved := !sliceContainsElement(updatedEntryList, entry) + if isMoved { + movedItems = append(movedItems, entry) + } + } + + // assign the new list + entryList = updatedEntryList + + // sleep + time.Sleep(sleepInterval) + + // check if something happened + if len(newItems) > 0 || len(movedItems) > 0 || len(modifiedItems) > 0 { + + // send out change + go func() { + folderWatcher.modified <- true + }() + + go func() { + log("Folder %q changed", directory) + folderWatcher.changeDetails <- newFolderChange(newItems, movedItems, modifiedItems) + }() + } else { + log("No change in folder %q", directory) + } + } + + folderWatcher.running = false + + // capture the entry list for a restart + folderWatcher.captureEntryList(entryList) + + // inform channel-subscribers + go func() { + folderWatcher.stopped <- true + }() + + // decrement the watch counter + numberOfFolderWatchers-- + + // final log message + log("Stopped folder watcher %q", folderWatcher.String()) + }() +} + +func (folderWatcher *FolderWatcher) Stop() { + log("Stopping folder watcher %q", folderWatcher.String()) + folderWatcher.wasStopped = true +} + +func (folderWatcher *FolderWatcher) IsRunning() bool { + return folderWatcher.running +} + +func (folderWatcher *FolderWatcher) getPreviousEntryList() []string { + return folderWatcher.previousEntries +} + +// Remember the entry list for a later restart +func (folderWatcher *FolderWatcher) captureEntryList(list []string) { + folderWatcher.previousEntries = list +} + +func getFolderEntries(directory string, recurse bool, skipFile func(path string) bool) ([]string, error) { + + // the return array + entries := make([]string, 0) + + // read the entries of the specified directory + directoryEntries, err := ioutil.ReadDir(directory) + if err != nil { + return entries, err + } + + for _, entry := range directoryEntries { + + // get the full path + subEntryPath := filepath.Join(directory, entry.Name()) + + // recurse or append + if recurse && entry.IsDir() { + + // recurse (ignore errors, unreadable sub directories don't hurt much) + subFolderEntries, _ := getFolderEntries(subEntryPath, recurse, skipFile) + entries = append(entries, subFolderEntries...) + + } else { + + // check if the enty shall be ignored + if skipFile(subEntryPath) { + continue + } + + // append entry + entries = append(entries, subEntryPath) + } + + } + + return entries, nil +} diff --git a/vendor/github.com/andreaskoch/go-fswatch/folderchange.go b/vendor/github.com/andreaskoch/go-fswatch/folderchange.go new file mode 100644 index 0000000..2d30cf2 --- /dev/null +++ b/vendor/github.com/andreaskoch/go-fswatch/folderchange.go @@ -0,0 +1,46 @@ +// Copyright 2013 Andreas Koch. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fswatch + +import ( + "fmt" + "time" +) + +type FolderChange struct { + timeStamp time.Time + newItems []string + movedItems []string + modifiedItems []string +} + +func newFolderChange(newItems, movedItems, modifiedItems []string) *FolderChange { + return &FolderChange{ + timeStamp: time.Now(), + newItems: newItems, + movedItems: movedItems, + modifiedItems: modifiedItems, + } +} + +func (folderChange *FolderChange) String() string { + return fmt.Sprintf("Folderchange (timestamp: %s, new: %d, moved: %d)", folderChange.timeStamp, len(folderChange.New()), len(folderChange.Moved())) +} + +func (folderChange *FolderChange) TimeStamp() time.Time { + return folderChange.timeStamp +} + +func (folderChange *FolderChange) New() []string { + return folderChange.newItems +} + +func (folderChange *FolderChange) Moved() []string { + return folderChange.movedItems +} + +func (folderChange *FolderChange) Modified() []string { + return folderChange.modifiedItems +} diff --git a/vendor/github.com/andreaskoch/go-fswatch/util.go b/vendor/github.com/andreaskoch/go-fswatch/util.go new file mode 100644 index 0000000..96003a2 --- /dev/null +++ b/vendor/github.com/andreaskoch/go-fswatch/util.go @@ -0,0 +1,14 @@ +// Copyright 2013 Andreas Koch. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fswatch + +func sliceContainsElement(list []string, elem string) bool { + for _, t := range list { + if t == elem { + return true + } + } + return false +} diff --git a/vendor/github.com/andreaskoch/go-fswatch/watcher.go b/vendor/github.com/andreaskoch/go-fswatch/watcher.go new file mode 100644 index 0000000..e089d61 --- /dev/null +++ b/vendor/github.com/andreaskoch/go-fswatch/watcher.go @@ -0,0 +1,15 @@ +// Copyright 2013 Andreas Koch. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fswatch + +type Watcher interface { + Modified() chan bool + Moved() chan bool + Stopped() chan bool + + Start() + Stop() + IsRunning() bool +} diff --git a/vendor/github.com/sabhiram/go-gitignore/.gitignore b/vendor/github.com/sabhiram/go-gitignore/.gitignore new file mode 100644 index 0000000..0e919af --- /dev/null +++ b/vendor/github.com/sabhiram/go-gitignore/.gitignore @@ -0,0 +1,28 @@ +# Package test fixtures +test_fixtures + +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof + diff --git a/vendor/github.com/sabhiram/go-gitignore/.travis.yml b/vendor/github.com/sabhiram/go-gitignore/.travis.yml new file mode 100644 index 0000000..24ddadf --- /dev/null +++ b/vendor/github.com/sabhiram/go-gitignore/.travis.yml @@ -0,0 +1,18 @@ +language: go + +go: + - 1.3 + - tip + +env: + - "PATH=$HOME/gopath/bin:$PATH" + +before_install: + - go get github.com/stretchr/testify/assert + - go get github.com/axw/gocov/gocov + - go get github.com/mattn/goveralls + - if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi + +script: + - go test -v -covermode=count -coverprofile=coverage.out + - goveralls -coverprofile=coverage.out -service travis-ci -repotoken $COVERALLS_TOKEN diff --git a/vendor/github.com/sabhiram/go-gitignore/LICENSE b/vendor/github.com/sabhiram/go-gitignore/LICENSE new file mode 100644 index 0000000..c606f49 --- /dev/null +++ b/vendor/github.com/sabhiram/go-gitignore/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Shaba Abhiram + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/vendor/github.com/sabhiram/go-gitignore/README.md b/vendor/github.com/sabhiram/go-gitignore/README.md new file mode 100644 index 0000000..9440d6d --- /dev/null +++ b/vendor/github.com/sabhiram/go-gitignore/README.md @@ -0,0 +1,15 @@ +# go-gitignore + +[![Build Status](https://travis-ci.org/sabhiram/go-gitignore.svg)](https://travis-ci.org/sabhiram/go-gitignore) [![Coverage Status](https://coveralls.io/repos/github/sabhiram/go-gitignore/badge.svg?branch=master)](https://coveralls.io/github/sabhiram/go-gitignore?branch=master) + +A gitignore parser for `Go` + +## Install + +```shell +go get github.com/sabhiram/go-gitignore +``` + +## Usage + +For a quick sample of how to use this library, check out the tests under `ignore_test.go`. diff --git a/vendor/github.com/sabhiram/go-gitignore/ignore.go b/vendor/github.com/sabhiram/go-gitignore/ignore.go new file mode 100644 index 0000000..846b52c --- /dev/null +++ b/vendor/github.com/sabhiram/go-gitignore/ignore.go @@ -0,0 +1,223 @@ +/* +ignore is a library which returns a new ignorer object which can +test against various paths. This is particularly useful when trying +to filter files based on a .gitignore document + +The rules for parsing the input file are the same as the ones listed +in the Git docs here: http://git-scm.com/docs/gitignore + +The summarized version of the same has been copied here: + + 1. A blank line matches no files, so it can serve as a separator + for readability. + 2. A line starting with # serves as a comment. Put a backslash ("\") + in front of the first hash for patterns that begin with a hash. + 3. Trailing spaces are ignored unless they are quoted with backslash ("\"). + 4. An optional prefix "!" which negates the pattern; any matching file + excluded by a previous pattern will become included again. It is not + possible to re-include a file if a parent directory of that file is + excluded. Git doesn’t list excluded directories for performance reasons, + so any patterns on contained files have no effect, no matter where they + are defined. Put a backslash ("\") in front of the first "!" for + patterns that begin with a literal "!", for example, "\!important!.txt". + 5. If the pattern ends with a slash, it is removed for the purpose of the + following description, but it would only find a match with a directory. + In other words, foo/ will match a directory foo and paths underneath it, + but will not match a regular file or a symbolic link foo (this is + consistent with the way how pathspec works in general in Git). + 6. If the pattern does not contain a slash /, Git treats it as a shell glob + pattern and checks for a match against the pathname relative to the + location of the .gitignore file (relative to the toplevel of the work + tree if not from a .gitignore file). + 7. Otherwise, Git treats the pattern as a shell glob suitable for + consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the + pattern will not match a / in the pathname. For example, + "Documentation/*.html" matches "Documentation/git.html" but not + "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html". + 8. A leading slash matches the beginning of the pathname. For example, + "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c". + 9. Two consecutive asterisks ("**") in patterns matched against full + pathname may have special meaning: + i. A leading "**" followed by a slash means match in all directories. + For example, "** /foo" matches file or directory "foo" anywhere, + the same as pattern "foo". "** /foo/bar" matches file or directory + "bar" anywhere that is directly under directory "foo". + ii. A trailing "/**" matches everything inside. For example, "abc/**" + matches all files inside directory "abc", relative to the location + of the .gitignore file, with infinite depth. + iii. A slash followed by two consecutive asterisks then a slash matches + zero or more directories. For example, "a/** /b" matches "a/b", + "a/x/b", "a/x/y/b" and so on. + iv. Other consecutive asterisks are considered invalid. */ +package ignore + +import ( + "io/ioutil" + "os" + "regexp" + "strings" +) + +//////////////////////////////////////////////////////////// + +// An IgnoreParser is an interface which exposes a single method: +// MatchesPath() - Returns true if the path is targeted by the patterns compiled +// in the GitIgnore structure +type IgnoreParser interface { + MatchesPath(f string) bool +} + +//////////////////////////////////////////////////////////// + +// This function pretty much attempts to mimic the parsing rules +// listed above at the start of this file +func getPatternFromLine(line string) (*regexp.Regexp, bool) { + // Trim OS-specific carriage returns. + line = strings.TrimRight(line, "\r") + + // Strip comments [Rule 2] + if strings.HasPrefix(line, `#`) { + return nil, false + } + + // Trim string [Rule 3] + // TODO: Handle [Rule 3], when the " " is escaped with a \ + line = strings.Trim(line, " ") + + // Exit for no-ops and return nil which will prevent us from + // appending a pattern against this line + if line == "" { + return nil, false + } + + // TODO: Handle [Rule 4] which negates the match for patterns leading with "!" + negatePattern := false + if line[0] == '!' { + negatePattern = true + line = line[1:] + } + + // Handle [Rule 2, 4], when # or ! is escaped with a \ + // Handle [Rule 4] once we tag negatePattern, strip the leading ! char + if regexp.MustCompile(`^(\#|\!)`).MatchString(line) { + line = line[1:] + } + + // If we encounter a foo/*.blah in a folder, prepend the / char + if regexp.MustCompile(`([^\/+])/.*\*\.`).MatchString(line) && line[0] != '/' { + line = "/" + line + } + + // Handle escaping the "." char + line = regexp.MustCompile(`\.`).ReplaceAllString(line, `\.`) + + magicStar := "#$~" + + // Handle "/**/" usage + if strings.HasPrefix(line, "/**/") { + line = line[1:] + } + line = regexp.MustCompile(`/\*\*/`).ReplaceAllString(line, `(/|/.+/)`) + line = regexp.MustCompile(`\*\*/`).ReplaceAllString(line, `(|.`+magicStar+`/)`) + line = regexp.MustCompile(`/\*\*`).ReplaceAllString(line, `(|/.`+magicStar+`)`) + + // Handle escaping the "*" char + line = regexp.MustCompile(`\\\*`).ReplaceAllString(line, `\`+magicStar) + line = regexp.MustCompile(`\*`).ReplaceAllString(line, `([^/]*)`) + + // Handle escaping the "?" char + line = strings.Replace(line, "?", `\?`, -1) + + line = strings.Replace(line, magicStar, "*", -1) + + // Temporary regex + var expr = "" + if strings.HasSuffix(line, "/") { + expr = line + "(|.*)$" + } else { + expr = line + "(|/.*)$" + } + if strings.HasPrefix(expr, "/") { + expr = "^(|/)" + expr[1:] + } else { + expr = "^(|.*/)" + expr + } + pattern, _ := regexp.Compile(expr) + + return pattern, negatePattern +} + +//////////////////////////////////////////////////////////// + +// ignorePattern encapsulates a pattern and if it is a negated pattern. +type ignorePattern struct { + pattern *regexp.Regexp + negate bool +} + +// GitIgnore wraps a list of ignore pattern. +type GitIgnore struct { + patterns []*ignorePattern +} + +// Accepts a variadic set of strings, and returns a GitIgnore object which +// converts and appends the lines in the input to regexp.Regexp patterns +// held within the GitIgnore objects "patterns" field +func CompileIgnoreLines(lines ...string) (*GitIgnore, error) { + gi := &GitIgnore{} + for _, line := range lines { + pattern, negatePattern := getPatternFromLine(line) + if pattern != nil { + ip := &ignorePattern{pattern, negatePattern} + gi.patterns = append(gi.patterns, ip) + } + } + return gi, nil +} + +// Accepts a ignore file as the input, parses the lines out of the file +// and invokes the CompileIgnoreLines method +func CompileIgnoreFile(fpath string) (*GitIgnore, error) { + buffer, error := ioutil.ReadFile(fpath) + if error == nil { + s := strings.Split(string(buffer), "\n") + return CompileIgnoreLines(s...) + } + return nil, error +} + +// Accepts a ignore file as the input, parses the lines out of the file +// and invokes the CompileIgnoreLines method with additional lines +func CompileIgnoreFileAndLines(fpath string, lines ...string) (*GitIgnore, error) { + buffer, error := ioutil.ReadFile(fpath) + if error == nil { + s := strings.Split(string(buffer), "\n") + return CompileIgnoreLines(append(s, lines...)...) + } + return nil, error +} + +//////////////////////////////////////////////////////////// + +// MatchesPath returns true if the given GitIgnore structure would target +// a given path string `f`. +func (gi *GitIgnore) MatchesPath(f string) bool { + // Replace OS-specific path separator. + f = strings.Replace(f, string(os.PathSeparator), "/", -1) + + matchesPath := false + for _, ip := range gi.patterns { + if ip.pattern.MatchString(f) { + // If this is a regular target (not negated with a gitignore exclude "!" etc) + if !ip.negate { + matchesPath = true + } else if matchesPath { + // Negated pattern, and matchesPath is already set + matchesPath = false + } + } + } + return matchesPath +} + +//////////////////////////////////////////////////////////// diff --git a/vendor/github.com/sabhiram/go-gitignore/version_gen.go b/vendor/github.com/sabhiram/go-gitignore/version_gen.go new file mode 100644 index 0000000..720a057 --- /dev/null +++ b/vendor/github.com/sabhiram/go-gitignore/version_gen.go @@ -0,0 +1,12 @@ +package ignore + +// WARNING: Auto generated version file. Do not edit this file by hand. +// WARNING: go get github.com/sabhiram/gover to manage this file. +// Version: 1.0.2 +const ( + Major = 1 + Minor = 0 + Patch = 2 + + Version = "1.0.2" +) diff --git a/vendor/modules.txt b/vendor/modules.txt index 35b057b..5d8d059 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -3,6 +3,8 @@ github.com/Microsoft/go-winio # github.com/actions/workflow-parser v1.0.0 github.com/actions/workflow-parser/model github.com/actions/workflow-parser/parser +# github.com/andreaskoch/go-fswatch v1.0.0 +github.com/andreaskoch/go-fswatch # github.com/containerd/continuity v0.0.0-20181203112020-004b46473808 github.com/containerd/continuity/pathdriver # github.com/davecgh/go-spew v1.1.1 @@ -96,6 +98,8 @@ github.com/pelletier/go-buffruneio github.com/pkg/errors # github.com/pmezard/go-difflib v1.0.0 github.com/pmezard/go-difflib/difflib +# github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 +github.com/sabhiram/go-gitignore # github.com/sergi/go-diff v1.0.0 github.com/sergi/go-diff/diffmatchpatch # github.com/sirupsen/logrus v1.3.0