From 64db0c655b0f4c0bd885140b9d95a7c66acc1f98 Mon Sep 17 00:00:00 2001 From: Lucas Chain Date: Sat, 2 Dec 2023 23:26:20 -0300 Subject: [PATCH] feat: add watch use case --- cmd/flags.go | 30 ++++++++++++++++++++ cmd/root.go | 13 +++++++-- cmd/run.go | 49 --------------------------------- core/event_types/filter.go | 10 +++++++ core/event_types/filter_test.go | 19 +++++++++++++ use_case/watch.go | 38 +++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 51 deletions(-) create mode 100644 cmd/flags.go delete mode 100644 cmd/run.go create mode 100644 core/event_types/filter.go create mode 100644 core/event_types/filter_test.go create mode 100644 use_case/watch.go diff --git a/cmd/flags.go b/cmd/flags.go new file mode 100644 index 0000000..c32aa4e --- /dev/null +++ b/cmd/flags.go @@ -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", + ) +} diff --git a/cmd/root.go b/cmd/root.go index eae927c..f8ed80c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,8 +1,11 @@ package cmd import ( - "github.com/spf13/cobra" "os" + "strings" + + "github.com/lucaschain/beholder/use_case" + "github.com/spf13/cobra" ) var Version string = "dev" @@ -24,5 +27,11 @@ func Execute() { } func init() { - setFlags(rootCmd) + SetFlags(rootCmd) +} + +func Run(cmd *cobra.Command, args []string) { + paths := strings.Split(args[0], ",") + command := args[1:] + use_case.Watch(paths, command, types, allowFailing) } diff --git a/cmd/run.go b/cmd/run.go deleted file mode 100644 index e8923f3..0000000 --- a/cmd/run.go +++ /dev/null @@ -1,49 +0,0 @@ -package cmd - -import ( - "fmt" - "log" - "strings" - - "github.com/lucaschain/beholder/core" - "github.com/lucaschain/beholder/core/event_types" - "github.com/lucaschain/beholder/infrastructure" - "github.com/spf13/cobra" -) - -var types []string -var defaultTypes = []string{"WRITE"} - -func onFileChange(command []string) core.ChangeCallback { - return func(event *core.ChangeEvent, err *error) { - if err != nil { - log.Fatal(err) - } - - if event.Type == event_types.Write { - command := core.CommandTokens(command, event) - commandError := infrastructure.Command(command) - - if commandError != nil { - log.Fatal(commandError) - } - } - } -} - -func Run(cmd *cobra.Command, args []string) { - paths := strings.Split(args[0], ",") - var command = strings.Join(args[1:], " ") - fmt.Printf("Watching path: %s and running command: '%s'\n", paths, command) - infrastructure.FileWatcher(paths, onFileChange(args[1:])) -} - -func setFlags(cmd *cobra.Command) { - cmd.Flags().StringSliceVarP( - &types, - "type", - "t", - defaultTypes, - fmt.Sprintf("Event types to watch, options: %s", event_types.EventTypes), - ) -} diff --git a/core/event_types/filter.go b/core/event_types/filter.go new file mode 100644 index 0000000..b0801a0 --- /dev/null +++ b/core/event_types/filter.go @@ -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 +} diff --git a/core/event_types/filter_test.go b/core/event_types/filter_test.go new file mode 100644 index 0000000..d83cbda --- /dev/null +++ b/core/event_types/filter_test.go @@ -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)) + }) +} diff --git a/use_case/watch.go b/use_case/watch.go new file mode 100644 index 0000000..fa9b39c --- /dev/null +++ b/use_case/watch.go @@ -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) +}