Skip to content

Commit

Permalink
feat: add watch use case
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaschain committed Dec 3, 2023
1 parent 49ef6b3 commit 64db0c6
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 51 deletions.
30 changes: 30 additions & 0 deletions cmd/flags.go
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",
)
}
13 changes: 11 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
49 changes: 0 additions & 49 deletions cmd/run.go

This file was deleted.

10 changes: 10 additions & 0 deletions core/event_types/filter.go
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
}
19 changes: 19 additions & 0 deletions core/event_types/filter_test.go
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))
})
}
38 changes: 38 additions & 0 deletions use_case/watch.go
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)
}

0 comments on commit 64db0c6

Please sign in to comment.