-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49ef6b3
commit 64db0c6
Showing
6 changed files
with
108 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/lucaschain/beholder/core/event_types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var types []string | ||
var allowFailing bool | ||
var defaultTypes = []string{"WRITE"} | ||
|
||
func SetFlags(cmd *cobra.Command) { | ||
cmd.Flags().StringSliceVarP( | ||
&types, | ||
"type", | ||
"t", | ||
defaultTypes, | ||
fmt.Sprintf("Event types to watch, options: %s", event_types.EventTypes), | ||
) | ||
|
||
cmd.Flags().BoolVarP( | ||
&allowFailing, | ||
"allow-failing", | ||
"f", | ||
false, | ||
"Keep running when command fails", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package event_types | ||
|
||
func Filter(event_type EventType, allowed_types []string) bool { | ||
for _, allowed_type := range allowed_types { | ||
if event_type.String() == allowed_type { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package event_types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lucaschain/beholder/core/event_types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFilter(t *testing.T) { | ||
t.Run("should return true only when allowed contains event type", func(t *testing.T) { | ||
allowed := []string{"CREATE", "WRITE"} | ||
assert.True(t, event_types.Filter(event_types.Create, allowed)) | ||
assert.True(t, event_types.Filter(event_types.Write, allowed)) | ||
assert.False(t, event_types.Filter(event_types.Remove, allowed)) | ||
assert.False(t, event_types.Filter(event_types.Rename, allowed)) | ||
assert.False(t, event_types.Filter(event_types.Chmod, allowed)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package use_case | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/lucaschain/beholder/core" | ||
"github.com/lucaschain/beholder/core/event_types" | ||
"github.com/lucaschain/beholder/infrastructure" | ||
) | ||
|
||
func onFileChange(command []string, allowedTypes []string, allowFailing bool) core.ChangeCallback { | ||
return func(event *core.ChangeEvent, err *error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if event_types.Filter(event.Type, allowedTypes) { | ||
command := core.CommandTokens(command, event) | ||
commandError := infrastructure.Command(command) | ||
|
||
if commandError != nil && !allowFailing { | ||
log.Fatal(commandError) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func Watch( | ||
paths []string, | ||
command []string, | ||
allowedTypes []string, | ||
allowFailing bool, | ||
) { | ||
fmt.Printf("Watching path: %s and running command: '%s'\n", paths, command) | ||
callback := onFileChange(command, allowedTypes, allowFailing) | ||
infrastructure.FileWatcher(paths, callback) | ||
} |