Skip to content

Commit

Permalink
feat: add --type flag
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaschain committed Dec 3, 2023
1 parent adbf509 commit 49ef6b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ func Execute() {
os.Exit(1)
}
}

func init() {
setFlags(rootCmd)
}
13 changes: 13 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"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 {
Expand All @@ -34,3 +37,13 @@ func Run(cmd *cobra.Command, args []string) {
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),
)
}
3 changes: 2 additions & 1 deletion core/command_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/lucaschain/beholder/core"
"github.com/lucaschain/beholder/core/event_types"
"github.com/stretchr/testify/assert"
)

Expand All @@ -18,7 +19,7 @@ func TestCommandTokens(t *testing.T) {
}
event := core.ChangeEvent{
FileName: "file.txt",
Type: "WRITE",
Type: event_types.Write,
}

tokens := core.CommandTokens(list, &event)
Expand Down
18 changes: 11 additions & 7 deletions core/event_types/event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ var (
Write EventType = "WRITE"
Remove EventType = "REMOVE"
Rename EventType = "RENAME"
Chmod EventType = "CHMOD"
)

func (e EventType) IsValid() bool {
switch e {
case Create, Write, Remove, Rename:
return true
}
return false
}
var EventTypes = []EventType{Create, Write, Remove, Rename, Chmod}

func (e EventType) String() string {
return string(e)
}

func FromString(str string) EventType {
for _, event := range EventTypes {
if event.String() == str {
return event
}
}
return ""
}
6 changes: 5 additions & 1 deletion infrastructure/file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/lucaschain/beholder/core"
"github.com/lucaschain/beholder/core/event_types"
)

func loop(watcher *fsnotify.Watcher, callback core.ChangeCallback) {
Expand All @@ -14,7 +15,10 @@ func loop(watcher *fsnotify.Watcher, callback core.ChangeCallback) {
if !ok {
continue
}
changeEvent := core.ChangeEvent{Type: event.Op, FileName: event.Name}
changeEvent := core.ChangeEvent{
Type: event_types.FromString(event.Op.String()),
FileName: event.Name,
}
callback(&changeEvent, nil)
case err, _ := <-watcher.Errors:
if err != nil {
Expand Down

0 comments on commit 49ef6b3

Please sign in to comment.